-
Notifications
You must be signed in to change notification settings - Fork 0
/
patron-autorenew.py
143 lines (128 loc) · 4.86 KB
/
patron-autorenew.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
#!/usr/bin/env python3
import json
from datetime import datetime, date, timedelta
from calendar import monthrange
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from jinja2 import Environment, FileSystemLoader
from Sierra import SierraAPI
import config
# Configure Sierra API client
sierra = SierraAPI(config.API_URL, config.API_KEY, config.API_SECRET)
# Configure SendGrid API client
sg = SendGridAPIClient(config.SENDGRID_API_KEY)
# Set up Jinja
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
year = int(datetime.now().strftime('%Y')) # Current year as int
month = int(datetime.now().strftime('%m')) # Current month as int
firstOfMonth = datetime.now().strftime('%m-01-%Y') # Date of the first of this month
lastOfMonth = date(year, month, monthrange(year,month)[1]).strftime('%m-%d-%Y') # Date of the last of this month
sixMonthsAgo = date.today() + timedelta(-180)
newExpirationDate = (date.today() + timedelta(365)).strftime('%Y-%m-%d')
# A lists of renewed patrons with and without email addresses will be sent to
# the staff email address provided in config.py
patronsWithEmail = []
patronsWithoutEmail = []
# Query from Create Lists to find patrons expiring this month who have been
# CIRCATIVE within the last 180 days. The %s placeholders are replaced with
# actual YYYY-MM-DD dates.
query = '''
{
"queries": [
{
"target": {
"record": {
"type": "patron"
},
"id": 43
},
"expr": {
"op": "between",
"operands": [
"%s",
"%s"
]
}
},
"and",
{
"target": {
"record": {
"type": "patron"
},
"id": 163
},
"expr": {
"op": "greater_than_or_equal",
"operands": [
"%s",
""
]
}
}
]
}
''' % (firstOfMonth, lastOfMonth, sixMonthsAgo)
# TODO: Currently limited to 10,000 patron record. Refactor this to loop through larger result sets.
patrons = sierra.post(sierra.apiURL + 'patrons/query', params={'offset': 0, 'limit': 10000}, data=query).json()
if patrons['total'] > 0:
for patron in patrons['entries']:
patronData = sierra.get(patron['link'], params={'fields': 'barcodes,expirationDate,emails,addresses'}).json()
patronEmail = ''
patronAddress = ''
patronBarcode = patronData['barcodes'][0]
print('Barcode ending in: ' + patronBarcode[-4:])
for line in patronData['addresses'][0]['lines']:
patronAddress = patronAddress + line + '\n'
patronAddress = patronAddress.strip()
print('Address:\n' + patronAddress)
if 'emails' in patronData:
patronEmail = patronData['emails'][0]
print('Email:' + patronEmail)
else:
print('NO EMAIL ADDRESS')
print('Current Exp: ' + patronData['expirationDate'])
print('New Exp: ' + newExpirationDate)
try:
print('Attempting to renew patron ' + patronBarcode)
sierra.put(patron['link'], data='{"expirationDate": "%s"}' % newExpirationDate)
print('Renewal succeeded')
except:
print('Failed to renew patron ' + patronBarcode)
if 'emails' in patronData:
patronsWithEmail.append(patronBarcode)
emailTemplate = env.get_template('renewal-notice.html')
message = Mail(
from_email = config.FROM_ADDRESS,
to_emails = patronEmail,
subject = 'Your library account has been automatically renewed',
html_content = emailTemplate.render(barcode=patronBarcode[-4:],
expiration=newExpirationDate,
address=patronAddress)
)
try:
response = sg.send(message)
print('Successfully sent renewal notice to: ' + patronEmail)
except Exception as e:
print('Failed to send renewal notice to: ' + patronEmail)
else:
patronsWithoutEmail.append(patronBarcode)
print('\n')
else:
print('No expiring patrons found.')
# Email staff a list of patrons without email addresses that were renewed
emailTemplate = env.get_template('staff-report.html')
message = Mail(
from_email = config.FROM_ADDRESS,
to_emails = config.STAFF_ADDRESS,
subject = 'Patron account renewal report',
html_content = emailTemplate.render(expiration=newExpirationDate,
patronsWithEmail=patronsWithEmail,
patronsWithoutEmail=patronsWithoutEmail)
)
try:
response = sg.send(message)
print('Successfully staff report to: ' + config.STAFF_ADDRESS)
except Exception as e:
print('Failed to send staff report to: ' + config.STAFF_ADDRESS)