-
Notifications
You must be signed in to change notification settings - Fork 0
/
fits.py
431 lines (377 loc) · 12.4 KB
/
fits.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
from flask import Flask, request, make_response, json, url_for, abort
from fitsdb import Db # See db.py
from utils import makeId, getHash
import random
app = Flask(__name__)
db = Db()
app.debug = True # Comment out when not testing
app.url_map.strict_slashes = False # Allows for a trailing slash on routes
#### ERROR HANDLERS
@app.errorhandler(500)
def server_error(e):
return make_json_response({ 'error': 'unexpected server error' }, 500)
@app.errorhandler(404)
def not_found(e):
return make_json_response({ 'error': e.description }, 404)
@app.errorhandler(403)
def forbidden(e):
return make_json_response({ 'error': e.description }, 403)
@app.errorhandler(400)
def client_error(e):
return make_json_response({ 'error': e.description }, 400)
## Helper method for creating JSON responses
def make_json_response(content, response = 200, headers = {}):
headers['Content-Type'] = 'application/json'
return make_response(json.dumps(content), response, headers)
#### MAIN ROUTES
#### User Table Paths
@app.route('/user', methods = ['GET'])
def user_list():
users = db.getUsers()
return make_json_response({
"users": [
{
"link": url_for('find_user', username = user.username) ,
"username" : user.username,
"first" : user.first,
"last" : user.last}
for user in users
]
})
@app.route('/user/<username>', methods = ['GET'])
def find_user(username):
user = findUser(username)
password = getPasswordFromQuery()
checkPassword(user, password)
return make_json_response({
"username" : user.username,
"link" : url_for('find_user', username = user.username),
"first" : user.first,
"last" : user.last,
"issues" : [
{
"id" : issue.id,
"link" : url_for('find_issue', id = issue.id),
"description" : issue.description,
"location" : issue.location}
for issue in user.issues]
})
@app.route('/user/<username>', methods = ['PUT'])
def create_user_with_username(username):
user = checkUserExsists(username)
password = getPasswordFromContents()
first_name = getFirstFromContents()
last_name = getLastFromContents()
db.addUser(username, first_name, last_name, getHash(password))
headers = {"Location" : url_for('find_user', username = username)}
return make_json_response({ 'ok': 'user created' }, 201, headers)
@app.route('/user/<username>', methods = ['DELETE'])
def user_delete(username):
user = findUser(username)
password = getPasswordFromQuery()
checkPassword(user, password)
db.deleteUser(user)
db.commit()
return make_json_response({'ok' : 'user deleted'}, 204)
#### Type Table Paths
@app.route('/type', methods = ['GET'])
def type_list():
types = db.getTypes()
return make_json_response({
"types": [
{
"link": url_for('find_type', id = type.id) ,
"id" : type.id,
"name" : type.name}
for type in types
]
})
@app.route('/type/<id>', methods = ['GET'])
def find_type(id):
type = findType(id)
return make_json_response({
"id" : type.id,
"link" : url_for('find_type', id = type.id),
"name" : type.name,
"issues" : [
{
"id" : issue.id,
"link" : url_for('find_issue', id = issue.id),
"description" : issue.description,
"location" : issue.location}
for issue in type.issues]
})
@app.route('/type/<id>', methods = ['PUT'])
def create_type_with_id(id):
type = checkTypeExsists(id)
name = getNameFromContents()
db.addType(type, name)
headers = {"Location" : url_for('find_type', id = id)}
return make_json_response({ 'ok': 'type created' }, 201, headers)
@app.route('/type', methods = ['POST'])
def create_type():
id = generateID("type")
return create_type_with_id(id)
@app.route('/type/<id>', methods = ['DELETE'])
def type_delete(id):
type = findType(id)
db.deleteType(type)
db.commit()
return make_json_response({'ok' : 'type deleted'}, 204)
#### Location Table Paths
@app.route('/location', methods = ['GET'])
def location_list():
locations = db.getLocations()
return make_json_response({
"locations": [
{
"link": url_for('find_location', id = location.id) ,
"id" : location.id,
"name" : location.name}
for location in locations
]
})
@app.route('/location/<id>', methods = ['GET'])
def find_location(id):
location = findLocation(id)
return make_json_response({
"id" : location.id,
"link" : url_for('find_location', id = location.id),
"name" : location.name,
"issues" : [
{
"id" : issue.id,
"link" : url_for('find_issue', id = issue.id),
"description" : issue.description,
"location" : issue.location}
for issue in location.issues]
})
@app.route('/location/<id>', methods = ['PUT'])
def create_location_with_id(id):
location = checkLocationExsists(id)
name = getNameFromContents()
db.addLocation(location, name)
headers = {"Location" : url_for('find_location', id = id)}
return make_json_response({ 'ok': 'location created' }, 201, headers)
@app.route('/location', methods = ['POST'])
def create_location():
id = generateID("location")
return create_location_with_id(id)
@app.route('/location/<id>', methods = ['DELETE'])
def location_delete(id):
location = findLocation(id)
db.deleteLocation(location)
db.commit()
return make_json_response({'ok' : 'location deleted'}, 204)
#### Issue Table Paths
@app.route('/issue', methods = ['GET'])
def issue_list():
issues = db.getIssues()
return make_json_response({
"issues": [
{
"link": url_for('find_issue', id = issue.id) ,
"id" : issue.id,
"userId" : issue.userId,
"type" : issue.type,
"description" : issue.description,
"location" : issue.location}
for issue in issues
]
})
@app.route('/issue/<id>', methods = ['GET'])
def find_issue(id):
issue = findIssue(id)
return make_json_response({
"id" : issue.id,
"userId" : issue.userId,
"link" : url_for('find_issue', id = issue.id),
"type" : issue.type,
"description" : issue.description,
"location" : issue.location
})
@app.route('/issue/<id>', methods = ['PUT'])
def create_issue_with_id(id):
issue = checkIssueExsists(id)
userID = getUsernameFromContents()
type = getTypeFromContents()
description = getDescriptionFromContents()
location = getLocationFromContents()
db.addIssue(id, userID, type, description, location)
headers = {"Location" : url_for('find_issue', id = id)}
return make_json_response({ 'ok': 'issue created' }, 201, headers)
@app.route('/issue', methods = ['POST'])
def create_issue():
id = generateID("issue")
return create_issue_with_id(id)
@app.route('/issue/<id>', methods = ['DELETE'])
def issue_delete(id):
issue = findIssue(id)
db.deleteIssue(issue)
db.commit()
return make_json_response({'ok' : 'issue deleted'}, 204)
#Path Functions to Help Search For Issues Based on Location, User, and Type
@app.route('/issue/user/<username>', methods = ['GET'])
def user_issues(username):
user = findUser(username)
return make_json_response({
"issues": [
{
"link": url_for('find_issue', id = issue.id) ,
"id" : issue.id,
"userId" : issue.userId,
"type" : issue.type,
"description" : issue.description,
"location" : issue.location}
for issue in user.issues
]
})
@app.route('/issue/type/<name>', methods = ['GET'])
def type_issues(name):
type = findTypeName(name)
return make_json_response({
"issues": [
{
"link": url_for('find_issue', id = issue.id) ,
"id" : issue.id,
"userId" : issue.userId,
"type" : issue.type,
"description" : issue.description,
"location" : issue.location}
for issue in type.issues
]
})
@app.route('/issue/location/<name>', methods = ['GET'])
def location_issues(name):
location = findLocationName(name)
return make_json_response({
"issues": [
{
"link": url_for('find_issue', id = issue.id) ,
"id" : issue.id,
"userId" : issue.userId,
"type" : issue.type,
"description" : issue.description,
"location" : issue.location}
for issue in location.issues
]
})
#Helper functions
def findUser(username):
user = db.getUser(username)
if user is None:
abort(404, "username not found in the database")
return user
def findType(id):
type = db.getType(id)
if type is None:
abort(404, "typeId not found")
return type
def findTypeName(name):
type = db.getTypeName(name)
if type is None:
abort(404, "There is no type with the given name")
return type
def findLocation(id):
location = db.getLocation(id)
if location is None:
abort(404, "locationId not found")
return location
def findLocationName(name):
location = db.getLocationName(name)
if location is None:
abort(404, "There is no location with the given name")
return location
def findIssue(id):
issue = db.getIssue(id)
if issue is None:
abort(404, "issueId not found")
return issue
#Helper funciton used to see if a user already exsist when trying to make a new user
def checkUserExsists(username):
user = db.getUser(username)
if user is not None:
abort(403, "There is already a user using this username.")
return user
def checkTypeExsists(id):
type = db.getType(id)
if type is not None:
abort(403, "There is already a type using this id.")
return type
def checkLocationExsists(id):
location = db.getLocation(id)
if location is not None:
abort(403, "There is already a location using this id.")
return location
def checkIssueExsists(id):
issue = db.getIssue(id)
if issue is not None:
abort(403, "There is already a issue using this id.")
return issue
##Helper functions used for user verification
def getPasswordFromQuery():
if "password" not in request.args:
abort(403, 'must provide a password parameter')
return request.args["password"]
#Helper functions to get information from the json object passed through the request
def getPasswordFromContents():
contents = request.get_json()
if contents is None or "password" not in contents:
abort(403, 'must provide a password field')
return contents["password"]
def getFirstFromContents():
contents = request.get_json()
if "first" not in contents:
abort(403, 'must provide a first field')
return contents['first']
def getLastFromContents():
contents = request.get_json()
if "last" not in contents:
abort(403, 'must provide a last field')
return contents['last']
def getNameFromContents():
contents = request.get_json()
if contents is None or "name" not in contents:
abort(403, 'must provide a name field')
return contents['name']
def getUsernameFromContents():
contents = request.get_json()
if contents is None or "username" not in contents:
abort(403, 'must provide a username field')
return contents['username']
def getTypeFromContents():
contents = request.get_json()
if contents is None or "type" not in contents:
abort(403, 'must provide a type field')
return contents['type']
def getDescriptionFromContents():
contents = request.get_json()
if contents is None or "description" not in contents:
return None
return contents['description']
def getLocationFromContents():
contents = request.get_json()
if contents is None or "location" not in contents:
return None
return contents['location']
#Helper function that checks if a given password is correct
def checkPassword(user, password):
hash = getHash(password)
if hash != user.password:
abort(403, "Incorrect password.")
#Helper Function to Generate a random id
def generateID(table):
id = random.randint(1,10000)
if table == "type":
while db.getType(id) is not None:
id = random.randint(1,10000)
if table == "location":
while db.getLocation(id) is not None:
id = random.randint(1,10000)
if table == "issue":
while db.getIssue(id) is not None:
id = random.randint(1,10000)
return id
# Starts the application
if __name__ == "__main__":
app.run()