-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.py
356 lines (290 loc) · 14 KB
/
handler.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
"""Backend Handler Module
@created: Goh Kian Wei (Brandon), Kevin Koh, Shannon Lim, Tony Tran, Samantha Wee, Wong Si Hui
@code_adapted_from: Chris Boesch, Daniel Tsou
"""
"""
Note to self: json.loads = json string to objects. json.dumps is object to json string.
"""
import datetime
import os
import urllib
import urllib2
from webapp2_extras import auth
from webapp2_extras import sessions
from webapp2_extras.auth import InvalidAuthIdError
from webapp2_extras.auth import InvalidPasswordError
import webapp2
from google.appengine.api import memcache
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import json
#Class for all backend URI handlers
class ActionHandler(webapp2.RequestHandler):
#Methods for retrieving authentication
@webapp2.cached_property
def auth(self):
return auth.get_auth()
@webapp2.cached_property
def session_store(self):
return sessions.get_store(request=self.request)
@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
#Response wrapper for handler
def respond(self,result):
"""Returns a JSON response to the client.
"""
callback = self.request.get('callback')
self.response.headers['Content-Type'] = 'application/json'
#self.response.headers['Content-Type'] = '%s; charset=%s' % (config.CONTENT_TYPE, config.CHARSET)
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD'
self.response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, X-Requested-With'
self.response.headers['Access-Control-Allow-Credentials'] = 'True'
#Add a handler to automatically convert datetimes to ISO 8601 strings.
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
if callback:
content = str(callback) + '(' + json.dumps(result,default=dthandler) + ')'
return self.response.out.write(content)
return self.response.out.write(json.dumps(result,default=dthandler))
#Handler for adding a Sketch
def add_sketch(self): #/add/sketch
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.add(self.request.body, userid=userid)
return self.respond(result)
#Handler for deleting a Sketch
def delete_sketch(self, model_id): #/delete/sketch/<model_id>
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.remove(model_id, userid)
return self.respond(result)
#Handler for listing Sketches by User
def user_sketch(self): #/list/sketch/user
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.get_entities_by_id(self.request.body,userid=userid)
return self.respond(result)
#Handler for listing Sketches by Group
def group_sketch(self): #/list/sketch/group
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.get_entity_by_group(self.request.body,userid=userid)
return self.respond(result)
#Handler for searching for Sketches
def list_sketch(self): #/list/sketch
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.get_entities(self.request.body, userid=userid)
return self.respond(result)
#Handler for viewing a particular Sketch
def view_sketch(self): #/get/sketch/view
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.get_entity_by_versioning(self.request.body, "View", userid=userid)
return self.respond(result)
#Handler for editing a particular Sketch
def edit_sketch(self): #/get/sketch/edit
auser = self.auth.get_user_by_session()
userid = 0
if auser:
userid = auser['user_id']
result = Sketch.get_entity_by_versioning(self.request.body, "Edit", userid=userid)
return self.respond(result)
#Handler for creating a Group
def add_group(self): #/add/group
if self.request.method=="POST":
result = Group.add(self.request.body)
return self.respond(result)
else:
data = self.request.get("obj")
if data:
result = Group.add(data)
return self.respond(result)
#Handler for getting a Group
def get_group(self): #/get/group
result = Group.get_entity(self.request.body)
return self.respond(result)
#Handler for getting Groups for a particular User
def user_group(self): #/list/group
offset = 0
new_offset = self.request.get("offset")
if new_offset:
offset = int(new_offset)
result = UserGroupMgmt.get_entities(self.request.body)
return self.respond(result)
#Handler for getting Users in a particular Group
def check_user_group(self): #/listuser/group
result = {'status':'error',
'message':''}
entities = UserGroupMgmt.check_users(self.request.body)
result = {'status':'success',
'method':'check_user_group',
'en_type': 'User',
'entities': entities}
return self.respond(result)
#Handler for inviting a User to a Group
def add_user_group(self): #/adduser/group
auser = self.auth.get_user_by_session()
result = {'status':'error',
'message':'There was an error in sending the invite.',
'submessage':'Please try again later.'}
if auser:
userid = auser['user_id']
result = UserGroupMgmt.add(self.request.body, userid)
return self.respond(result)
#Handler for removing a User/quitting from a Group
def remove_user_group(self): #/removeuser/group
auser = self.auth.get_user_by_session()
if auser:
userid = auser['user_id']
result = UserGroupMgmt.remove_user(self.request.body, userid)
return self.respond(result)
#Handler for passing ownership of a Group to another User
def pass_founder_group(self): #/passfounder/group
auser = self.auth.get_user_by_session()
if auser:
userid = auser['user_id']
result = UserGroupMgmt.pass_founder(self.request.body, userid)
return self.respond(result)
#Handler for accepting/rejecting a Group invite
def accept_reject_group(self): #/acceptreject/group
auser = self.auth.get_user_by_session()
result = {'status':'error',
'message':'Not authenticated.'}
if auser:
userid = auser['user_id']
result = UserGroupMgmt.accept_reject(self.request.body, userid)
return self.respond(result)
#Handler for deleting a Group
def delete_group(self): #/delete/group
auser = self.auth.get_user_by_session()
result = {'status':'error',
'message':'Not authenticated.'}
if auser:
userid = auser['user_id']
result = Group.remove(self.request.body, userid)
return self.respond(result)
#Handler for getting latest Notifications
def get_notification(self, limit): #/get/notification/<limit>
auser = self.auth.get_user_by_session()
result = {}
if auser:
userid = auser['user_id']
result = Notification.get_entities(userid, limit)
return self.respond(result)
#Handler for getting all Notifications
def get_all_notification(self): #/get/notification
auser = self.auth.get_user_by_session()
result = {}
if auser:
userid = auser['user_id']
result = Notification.get_entities(userid)
return self.respond(result)
#Handler for adding a Comment to a Sketch
def add_comment(self): #/add/comment
auser = self.auth.get_user_by_session()
result = {'status':'error',
'message':'There was an error in adding the comment.',
'submessage':'Please try again later.'}
if auser:
userid = auser['user_id']
if self.request.method=="POST":
result = Comment.add(self.request.body, userid)
else:
data = self.request.get("obj")
if data:
result = Comment.add(data, userid)
return self.respond(result)
#Handler for getting Comments for a Sketch
def get_comment(self): #/get/comment
auser = self.auth.get_user_by_session()
if auser:
userid = auser['user_id']
result = Comment.get_entities_by_id_handler_wrapper(self.request.body)
return self.respond(result)
#Handler for Liking/Unliking a Sketch
def toggle_like(self): #/toggle/like
auser = self.auth.get_user_by_session()
result = {'status':'error',
'message':'There was an error in liking/unliking the sketch.',
'submessage':'Please try again later.'}
if auser:
userid = auser['user_id']
if self.request.method=="POST":
result = Like.like_unlike(self.request.body, userid)
else:
data = self.request.get("obj")
if data:
result = Like.like_unlike(data, userid)
return self.respond(result)
#Handler for getting Likes for a sketch
def get_like(self): #/get/like
auser = self.auth.get_user_by_session()
if auser:
userid = auser['user_id']
result = Like.get_entities_by_id_handler_wrapper(self.request.body, userid)
else:
result = Like.get_entities_by_id_handler_wrapper(self.request.body, 0)
return self.respond(result)
#Handler for getting Sketches/Users by app version
def get_versions(self): #/list/version
offset = 0
new_offset = self.request.get("offset")
if new_offset:
offset = int(new_offset)
result = AppVersionCount.retrieve_by_version()
return self.respond(result)
#Configuration and URI mapping
webapp2_config = {}
webapp2_config['webapp2_extras.sessions'] = {
'secret_key': 'n\xd99\xd4\x01Y\xea5/\xd0\x8e\x1ba\\:\x91\x10\x16\xbcTA\xe0\x87lf\xfb\x0e\xd2\xc4\x15\\\xaf\xb0\x91S\x12_\x86\t\xadZ\xae]\x96\xd0\x11\x80Ds\xd5\x86.\xbb\xd5\xcbb\xac\xc3T\xaf\x9a+\xc5',
}
application = webapp2.WSGIApplication([
webapp2.Route('/metadata', handler=ActionHandler, handler_method='metadata'),
webapp2.Route('/delete/sketch/<model_id>', handler=ActionHandler, handler_method='delete_sketch'),
webapp2.Route('/add/sketch', handler=ActionHandler, handler_method='add_sketch'), # Add Sketch
webapp2.Route('/list/sketch', handler=ActionHandler, handler_method='list_sketch'), # List/Search Sketch
webapp2.Route('/list/sketch/user', handler=ActionHandler, handler_method='user_sketch'), # List Sketch By User
webapp2.Route('/list/sketch/group', handler=ActionHandler, handler_method='group_sketch'), # List Sketch By Group
webapp2.Route('/get/sketch/view', handler=ActionHandler, handler_method='view_sketch'), # Get Sketch (View)
webapp2.Route('/get/sketch/edit', handler=ActionHandler, handler_method='edit_sketch'), # Get Sketch (Edit)
webapp2.Route('/add/group', handler=ActionHandler, handler_method='add_group'), # Add Group
webapp2.Route('/get/group', handler=ActionHandler, handler_method='get_group'), # Get Group
webapp2.Route('/list/group', handler=ActionHandler, handler_method='user_group'), #
webapp2.Route('/listuser/group', handler=ActionHandler, handler_method='check_user_group'), #
webapp2.Route('/adduser/group', handler=ActionHandler, handler_method='add_user_group'), # Invite User To Group
webapp2.Route('/removeuser/group', handler=ActionHandler, handler_method='remove_user_group'), # Expel User To Group/Leave Group
webapp2.Route('/passfounder/group', handler=ActionHandler, handler_method='pass_founder_group'), # Pass Group Founder Position
webapp2.Route('/acceptreject/group', handler=ActionHandler, handler_method='accept_reject_group'), # Accept/Reject Group Invite
webapp2.Route('/delete/group', handler=ActionHandler, handler_method='delete_group'), # Delete Group
webapp2.Route('/get/notification/<limit>', handler=ActionHandler, handler_method='get_notification'), # Get Notifications (Menu)
webapp2.Route('/get/notification', handler=ActionHandler, handler_method='get_all_notification'), # Get All Notifications
webapp2.Route('/add/comment', handler=ActionHandler, handler_method='add_comment'), # Add Comment
webapp2.Route('/get/comment', handler=ActionHandler, handler_method='get_comment'), # Get Comment For Sketch
webapp2.Route('/toggle/like', handler=ActionHandler, handler_method='toggle_like'), # Add Comment
webapp2.Route('/get/like', handler=ActionHandler, handler_method='get_like'), # Get Comment For Sketch
webapp2.Route('/list/version', handler=ActionHandler, handler_method='get_versions') # Get Versions
],
config=webapp2_config,
debug=True)
#Imports placed below to avoid circular imports
from rpx import User
from sketches import Sketch
from counters import AppVersionCount
from comments_likes import Comment, Like
from permissions_groups import Sketch_Groups, Group, UserGroupMgmt
from notifications import Notification