-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
508 lines (465 loc) · 18.7 KB
/
api.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# api.py: Definitions of API methods.
##
# © 2012 Steven Casagrande ([email protected]) and
# Christopher E. Granade ([email protected]).
# This file is a part of the ThingPool Server project.
# Licensed under the AGPL version 3.
##
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
## IMPORTS #####################################################################
## GAE API ##
from google.appengine.api import users
from google.appengine.ext import db
import webapp2
## PYTHON STANDARD LIBRARY ##
import json
import datetime
## THINGPOOL MODULES ##
from dataModels import *
from security import *
## API REQUEST HANDLERS ########################################################
def as_json(obj):
# TODO: Generate error if list contents do not have __api__()
#assert hasattr(obj, '__api__'), "Cannot serialize {} to JSON.".format(obj)
if hasattr(obj,'__api__'):
return json.dumps(obj.__api__())
else:
return json.dumps([o.__api__() for o in obj])
class ServerInfoHandler(webapp2.RequestHandler):
# No security here, all should be able to get this information
def get(self):
"""
GET /info
Queries basic server information (eg: API version)
"""
pass
class UserHandler(webapp2.RequestHandler):
@require_permission('query_user')
@require_gae_login('deny')
def get(self, user_id):
"""
GET /users/{id}
Queries the user given by the ID {id}.
"""
try:
user_id = int(user_id)
q = Person.get_by_id(user_id)
self.response.write(as_json(q))
except ValueError:
self.error(400)
# Permissions checking is a little more complicated here, so we
# don't use @require_permission.
@require_gae_login('deny')
def post(self, user_id):
"""
POST /users/{id}
Modifies the user given by ID {id}.
"""
user = get_current_person()
try:
user_id = int(user_id)
target_user = Person.get_by_id(user_id)
new_permission = self.request.get('permissions')
if new_permission is not "":
new_permission = int(new_permission)
# Filter invalid permission settings
# TODO: move this filter to security.py
if (new_permission < USER_STATUS_BANNED) or (new_permission > USER_STATUS_ADMIN):
self.error(400)
return
# Manager can change those below
# TODO: Add can_change_permissions to UserModel.
if (user.permissions >= USER_STATUS_MANAGER) and (new_permission < USER_STATUS_MANAGER):
target_user.permissions = new_permission
db.put(target_user)
# Admins can add more admins. GAE admin should not have to handle system at all.
elif ( (user.permissions >= USER_STATUS_ADMIN) or users.is_current_user_admin() ) and (new_permission <= USER_STATUS_ADMIN ):
target_user.permissions = new_permission
db.put(target_user)
except ValueError:
self.error(400)
class UserListHandler(webapp2.RequestHandler):
@require_permission('query_users')
@require_gae_login('deny')
def get(self):
"""
GET /users
Returns a list of all users matching a given filter, paginated and
starting at a given index.
"""
# TODO: filter on additional query strings
users = Person.all()
if self.request.get('permissions') is not "":
try:
permissions = int(self.request.get('permissions'))
users = Person.all().filter('permissions = ', permissions)
except ValueError:
self.error(400)
self.response.write(as_json(users))
@require_permission('request_account', reason="Banned users cannot request accounts.") # Denied only for banned users.
@require_gae_login('deny')
def post(self):
"""
POST /users
Creates a new user as specified by the POST content.
"""
# Note, anyone can request an account
user = users.get_current_user()
q = Person.all().filter("user_account = ", user)
q = q.get()
if not q: # If does not currently have account, add to list
p = Person(
user_account = users.get_current_user(),
permissions = USER_STATUS_REQUESTED # Set to 'requested' status
)
p.put()
# else silently fail
class ItemListHandler(webapp2.RequestHandler):
@require_permission('new_item')
@require_gae_login('deny')
def post(self):
"""
POST /items
Creates a new item as specified by the POST content.
"""
if self.request.post('name') is not "":
item = Item(
name = self.request.post('name')
)
item.put()
else:
self.error(400)
@require_permission('query_items')
@require_gae_login('deny')
def get(self):
"""
GET /items
Returns a list of all items matching a given filter
"""
if self.request.get("category") is "":
items = Item.all()
# TODO: Find why the following code block causes the dev server to report Syntax Error
'''else:
try:
category = Category.get_by_id( int(self.request.get("category") ) )
items = Item.all().filter("category = ", category)
except ValueError:
self.error(400)'''
self.response.write(as_json(items))
class ItemHandler(webapp2.RequestHandler):
@require_permission('query_item')
@require_gae_login('deny')
def get(self, item_id):
"""
GET /items/{id}
Queries the item given by the ID {id}.
"""
try:
item_id = int(item_id)
q = Item.get_by_id(item_id)
self.response.write(as_json(q))
except ValueError:
self.error(400)
@require_permission('modify_item')
@require_gae_login('deny')
def post(self, item_id):
"""
POST /items/{id}
Modifies the item given by ID {id}.
"""
try:
item_id = int(item_id)
item = Item.get_by_id(item_id)
if item is not None:
if self.request.post('category_id') is not "":
new_category = Category.get_by_id(int(self.request.post('category_id')))
item.category = new_category
if self.request.post('name') is not "":
item.name = self.request.post('name')
if self.request.post('name2') is not "":
item.name2 = self.request.post('name2')
if self.request.post('content') is not "":
item.content = self.request.post('content')
if self.request.post('store_location') is not "":
item.store_location = self.request.post('store_location')
db.put(item)
else:
self.error(400)
except ValueError:
self.error(400)
class CategoryListHandler(webapp2.RequestHandler):
@require_permission('query_categories')
@require_gae_login('deny')
def get(self):
"""
GET /categories
Queries list of categories, filtered by some parent
If no parent specified, assume root categories
"""
if self.request.get('parent') is "":
categories = Category.all().filter("category_parent = ", None)
else:
try:
parent_id = int(self.request.get('parent'))
category_parent = Category.get_by_id(parent_id)
categories = Category.all().filter("category_parent = ", category_parent)
except ValueError:
self.error(400)
self.response.write(as_json(categories))
class CategoryHandler(webapp2.RequestHandler):
@require_permission('query_category')
@require_gae_login('deny')
def get(self, category_id):
"""
GET /categories/{id}
Queries the category by the ID {id}
"""
try:
category_id = int(category_id)
q = Category.get_by_id(category_id)
self.response.write(as_json(q))
except ValueError:
self.error(400)
@require_permission('modify_category')
@require_gae_login('deny')
def post(self, category_id):
"""
POST /categories/{id}
Modifies the category by the ID {id}
"""
try:
category_id = int(category_id)
q = Category.get_by_id(category_id)
new_parent = self.request.post('parent_id')
if new_parent is not "":
new_parent = Category.get_by_id(int(new_parent))
q.category_parent = new_parent
new_name = self.request.post('name')
if new_name is not "":
q.name = new_name
db.put(q)
except ValueError:
self.error(400)
class CheckoutListHandler(webapp2.RequestHandler):
@require_permission('new_checkout')
@require_gae_login('deny')
def post(self):
"""
POST /checkout
Creates a new checkout transaction
"""
try:
item = Item.get_by_id(int(self.request.post('item_id')))
user = Person.get_person()
if user.permissions is USER_STATUS_KIOSK: # If kiosk account, use provided keycard
keycard_id = self.request.post('keycard')
if keycard_id is not "":
user = Person.get_person(keycard=keycard_id)
else:
self.error(400)
if item.is_checked_out(): # Clear previous checkout transaction
prev_checkout = CheckoutTransaction.all().filter('item =', item).filter('checkin_date =', None)
prev_checkout = prev_checkout.get()
prev_checkout.checkin_date = datetime.datetime.now()
db.put(prev_checkout)
if item.is_requested(): # If item has an outstanding request transaction for this user
request = RequestTransaction.all().filter('item =', self).filter('resolved_date =', None).filter('requestor = ', user)
request = request.get()
request.resolved_date = datetime.datetime.now()
db.put(request)
c = CheckoutTransaction(
item=item,
holder = user
)
c.put()
except ValueError:
self.error(400)
@require_permission('query_checkout_transactions')
@require_gae_login('deny')
def get(self):
"""
GET /checkout
Query checkout transaction history for a user or item
"""
# TODO: Consider limiting number of transactions returned
checkouts = CheckoutTransaction.all()
if self.request.get("item_id") is not "":
try:
item = Item.get_by_id(int(self.requst.get('item_id')))
checkouts = checkouts.filter("item = ", item)
except ValueError:
self.error(400)
if self.request.get("user_id") is not "":
try:
user = Person.get_by_id(int(self.requst.get('user_id')))
checkouts = checkouts.filter("holder = ", user)
except ValueError:
self.error(400)
year_start = self.request.get('year_start')
month_start = self.request.get('month_start')
day_start = self.request.get('day_start')
if (year_start is not "") and (month_start is not "") and (day_start is not ""):
try:
year_start = int(year_start)
month_start = int(month_start)
day_start = int(day_start)
start_date = datetime.datetime(year_start,month_start,day_start)
checkouts = checkouts.filter('checkout_date >= ', start_date)
except ValueError:
self.error(400)
year_end = self.request.get('year_end')
month_end = self.request.get('month_end')
day_end = self.request.get('day_end')
if (year_end is not "") and (month_end is not "") and (day_end is not ""):
try:
year_end = int(year_end)
month_end = int(month_end)
day_end = int(day_end)
end_date = datetime.datetime(year_end,month_end,day_end)
checkouts = checkouts.filter('checkout_date <= ', end_date)
except ValueError:
self.error(400)
self.response.write(as_json(checkouts))
class CheckoutHandler(webapp2.RequestHandler):
@require_permission('query_checkout')
@require_gae_login('deny')
def get(self, checkout_id):
"""
GET /checkout/{id}
Query specific checkout transaction details
"""
try:
checkout_id = int(checkout_id)
q = CheckoutTransaction.get_by_id(category_id)
self.response.write(as_json(q))
except ValueError:
self.error(400)
@require_permission('modify_checkout')
@require_gae_login('deny')
def post(self, checkout_id):
"""
POST /checkout/{id}
Modify checkout transaction to include a checkin date
"""
try:
transaction = CheckoutTransaction.get_by_id(int(checkout_id))
user = Person.get_person()
if (transaction.holder is user) or (user.permissions >= USER_STATUS_MANAGER):
transaction.checkin_date = datetime.datetime.now()
db.put(transaction)
except ValueError:
self.error(400)
class RequestListHandler(webapp2.RequestHandler):
@require_permission('new_request')
@require_gae_login('deny')
def post(self):
"""
POST /request
Add a new request transaction
"""
try:
item = Item.get_by_id(int(self.request.post('item_id')))
user = Person.get_person()
# Check if current user does not already have outstanding request on item
if not RequestTransaction.all().filter("item = ", item).filter("requestor = ",user).filter("resolved_date = ", None):
c = RequestTransaction(
item=item,
requestor = user
)
c.put()
# else silently fail
except ValueError:
self.error(400)
@require_permission('query_request_transactions')
@require_gae_login('deny')
def get(self):
"""
GET /request
Query request transaction history for a user or item
"""
# TODO: Consider limiting number of transactions returned
# also filter on current outstanding requests
requests = RequestTransaction.all()
if self.request.get("item_id") is not "":
try:
item = Item.get_by_id(int(self.requst.get('item_id')))
requests = requests.filter("item = ", item)
except ValueError:
self.error(400)
if self.request.get("user_id") is not "":
try:
user = Person.get_by_id(int(self.requst.get('user_id')))
requests = requests.filter("requestor = ", user)
except ValueError:
self.error(400)
year_start = self.request.get('year_start')
month_start = self.request.get('month_start')
day_start = self.request.get('day_start')
if (year_start is not "") and (month_start is not "") and (day_start is not ""):
try:
year_start = int(year_start)
month_start = int(month_start)
day_start = int(day_start)
start_date = datetime.datetime(year_start,month_start,day_start)
requests = requests.filter('checkout_date >= ', start_date)
except ValueError:
self.error(400)
year_end = self.request.get('year_end')
month_end = self.request.get('month_end')
day_end = self.request.get('day_end')
if (year_end is not "") and (month_end is not "") and (day_end is not ""):
try:
year_end = int(year_end)
month_end = int(month_end)
day_end = int(day_end)
end_date = datetime.datetime(year_end,month_end,day_end)
requests = requests.filter('checkout_date <= ', end_date)
except ValueError:
self.error(400)
self.response.write(as_json(requests))
class RequestHandler(webapp2.RequestHandler):
@require_permission('query_request')
@require_gae_login('deny')
def get(self, request_id):
"""
GET /request/{id}
Query specific request transaction details
"""
try:
request_id = int(request_id)
q = RequestTransaction.get_by_id(category_id)
self.response.write(as_json(q))
except ValueError:
self.error(400)
@require_permission('modify_request')
@require_gae_login('deny')
def post(self, request_id):
"""
POST /request/{id}
Modify request transaction to include a resolution date
"""
# TODO: A request should only be cleared by the request-owner
#
# Do we need this? These are automatically resolved when the request-owner
# checks the item out
try:
transaction = RequestTransaction.get_by_id(int(request_id))
transaction.resolved_date = datetime.datetime.now()
db.put(transaction)
except ValueError:
self.error(400)