This repository was archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
306 lines (284 loc) · 17.9 KB
/
Copy pathhandler.py
File metadata and controls
306 lines (284 loc) · 17.9 KB
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
import json
import os
import mysql
import mysql.connector
from mysql.connector import Error
# if we're working locally, prepend /dev/ to the URL
root = "/dev/" if os.environ['IS_OFFLINE'] else "/"
#function to build the body object (it's a body builder, like Schwarzenegger!)
def the_govnuh(msg = "default message",dict = {}):
return {
"message": msg,
**dict,
}
def hello(event, context):
global root
#default response when no other routes are matched
body = the_govnuh("Go Serverless v1.0! Your function executed successfully!",{"event":event})
#open db connection
try:
conn = mysql.connector.connect(
host = os.environ.get('GEAR_CALC_HOST'),
user = os.environ.get('GEAR_CALC_USER'),
password = os.environ.get('GEAR_CALC_PASSWORD'),
auth_plugin = 'mysql_native_password', #when testing in vscode, the mysql extension only supported this older authentication scheme. Might be able to upgrade now
database = os.environ.get('GEAR_CALC_DATABASE'),
)
print("MySQL connection open")
cursor = conn.cursor()
nt_cursor = conn.cursor(named_tuple=True)
qSP = event['queryStringParameters']
if event['resource'] == root + "users/create": #if query isn't null, write new record to fc_users in the db
if qSP:
#body = the_govnuh("My bust size is " + qSP["bust"],{"input": qSP}) #original test message, replaced with db message below
user_bust = qSP["bust"]
user_waist = qSP["waist"]
sql = "INSERT INTO fc_users(user_bust,user_waist) values(%s,%s);"%(user_bust,user_waist)
cursor.execute(sql)
conn.commit()
body = the_govnuh("My bust is " + user_bust + " inches, and my waist is " + user_waist + " inches. Writing to users.")
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "gear/create":
if qSP:
gear_type = qSP["type"]
gear_brand = qSP["brand"]
gear_name = qSP["name"]
gear_size = qSP["size"]
gear_bust = qSP["bust"]
gear_waist = qSP["waist"]
sql = "INSERT INTO fc_gear(gear_type,gear_brand,gear_name,gear_size,gear_derived_bust,gear_derived_waist) values(%s,%s,%s,%s,%s,%s);"%(gear_type,gear_brand,gear_name,gear_size,gear_bust,gear_waist)
cursor.execute(sql)
conn.commit()
body = the_govnuh("New gear created! Gear type is %s, brand is %s, name is %s, size is %s, bust is %s, and waist is %s"%(gear_type,gear_brand,gear_name,gear_size,gear_bust,gear_waist))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "fr/create":
if qSP:
fr_gear_id = qSP["gear"]
fr_user_id = qSP["user"]
fr_backpro = qSP["backpro"]
fr_bust_adjust = qSP["bust"]
fr_waist_adjust = qSP["waist"]
sql = "INSERT INTO fc_fit_reports(fr_gear_id,fr_user_id,fr_backpro,fr_bust_adjust,fr_waist_adjust) values(%s,%s,%s,%s,%s);"%(fr_gear_id,fr_user_id,fr_backpro,fr_bust_adjust,fr_waist_adjust)
cursor.execute(sql)
conn.commit()
body = the_govnuh("New fit report created! Gear ID is %s, user ID is %s, bust adjust is %s, and waist adjust is %s;"%(fr_gear_id,fr_user_id,fr_bust_adjust,fr_waist_adjust))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "users/get-many": #returns all data for all user id's in a given range
if qSP:
id_min = qSP["min"]
id_max = qSP["max"]
sql = "SELECT * FROM fc_users WHERE user_id BETWEEN %s AND %s;"%(id_min,id_max)
nt_cursor.execute(sql)
res = nt_cursor.fetchall() #res should be a list of named tuples, with each column name as the index
for row in res:
if row.user_id: #checks if user id exists, to protect against user id's being out of range
user_id = row.user_id
user_bust = row.user_bust
user_waist = row.user_waist
if row.user_derived_bust:
user_derived_bust = row.user_derived_bust
if row.user_derived_waist:
user_derived_waist = row.user_derived_waist
# body[user_id] = the_govnuh("The user id is " + str(user_id) + ", the user bust is " + str(user_bust) + ", and the user waist is " + str(user_waist) + ". ")
body[user_id] = the_govnuh("For user id %s, the bust is %s, the waist is %s, the derived bust is %s, and the derived waist is %s"%(user_id,user_bust,user_waist,user_derived_bust,user_derived_waist))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "gear/get-many": #returns all data for all gear with a single given parameter (eg brand or name) #later can add size ranges
if qSP:
if qSP.get("gear") is not None:
gear_id = qSP["gear"]
sql = "SELECT * FROM fc_gear WHERE gear_id = %s;"%(gear_id) #this should only return 1 record
if qSP.get("type") is not None:
gear_type = qSP["type"]
sql = "SELECT * FROM fc_gear WHERE gear_type = %s LIMIT 100;"%(gear_type)
elif qSP.get("brand") is not None:
gear_brand = qSP["brand"]
sql = "SELECT * FROM fc_gear WHERE gear_brand = %s LIMIT 100;"%(gear_brand)
elif qSP.get("name") is not None:
gear_name = qSP["name"]
sql = "SELECT * FROM fc_gear WHERE gear_name = %s LIMIT 100;"%(gear_name)
nt_cursor.execute(sql)
res = nt_cursor.fetchall() #res should be a list of named tuples, with each column name as the index
for row in res:
if row.gear_id: #checks if gear id exists, to protect against id's being out of range
gear_id = row.gear_id
gear_type = row.gear_type
gear_brand = row.gear_brand
gear_name = row.gear_name
gear_size = row.gear_size
gear_bust = row.gear_bust
gear_waist = row.gear_waist
body[user_id] = the_govnuh("For gear id %s, the gear type is %s, brand is %s, name is %s, size is %s, derived bust is %s, and derived waist is %s."%(gear_id,gear_type,gear_brand,gear_name,gear_size,gear_bust,gear_waist))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "fr/get-many": #returns all fit reports for a given user id OR a given gear id
if qSP:
if qSP.get("gear") is not None:
fr_gear_id = qSP["gear"]
sql = "SELECT * FROM fc_fit_reports WHERE fr_gear_id = %s;"%(fr_gear_id)
nt_cursor.execute(sql)
res = nt_cursor.fetchall() #res should be a list of named tuples, with each column name as the index
for row in res:
if row.fr_id: #checks if record exists, to protect against id's being out of range
fr_id = row.fr_id
fr_user_id = row.fr_user_id
fr_backpro = row.fr_backpro
fr_bust_adjust = row.fr_bust_adjust
fr_waist_adjust = row.fr_waist_adjust
if row.gear_bust_est:
gear_bust_est = row.gear_bust_est
if row.gear_waist_est:
gear_waist_est = row.gear_waist_est
if row.user_bust_est:
user_bust_est = row.user_bust_est
if row.user_waist_est:
user_waist_est = row.user_waist_est
body[fr_id] = the_govnuh("For fit report %s, the user is %s, back protector is %s, bust adjust is %s, and waist adjust is %s. The estimated bust of the item is %s, and the estimated waist of the item is %s. The estimated bust of the user is %s, and the estimated waist of the user is %s"%(fr_id,fr_user_id,fr_backpro,fr_bust_adjust,fr_waist_adjust,gear_bust_est,gear_waist_est,user_bust_est,user_waist_est))
elif qSP.get("user") is not None:
fr_user_id = qSP["user"]
sql = "SELECT * FROM fc_fit_reports WHERE fr_user_id = %s;"%(fr_user_id)
nt_cursor.execute(sql)
res = nt_cursor.fetchall() #res should be a list of named tuples, with each column name as the index
for row in res:
if row.fr_id: #checks if record exists, to protect against id's being out of range
fr_id = row.fr_id
fr_gear_id = row.fr_gear_id
fr_backpro = row.fr_backpro
fr_bust_adjust = row.fr_bust_adjust
fr_waist_adjust = row.fr_waist_adjust
if row.gear_bust_est:
gear_bust_est = row.gear_bust_est
if row.gear_waist_est:
gear_waist_est = row.gear_waist_est
if row.user_bust_est:
user_bust_est = row.user_bust_est
if row.user_waist_est:
user_waist_est = row.user_waist_est
body[fr_id] = the_govnuh("For fit report %s, the gear id is %s, back protector is %s, bust adjust is %s, and waist adjust is %s. The estimated bust of the item is %s, and the estimated waist of the item is %s. The estimated bust of the user is %s, and the estimated waist of the user is %s"%(fr_id,fr_gear_id,fr_backpro,fr_bust_adjust,fr_waist_adjust,gear_bust_est,gear_waist_est,user_bust_est,user_waist_est))
else:
body = the_govnuh("Please specify whether you're looking by user or gear id. ")
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "users/get-one": #returns all data for a given user id
if qSP:
user = qSP["id"]
sql = "SELECT * FROM fc_users WHERE user_id = %s;"%(user)
nt_cursor.execute(sql)
res = nt_cursor.fetchone() #res is a list of named tuples, with each column name as the index
user_id = res.user_id
user_bust = res.user_bust
user_waist = res.user_waist
body = the_govnuh("The user id is " + str(user_id) + ", the user bust is " + str(user_bust) + ", and the user waist is " + str(user_waist) + "./n")
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "gear/get-one": #returns all data for a given gear id
if qSP:
gear_id = qSP["id"]
sql = "SELECT * FROM fc_gear WHERE gear_id = %s;"%(gear_id)
nt_cursor.execute(sql)
res = nt_cursor.fetchone() #res is a list of named tuples, with each column name as the index
gear_type = res.gear_type
gear_brand = res.gear_brand
gear_name = res.gear_name
gear_size = res.gear_size
gear_derived_bust = res.gear_derived_bust
gear_derived_waist = res.gear_derived_waist
body = the_govnuh("For gear id %s, the type is %s, the brand is %s, the name is %s, the size is %s, the derived bust is %s, and the derived waist is %s"%(gear_id,gear_type,gear_brand,gear_name,gear_size,gear_derived_bust,gear_derived_waist))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "fr/get-one": #returns all data for a given fit report id
if qSP:
fr_id = qSP["id"]
sql = "SELECT * FROM fc_fit_reports WHERE fr_id = %s;"%(fr_id)
nt_cursor.execute(sql)
res = nt_cursor.fetchone() #res is a list of named tuples, with each column name as the index
if res.fr_id:
fr_gear_id = res.fr_gear_id
fr_user_id = res.fr_user_id
fr_backpro = res.fr_backpro
fr_bust_adjust = res.fr_bust_adjust
fr_waist_adjust = res.fr_waist_adjust
if row.gear_bust_est:
gear_bust_est = row.gear_bust_est
if row.gear_waist_est:
gear_waist_est = row.gear_waist_est
if row.user_bust_est:
user_bust_est = row.user_bust_est
if row.user_waist_est:
user_waist_est = row.user_waist_est
body = the_govnuh("For fit report %s, the gear id is %s, back protector is %s, bust adjust is %s, and waist adjust is %s. The estimated bust of the item is %s, and the estimated waist of the item is %s. The estimated bust of the user is %s, and the estimated waist of the user is %s"%(fr_id,fr_gear_id,fr_backpro,fr_bust_adjust,fr_waist_adjust,gear_bust_est,gear_waist_est,user_bust_est,user_waist_est))
else:
body = the_govnuh("Fit report ID %s does not exist. "%(fr_id))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "users/update": #updates any given values for a (mandatory) given user id
if qSP:
user_id = qSP["id"]
if (qSP.get("bust") is not None and qSP.get("waist") is not None):
user_bust = qSP["bust"]
user_waist = qSP["waist"]
sql = "UPDATE fc_users SET user_bust = %s, user_waist = %s WHERE user_id = %s;"%(user_bust,user_waist,user_id)
body = the_govnuh("The user id is " + str(user_id) + ", the new user bust is " + str(user_bust) + ", and the new user waist is " + str(user_waist) + ". ")
cursor.execute(sql)
conn.commit()
elif (qSP.get("bust") is not None):
user_bust = qSP["bust"]
sql = "UPDATE fc_users SET user_bust = %s WHERE user_id = %s;"%(user_bust,user_id)
body = the_govnuh("The user id is " + str(user_id) + " and the new user bust is " + str(user_bust) + ". ")
cursor.execute(sql)
conn.commit()
elif (qSP.get("waist") is not None):
user_waist = qSP["waist"]
sql = "UPDATE fc_users SET user_waist = %s WHERE user_id = %s;"%(user_waist,user_id)
body = the_govnuh("The user id is " + str(user_id) + " and the new user waist is " + str(user_waist) + ". ")
cursor.execute(sql)
conn.commit()
else:
body = the_govnuh("Please specify a measurement to update for user " + str(user_id) + ". ")
# body = the_govnuh("The user id is " + str(user_id) + ", the new user bust is " + str(user_bust) + ", and the new user waist is " + str(user_waist) + ". ")
# probably need to parse the query contents in order to make the below work. EG, if query contains bust, user_bust = qSP["bust"]
# user_id = qSP["id"]
# def user_updater(user_id, **update):
# #normal sql update: "UPDATE table SET col1 = val 1, col2 = val2... WHERE col9 = val9;"
# query = "UPDATE fc_users SET "
# for key, value in update.items():
# if value > 0:
# query += " %s = %s,"%(key,value)
# query -= "," #strip last comma from query
# query += " WHERE user_id = %s;"%(user_id)
# return query
# fields = {"user_bust":qSP["bust"],"user_waist":qSP["waist"],"user_derived_bust":qSP["der-bust"],"user_derived_waist":qSP["der-waist"]}
# updates = {}
# for key,value in fields.items():
# if value is not None:
# updates[key] = value
# sql = user_updater(user_id, updates)
# cursor.execute(sql)
# conn.commit()
# body = the_govnuh("The following values were updated for User %s: %s"%(user_id,updates))
else:
body = the_govnuh("Missing qSP")
if event['resource'] == root + "users/delete": #deletes a single user identified by a given user id
if qSP:
user_id = qSP["id"]
sql = "DELETE FROM fc_users WHERE user_id = %s LIMIT 1;"%(user_id) #LIMIT 1 just to limit the damage if SQL accepts a wildcard for user id
cursor.execute(sql)
conn.commit()
body = the_govnuh("User " + str(user_id) + " has been deleted from the database.")
else:
body = the_govnuh("Missing qSP")
response = {
"statusCode": 200,
"body": json.dumps(body)
}
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
finally:
if (conn.is_connected()):
cursor.close()
conn.close()
print("MySQL connection is closed")
return response