-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_decorators.py
389 lines (350 loc) · 15.1 KB
/
my_decorators.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
from datetime import datetime
import json
from xml.etree.ElementTree import Comment
from flask import Response, request
from views import can_view_post
from models import Bookmark
from models import Comment
from models import Following
from models import Post
from models import LikePost
def handle_db_insert_error(endpoint_function):
def outer_function(self, *args, **kwargs):
print('handle_db_insert_error')
try:
# try to execute the query:
return endpoint_function(self, *args, **kwargs)
except:
import sys
db_message = str(sys.exc_info()[1]) # stores DB error message
print(db_message) # logs it to the console
message = 'DECORATOR! Database Insert error. Make sure your post data is valid.'
post_data = request.get_json()
# post_data['user_id'] = self.current_user.id
response_obj = {
'message': message,
'db_message': db_message,
'post_data': post_data
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return outer_function
def is_valid_int(endpoint_function):
def outer_function_with_security_checks(self):
try:
body = request.get_json()
post_id = body.get('post_id')
post_id = int(post_id)
except:
response_obj = {
'message': 'Invalid post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self)
return outer_function_with_security_checks
def secure_bookmark(endpoint_function):
def outer_function_with_security_checks(self):
print('secure_bookmark')
body = request.get_json()
post_id = body.get('post_id')
print(post_id)
print(can_view_post(post_id, self.current_user))
if can_view_post(post_id, self.current_user):
return endpoint_function(self)
else:
response_obj = {
'message': 'You don\'t have access to post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
def check_ownership_of_bookmark(endpoint_function):
def outer_function_with_security_checks(self, id):
print(id)
bookmark = Bookmark.query.get(id)
if bookmark.user_id == self.current_user.id:
return endpoint_function(self, id)
else:
response_obj = {
'message': 'You did not create bookmark id={0}'.format(id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
def valid_id_format(endpoint_function):
def outer_function_with_security_checks(self, id):
try:
bookmark_id = id
bookmark_id = int(bookmark_id)
except:
response_obj = {
'message': 'Invalid bookmark id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, id)
return outer_function_with_security_checks
def id_exists_or_not(endpoint_function):
def outer_function_with_security_checks(self, id):
bookmark = Bookmark.query.get(id)
if bool(bookmark) == False:
response_obj = {
'message': 'ID does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, id)
return outer_function_with_security_checks
########################################## COMMENTS ########################################################
def id_exists_or_not_comments(endpoint_function):
def outer_function_with_security_checks(self, id):
comment = Comment.query.get(id)
if bool(comment) == False:
response_obj = {
'message': 'Comment ID does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, id)
return outer_function_with_security_checks
def comment_valid_id_format(endpoint_function):
def outer_function_with_security_checks(self, id):
try:
comment_id = id
comment_id = int(comment_id)
except:
response_obj = {
'message': 'Invalid comment id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, id)
return outer_function_with_security_checks
def check_ownership_of_comment(endpoint_function):
def outer_function_with_security_checks(self, id):
print(id)
comment = Comment.query.get(id)
if comment.user_id == self.current_user.id:
return endpoint_function(self, id)
else:
response_obj = {
'message': 'You did not create comment id={0}'.format(id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
def is_valid_int_comments(endpoint_function):
def outer_function_with_security_checks(self):
try:
body = request.get_json()
post_id = body.get('post_id')
post_id = int(post_id)
except:
response_obj = {
'message': 'Invalid post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self)
return outer_function_with_security_checks
def handle_db_insert_error_comments(endpoint_function):
def outer_function(self, *args, **kwargs):
print('handle_db_insert_error')
try:
# try to execute the query:
return endpoint_function(self, *args, **kwargs)
except:
import sys
db_message = str(sys.exc_info()[1]) # stores DB error message
print(db_message) # logs it to the console
message = 'DECORATOR! Database Insert error. Make sure your post data is valid.'
post_data = request.get_json()
post_data['user_id'] = self.current_user.id
response_obj = {
'message': message,
'db_message': db_message,
'post_data': post_data
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function
def secure_comment(endpoint_function):
def outer_function_with_security_checks(self):
print('secure_comment')
body = request.get_json()
post_id = body.get('post_id')
print(post_id)
print(can_view_post(post_id, self.current_user))
if can_view_post(post_id, self.current_user):
return endpoint_function(self)
else:
response_obj = {
'message': 'You don\'t have access to post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
######################################### FOLLOWING ####################################################
def handle_db_insert_error_following(endpoint_function):
def outer_function(self, *args, **kwargs):
print('handle_db_insert_error')
try:
# try to execute the query:
return endpoint_function(self, *args, **kwargs)
except:
import sys
db_message = str(sys.exc_info()[1]) # stores DB error message
print(db_message) # logs it to the console
message = 'DECORATOR! Database Insert error. Make sure your post data is valid.'
post_data = request.get_json()
# post_data['user_id'] = self.current_user.id
response_obj = {
'message': message,
'db_message': db_message,
'post_data': post_data
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function
def is_valid_int_following(endpoint_function):
def outer_function_with_security_checks(self):
try:
body = request.get_json()
following_id = body.get('user_id')
following_id = int(following_id)
except:
response_obj = {
'message': 'Invalid following id={0}'.format(following_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self)
return outer_function_with_security_checks
def following_valid_id_format(endpoint_function):
def outer_function_with_security_checks(self, id):
try:
following_id = id
following_id = int(following_id)
except:
response_obj = {
'message': 'Invalid following id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, id)
return outer_function_with_security_checks
def id_exists_or_not_following(endpoint_function):
def outer_function_with_security_checks(self, id):
following = Following.query.get(id)
if bool(following) == False:
response_obj = {
'message': 'Person does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, id)
return outer_function_with_security_checks
def check_ownership_of_following(endpoint_function):
def outer_function_with_security_checks(self, id):
following = Following.query.get(id)
if following.user_id == self.current_user.id:
return endpoint_function(self, id)
else:
response_obj = {
'message': 'You did not create this following record'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
######################################### LIKE POSTS ####################################################
def valid_postid_format(endpoint_function):
def outer_function_with_security_checks(self, post_id):
try:
id_post = post_id
id_post = int(id_post)
except:
response_obj = {
'message': 'Invalid following id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, post_id)
return outer_function_with_security_checks
def post_id_exists(endpoint_function):
def outer_function_with_security_checks(self, post_id):
post = Post.query.get(post_id)
if bool(post) == False:
response_obj = {
'message': 'Post does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, post_id)
return outer_function_with_security_checks
def secure_like(endpoint_function):
def outer_function_with_security_checks(self, post_id):
if can_view_post(post_id, self.current_user):
return endpoint_function(self, post_id)
else:
response_obj = {
'message': 'You don\'t have access to post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
def like_valid_id_format(endpoint_function):
def outer_function_with_security_checks(self, post_id, id):
try:
id_post = post_id
id_post = int(id_post)
like_id = id
id = int(id)
except:
response_obj = {
'message': 'Invalid post id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, post_id, id)
return outer_function_with_security_checks
def like_post_id_exists(endpoint_function):
def outer_function_with_security_checks(self, post_id, id):
post = Post.query.get(post_id)
like = LikePost.query.get(id)
if bool(post) == False or bool(like) == False:
response_obj = {
'message': 'Post does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, post_id, id)
return outer_function_with_security_checks
def check_ownership_of_like(endpoint_function):
def outer_function_with_security_checks(self, post_id, id):
like = LikePost.query.get(id)
if like.user_id == self.current_user.id:
return endpoint_function(self, post_id, id)
else:
response_obj = {
'message': 'You did not create this like'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks
######################################### POSTS ####################################################
def valid_id_format_post(endpoint_function):
def outer_function_with_security_checks(self, id):
try:
post_id = id
post_id = int(post_id)
except:
response_obj = {
'message': 'Invalid post id format'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=400)
return endpoint_function(self, id)
return outer_function_with_security_checks
def id_exists_posts(endpoint_function):
def outer_function_with_security_checks(self, id):
post = Post.query.get(id)
if bool(post) == False:
response_obj = {
'message': 'Post does not exist'
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
else:
return endpoint_function(self, id)
return outer_function_with_security_checks
def secure_post(endpoint_function):
def outer_function_with_security_checks(self, post_id):
if can_view_post(post_id, self.current_user):
return endpoint_function(self, post_id)
else:
response_obj = {
'message': 'You don\'t have access to post_id={0}'.format(post_id)
}
return Response(json.dumps(response_obj), mimetype="application/json", status=404)
return outer_function_with_security_checks