-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
285 lines (238 loc) · 10.5 KB
/
main.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
import math
import base64
from pathlib import Path
from xml.etree.ElementTree import ElementTree, Element, SubElement
from borb.pdf import Document, PDF, Page
import mailchimp_transactional as MailchimpTransactional
from mailchimp_transactional.api_client import ApiClientError
# Defining constants
ROOT = Element('root')
BUYER_TREE = ElementTree(ROOT)
MAILCHIMP_API_KEY = 'YRxKQk091G8qZlp9FjIxtw'
PATH_TO_TEMPLATES = (Path.home() / "PycharmProjects" / "giftlist" / "data")
PATH_BUYER_PAGE = (PATH_TO_TEMPLATES / "buyer_page.pdf")
PATH_RECIPIENT_PAGE = (PATH_TO_TEMPLATES / "recipient_page.pdf")
PATH_BACK_PAGE = (PATH_TO_TEMPLATES / "details_page.pdf")
# Use borb to open pdf_form_example.pdf
with open(str(PATH_BUYER_PAGE), "rb") as file:
BUYER_PAGE = PDF.loads(file)
with open(str(PATH_RECIPIENT_PAGE), "rb") as file:
RECIPIENT_PAGE = PDF.loads(file)
with open(str(PATH_BACK_PAGE), "rb") as file:
BACK_PAGE = PDF.loads(file)
def main_loop(input_file: str):
#######################################################################################
# Main Loop for Gift Lift Process
# Open input_file
# for line in file:
# if (new buyer)
# if data
# call make_giftlist(---)
# call email_giftlist(---)
# call prune_xml()
# fill data with Buyer data
# else
# fill data with Recipient data
#
#######################################################################################
global BUYER_TREE
with open(input_file) as file:
for line in file:
if line[15:25] == "0000000000": # If New Buyer
print('New Buyer')
# If BUYER_TREE is populated, process a new gift list
if len(ROOT) > 0:
print('Processing new gift list')
print('--- calling make_giftlist()')
make_giftlist()
print('--- calling email_giftlist()')
email_giftlist()
print('Pruning XML Tree')
print('--- calling prune_xml()')
prune_xml()
# Fill in new Buyer data
print('Populating root with new Buyer data')
print('--- calling new_buyer(buyer_line)')
new_buyer(buyer_line=line)
else: # We have a new recipient
print('New Recipient')
print('--- calling new_recipient(recipient_line)')
new_recipient(recipient_line=line)
# Finished process new data - need to process last Gift List
if len(ROOT) > 0:
print('Processing new gift list')
print('--- calling make_giftlist()')
make_giftlist()
print('--- calling e-mail_giftlist()')
email_giftlist()
print('Pruning XML Tree')
print('--- calling prune_xml()')
prune_xml()
def make_giftlist():
print('*** making the giftlist ***')
global PATH_TO_TEMPLATES
global BUYER_PAGE, RECIPIENT_PAGE, BACK_PAGE
global ROOT
giftlist = Document()
# Print BUYER details on FRONT_PAGE
page = BUYER_PAGE.get_page(0)
buyer = ROOT.find('buyer')
page.set_form_field_value('b_acct_num', buyer.get('acct_num'))
page.set_form_field_value('b_key_code', buyer.get('key_code'))
page.set_form_field_value('b_name', buyer.get('name'))
buyer_address = [buyer.get('company_name'),
buyer.get('address_line_1'),
buyer.get('address_line_2'),
f'{buyer.get("city")}, {buyer.get("state")} {buyer.get("zip")}']
buyer_address = [x for x in buyer_address if x]
for i in range(len(buyer_address)):
page.set_form_field_value('b_address_'+str(i+1), buyer_address[i])
# Print RECIPIENT details to FRONT_PAGE
recip_list = ROOT.findall('recip')
num_recip = len(recip_list)
for i in range(min(5, num_recip)):
print('Printing recipient data on FRONT_PAGE')
# Printing recipient data to next box
recip = recip_list.pop()
page.set_form_field_value(f'r{i+1}_name', recip.get('name'))
page.set_form_field_value(f'r{i+1}_acct_num', recip.get('acct_num'))
page.set_form_field_value(f'r{i+1}_address_1', recip.get('address_line_1'))
al2 = f"{recip.get('city')}, {recip.get('state')} {recip.get('zip')}"
page.set_form_field_value(f'r{i+1}_address_2', al2)
page.set_form_field_value(f'r{i+1}_item_desc', recip.get('item_desc_page'))
page.set_form_field_value(f'r{i+1}_greeting_1', recip.get('greeting_1'))
page.set_form_field_value(f'r{i+1}_greeting_2', recip.get('greeting_2'))
page.set_form_field_value(f'r{i+1}_greeting_3', recip.get('greeting_3'))
# FRONT_PAGE is complete! Let's save it.
print('Adding completed FRONT_PAGE to Gift List doc')
giftlist.add_page(page)
# Print RECIPIENT details to BACK_PAGE
num_pages = math.ceil((num_recip - 5) / 6)
recips_left = len(recip_list)
for p in range(num_pages):
print('Acquiring new copy of RECIPIENT_PAGE')
page = RECIPIENT_PAGE.get_page(0)
for i in range(min(6, recips_left)):
print('Printing recipient data on page')
# Printing recipient data to next box
recip = recip_list.pop()
page.set_form_field_value(f'r{i + 1}_name', recip.get('name'))
page.set_form_field_value(f'r{i + 1}_acct_num', recip.get('acct_num'))
page.set_form_field_value(f'r{i + 1}_address_1', recip.get('address_line_1'))
al2 = f"{recip.get('city')}, {recip.get('state')} {recip.get('zip')}"
page.set_form_field_value(f'r{i + 1}_address_2', al2)
page.set_form_field_value(f'r{i + 1}_item_desc', recip.get('item_desc_page'))
page.set_form_field_value(f'r{i + 1}_greeting_1', recip.get('greeting_1'))
page.set_form_field_value(f'r{i + 1}_greeting_2', recip.get('greeting_2'))
page.set_form_field_value(f'r{i + 1}_greeting_3', recip.get('greeting_3'))
recips_left = len(recip_list)
print('Adding copy of completed RECIPIENT_PAGE to Gift List doc')
giftlist.add_page(page)
print("Adding copy of BACK_PAGE to Gift List doc")
giftlist.add_page(BACK_PAGE.get_page(0))
print("Saving Gift List to new PDF file")
output_file_name = f"GiftList_for_Acct_{buyer.get('acct_num')}.pdf"
output_path = (PATH_TO_TEMPLATES / output_file_name)
with open(str(output_path), "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, giftlist)
print("The Gift List Order Form for this buyer is complete!")
def email_giftlist():
print('*** emailing the giftlist ***')
global ROOT
# Getting buyer data
buyer = ROOT.find('buyer')
# Getting body of e-mail message
is_corporate = buyer.get('is_corporate')
letter_path = (PATH_TO_TEMPLATES / ('letter_corporate.txt' if is_corporate else 'letter_noncorporate.txt'))
with open(str(letter_path), 'r') as letter_file:
letter_body = letter_file.read()
# Getting attachment details
attachment_name = f"GiftList_for_Acct_{buyer.get('acct_num')}.pdf"
attachment_path = (PATH_TO_TEMPLATES / attachment_name)
with open(str(attachment_path), "rb") as pdf_file:
attachment_content = base64.b64encode(pdf_file.read()).decode('utf-8')
# Setting e-mail details
message = {
"from_email": "[email protected]",
"from_name": "Priester's Pecans",
"subject": "Gift List Order Form",
"text": f"{buyer.get('salutation')}{chr(10)}{chr(10)}{letter_body}",
"to": [
{
"email": buyer.get('email'),
"type": "to"
},
{
"email": "[email protected]",
"type": "bcc"
}
],
"attachments": [
{
"type": 'application/pdf',
"name": attachment_name,
"content": attachment_content
}
]
}
try:
mailchimp = MailchimpTransactional.Client(MAILCHIMP_API_KEY)
response = mailchimp.messages.send({"message": message})
print('API called successfully: {}'.format(response))
except ApiClientError as error:
print('An exception occurred: {}'.format(error.text))
def prune_xml():
global BUYER_TREE
BUYER_TREE.getroot().clear()
def new_buyer(buyer_line: str):
global ROOT
# Pull Buyer data from line
buyer_line = buyer_line.rstrip()
assert len(buyer_line) <= 412
buyer_line += ' ' * (412 - len(buyer_line))
buyer_dict = {
'acct_num': buyer_line[5:15].strip(),
'key_code': buyer_line[27:32],
'name': buyer_line[33:63].strip(),
'address_line_1': buyer_line[63:93].strip(),
'address_line_2': buyer_line[93:123].strip(),
'city': buyer_line[123:138].strip(),
'state': buyer_line[138:140].strip(),
'zip': buyer_line[0:5].strip(),
'salutation': buyer_line[145:175].strip(),
'company_name': buyer_line[337:366].strip(),
'email': buyer_line[366:412].strip(),
'is_corporate': (buyer_line[175] == 'Y')
}
# Add 'buyer' node to ROOT
SubElement(ROOT, 'buyer', attrib=buyer_dict)
def new_recipient(recipient_line: str):
global ROOT
# Pull Recipient data from line
recipient_line = recipient_line.rstrip()
assert len(recipient_line) <= 412
recipient_line += ' ' * (412 - len(recipient_line)) # Enforce that the line is 366 characters long
recip_dict = {
'acct_num': recipient_line[15:25].strip(),
'name': recipient_line[33:63].strip(),
'address_line_1': recipient_line[63:93].strip(),
'address_line_2': recipient_line[93:123].strip(),
'city': recipient_line[123:138].strip(),
'state': recipient_line[138:140].strip(),
'zip': recipient_line[0:5].strip(),
'item_desc_page': recipient_line[176:216].strip(),
'greeting_1': recipient_line[216:256].strip(),
'greeting_2': recipient_line[256:296].strip(),
'greeting_3': recipient_line[296:336].strip(),
'company_name': recipient_line[337:366].strip()
}
# Add recipient node to ROOT
SubElement(ROOT, 'recip', attrib=recip_dict)
if __name__ == '__main__':
input_file_path = (
Path.home()
/ "PycharmProjects"
/ "giftlist"
/ "test_data_with_email.txt"
)
main_loop(input_file=str(input_file_path))