-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
141 lines (118 loc) · 3.95 KB
/
lambda_function.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
import sys, rds_config, logging, pymysql, random, datetime, json, os, urllib
from urllib.parse import urlparse, parse_qs
from hashlib import md5
from mailchimp3 import MailChimp
# rds settings
rds_host = rds_config.db_host
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name
# url settings
baseURL = 'https://uais.dev/signup'
successURL = baseURL + '/success'
failURL = baseURL + '/issue'
# mailchimp settings
mailchimpAPI = os.environ['mailchimp_api']
mailchimpID = os.environ['mailchimp_id']
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except pymysql.MySQLError as e:
logger.error('ERROR: Unexpected error: Could not connect to MySQL instance.')
logger.error(e)
sys.exit()
client = client = MailChimp(mc_api=mailchimpAPI, mc_user='UAISalberta')
logger.info('SUCCESS: Connection to RDS MySQL instance succeeded')
def lambda_handler(event, context):
formData = get_form_data(event)
validForm, validItems = validate_form_items(formData)
if not validForm:
return {
'statusCode': 302,
'headers': {
'location': failURL
}
}
db_order, db_statement = create_db_statement(validItems)
## UPDATE MYSQL
with conn.cursor() as cur:
cur.execute(f'INSERT INTO registration {db_order} VALUES {db_statement}')
conn.commit()
## UPDATE MAILC
sub_hash = md5(validItems['email'].lower().encode("utf-8")).hexdigest()
client.lists.members.create_or_update(list_id=mailchimpID, subscriber_hash=sub_hash, data={
'email_address': validItems['email'],
'status_if_new': 'subscribed',
'merge_fields': {
'FNAME': validItems['firstname'],
'LNAME': validItems['lastname'],
},
})
conn.close()
return {
'statusCode': 301,
'headers': {
'location': successURL
}
}
def get_form_data(event):
body = urllib.parse.parse_qs(event['body'])
bodyItems = {key : body[key][0] for key in body}
return bodyItems
def validate_form_items(formItems):
validItems = {}
validItems['etc'] = {}
validMembership = ['regular','associate']
if 'membership' in formItems and formItems['membership'] in validMembership:
if formItems['membership'] == 'regular':
validItems['memberStatus'] = True
if formItems['membership'] == 'associate':
validItems['memberStatus'] = False
else:
return False, validItems
if 'email' in formItems and '@' in formItems['email']:
validItems['email'] = formItems['email'].lower()
else:
return False, validItems
if 'firstname' in formItems:
validItems['firstname'] = formItems['firstname']
if 'lastname' in formItems:
validItems['lastname'] = formItems['lastname']
if 'program' in formItems:
p = formItems['program']
switch={
'cs': 'Computing Science',
'ce': 'Computer Engineering',
'math': 'Mathematics',
'stat': 'Statistics',
'psych': 'Psychology',
'other': 'Other'
}
validItems['etc']['program'] = switch.get(p, 'None')
validYears = ['0','1','2','3','4','5','5+']
if 'year' in formItems and formItems['year'] in validYears:
validItems['etc']['year'] = formItems['year']
validOrgOptions = ['university', 'company', 'other']
if 'org' in formItems and formItems['org'] in validOrgOptions:
o = formItems['org']
switch={
'university': 'University',
'company': 'Company',
'other': 'Other'
}
validItems['etc']['organizationType'] = switch.get(o, 'None')
if 'orgname' in formItems:
validItems['etc']['organizationName'] = formItems['orgname']
if 'position' in formItems:
validItems['etc']['organizationPosition'] = formItems['position']
return True, validItems
def create_db_statement(formData):
email = formData['email']
firstname = formData['firstname']
lastname = formData['lastname']
memberStatus = formData['memberStatus']
etc = str(formData['etc']).replace("'", '"')
db_order = '(email, firstname, lastname, memberStatus, etc)'
db_statement = str(f"('{email}', '{firstname}', '{lastname}', {memberStatus}, '{etc}')")
return db_order, db_statement