-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwts_outbot.py
409 lines (290 loc) · 15.4 KB
/
wts_outbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# ---------------------------------------------------------------------------------------------------------------------#
# wts_outbot.py
#
# This module prepares Wilson Team Shop order emails.
#
# ---------------------------------------------------------------------------------------------------------------------#
import csv
import os
import win32com.client as win32
from configparser import ConfigParser
from datetime import date, timedelta
from pathlib import Path
config_path = Path(__file__).parent.absolute().joinpath('config.ini')
config = ConfigParser()
config.read(config_path)
recipient_list = str(config['Outbot Options']['recipient_list']).split(',')
auto_send = config.getboolean('Outbot Options', 'auto_send')
milb_list = str(config['Special Teams']['milb_list']).split(',')
direct_transfer_list = str(config['Special Teams']['direct_transfer_list']).split(',')
def main(folder_path):
# These variables keep track of what elements are included in the outbound order and which, if any, need to be
# included in the inbound order.
#
# Some rules/assumptions:
#
# Heat transfers and player numbers (which are also heat transfers) are kept on hand and may already be in stock.
# Otherwise, they need to be either made in-house or ordered, a decision beyond the purview of the art department.
#
# Film and embroidery are produced on demand, so if the order has them you can assume it needs them as well.
#
# Player numbers never appear on a garment without other heat transfers. If there are no heat transfers then there
# are no numbers.
has_heat_transfers = False
needs_heat_transfers = False
has_player_numbers = False
needs_player_numbers = False
has_film = False
has_embroidery = False
has_rdx = False
# This block finds the path for the count .CSV in the folder. If it fails to do so, the module is terminated.
csv_path = ''
for file in os.listdir(folder_path):
if file.endswith('.csv'):
csv_path = os.path.join(folder_path, file)
break
if csv_path == '':
print('No count found!')
return
# This opens the count .CSV and checks it for various indicators as described below.
with open(csv_path, 'r', errors="ignore") as csv_count:
reader = csv.reader(csv_count)
# This checks the count sheet for evidence of heat transfers.
for line in reader:
if 'Heat Transfer' in str(line):
has_heat_transfers = True
break
# If there are heat transfers, this checks the count sheet for evidence of player numbers.
csv_count.seek(0)
if has_heat_transfers:
for line in reader:
if 'NUMBERS' in str(line):
has_player_numbers = True
break
# If there are player numbers, this checks if they need to be ordered.
csv_count.seek(0)
if has_player_numbers:
for line in reader:
if 'ORDER' in str(line):
needs_player_numbers = True
break
# This checks the count sheet for evidence of embroidered items.
csv_count.seek(0)
for line in reader:
if 'Embroidery' in str(line):
has_embroidery = True
break
# This checks the folder for evidence of film items.
for file in os.listdir(folder_path):
if 'Film Art Page' in str(file):
has_film = True
break
# This checks the folder for evidence of heat transfers order pages, provided the order features heat transfers.
if has_heat_transfers:
for file in os.listdir(folder_path):
if 'Order Art Page' in str(file):
needs_heat_transfers = True
break
# This checks the folder for evidence of RDX helmets.
for file in os.listdir(folder_path):
if 'RDX Art Page' in str(file):
has_rdx = True
break
# This block selects the type of email(s) that will be sent based on the information gathered above.
if has_film and (needs_heat_transfers or needs_player_numbers):
compose_combo(needs_heat_transfers, has_player_numbers, needs_player_numbers, has_embroidery, folder_path,
csv_path)
elif has_film:
compose_film(has_heat_transfers, has_player_numbers, has_embroidery, folder_path, csv_path)
elif needs_heat_transfers or has_player_numbers:
compose_transfer(needs_heat_transfers, has_player_numbers, needs_player_numbers, has_embroidery, folder_path,
csv_path)
elif has_embroidery and not (has_heat_transfers or has_player_numbers):
compose_embroidery(folder_path, csv_path)
else:
compose_stock(has_embroidery, folder_path, csv_path)
if has_rdx:
compose_rdx(folder_path, csv_path)
def compose_combo(needs_heat_transfers, has_player_numbers, needs_player_numbers, has_embroidery, folder_path,
csv_path):
# This function is used for orders that need heat transfers or numbers AND film elements created.
# For some reason, this situation requires the creation of two separate emails, which this function prepares to do.
#
# Due to heavy redundancy, the other "compose" functions will less thoroughly commented upon.
# This names the first email after the folder and appends the word "FILM" for differentiation.
film_subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1] + ' FILM'
# This sets the body text, which is simpler in this case than in the heat transfer email.
film_body_list = ['This order requires film.']
# This block attaches the count and the film art pages.
film_attachment_list = [csv_path]
for file in os.listdir(folder_path):
if 'Film Art Page' in str(file):
film_attachment_list.append(os.path.join(folder_path, file))
# This calls the mail_maker function to cobble together an email from these elements.
mail_maker(film_subject, film_body_list, film_attachment_list)
# This names the second email after the folder and appends the words "HEAT TRANSFER" for differentiation.
trans_subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1] + ' HEAT TRANSFER'
# This sets the body text depending upon the specific requirements of the order.
trans_body_list = ['This order also requires ']
if needs_heat_transfers and needs_player_numbers:
trans_body_list.append('heat transfers and player numbers.')
elif needs_heat_transfers and has_player_numbers:
trans_body_list.append('heat transfers. Its player numbers are covered by stock.')
elif needs_heat_transfers:
trans_body_list.append('heat transfers. No player numbers.')
else:
trans_body_list.append('player numbers. Its heat transfers are covered by stock.')
if has_embroidery:
trans_body_list.append('\n\nEmbroidered Items')
# This block attaches the count sheet, the MLOrder file, and some combination of art pages and/or number sheets.
trans_attachment_list = [csv_path]
for file in os.listdir(folder_path):
if 'MLOrder' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if 'Order Art Page' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '3N' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '4N' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '6N' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '8N' in str(file):
trans_attachment_list.append(os.path.join(folder_path, file))
# This calls the mail_maker function to cobble together an email from these elements.
mail_maker(trans_subject, trans_body_list, trans_attachment_list)
print('Emails generated: Combo')
return
def compose_film(has_heat_transfers, has_player_numbers, has_embroidery, folder_path, csv_path):
subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1]
# This function is used for orders that need film elements but no heat transfer elements.
# For more documentation, please see the comments on the "compose_combo" function.
if any(word in str(folder_path.split(os.path.sep)[-2]) for word in milb_list):
body_list = ['This order requires film (MiLB). ']
else:
body_list = ['This order requires film. ']
if has_heat_transfers and has_player_numbers:
body_list.append('Its heat transfers and player numbers are covered by stock.')
elif has_heat_transfers:
body_list.append('Its heat transfers are covered by stock. No player numbers.')
else:
body_list.append('No heat transfers. No player numbers.')
if has_embroidery:
body_list.append('\n\nEmbroidered Items')
attachment_list = [csv_path]
for file in os.listdir(folder_path):
if 'Film Art Page' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '3N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '4N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '6N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '8N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
mail_maker(subject, body_list, attachment_list)
print('Email generated: Film')
return
def compose_transfer(needs_heat_transfers, has_player_numbers, needs_player_numbers, has_embroidery, folder_path,
csv_path):
# This function is used for orders that need heat transfer elements but no film elements.
# For more documentation, please see the comments on the "compose_combo" function.
subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1]
body_list = [' No film.']
if needs_heat_transfers and needs_player_numbers:
body_list.insert(0, 'This order requires heat transfers and player numbers.')
elif needs_heat_transfers and has_player_numbers and not needs_player_numbers:
body_list.insert(0, 'This order requires heat transfers. Its player numbers are covered by stock.')
elif needs_heat_transfers and not has_player_numbers:
body_list.insert(0, 'This order requires heat transfers. No player numbers.')
elif needs_player_numbers and not needs_heat_transfers:
body_list.insert(0, 'This order requires player numbers. Its heat transfers are covered by stock.')
else:
body_list.insert(0, 'This order\'s heat transfers and player numbers are covered by stock.')
if has_embroidery:
body_list.append('\n\nEmbroidered Items')
attachment_list = [csv_path]
for file in os.listdir(folder_path):
if 'MLOrder' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if 'Order Art Page' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '3N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '4N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '6N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
for file in os.listdir(folder_path):
if '8N' in str(file):
attachment_list.append(os.path.join(folder_path, file))
mail_maker(subject, body_list, attachment_list)
print('Email generated: Heat Transfer')
return
def compose_embroidery(folder_path, csv_path):
# This function is used for orders that need exclusively embroidered items.
# For more documentation, please see the comments on the "compose_combo" function.
subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1]
body_list = ['This order is all Embroidered Items. No heat transfers. No player numbers. No film.']
attachment_list = [csv_path]
mail_maker(subject, body_list, attachment_list)
print('Email generated: Embroidery')
return
def compose_stock(has_embroidery, folder_path, csv_path):
# This function is used for orders that need nothing but do have heat transfers which are already in stock.
# For more documentation, please see the comments on the "compose_combo" function.
subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1]
if any(word in str(folder_path.split(os.path.sep)[-2]) for word in direct_transfer_list):
body_list = ['This order requires Direct Transfer items. No player numbers. No film.']
else:
body_list = ['This order is covered by stock. No player numbers. No film.']
if has_embroidery:
body_list.append(' \nEmbroidered Items')
attachment_list = [csv_path]
mail_maker(subject, body_list, attachment_list)
print('Email generated: Stock')
return
def compose_rdx(folder_path, csv_path):
# This function creates an additional email for RDX helmets.
# This email is always separate because it seems like a good idea.
subject = folder_path.split(os.path.sep)[-2] + ' ' + folder_path.split(os.path.sep)[-1] + ' RDX'
body_list = ['This order also requires RDX helmet decals.']
attachment_list = [csv_path]
for file in os.listdir(folder_path):
if 'RDX Art Page' in str(file):
attachment_list.append(os.path.join(folder_path, file))
mail_maker(subject, body_list, attachment_list)
print('Email generated: RDX')
return
def mail_maker(subject, body_list, attachment_list):
# This function takes the prepared email components from any one of the "compose" functions and creates an email.
ship_date = date.today() + timedelta(days=7)
ship_date_string = ''.join(('\n\nArt Ship Date: ', ship_date.strftime("%B %d, %Y")))
body_list.append(ship_date_string)
outlook = win32.Dispatch('Outlook.Application')
mail_item = outlook.CreateItem(0)
mail_item.Subject = subject
mail_item.To = '; '.join(recipient_list)
mail_item.Body = ''.join(body_list)
mail_item.BodyFormat = 2
for item in attachment_list:
mail_item.Attachments.Add(item)
# Depending on the configuration, the email is either sent or saved as a draft.
if auto_send:
mail_item.Send()
else:
mail_item.Save()