-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
173 lines (139 loc) · 5.54 KB
/
app.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
from flask import Flask, render_template, request, Response, json, jsonify
from modules.event import Event
import modules.auth as Auth
import routers.mongo as Database
app = Flask(__name__)
app.config['DEBUG'] = True
STATUS_KEY = 'status'
RESULT_KEY = 'result'
CITY_KEY = 'city'
CLIENT_ID_KEY = 'clientid'
USER_PASSWORD = 'password'
EVENT_ID_KEY = 'eventid'
SWITCH_KEY = 'switch'
USERID_KEY = 'uuid'
USERTAGS_KEY = 'tags'
SUCCESS_STATUS = 200
INVALID_SEARCH_STATUS = 404
INVALID_PASSWORD = 405
INVALID_QUERY = 406
INVALID_PASSWORD_MESSAGE = 'Invalid Password'
INVALID_ClIENT_MESSAGE = 'Invalid Client'
INVALID_SEARCH_MESSAGE = 'Search Error: Not Found'
def getJsonResponse(code, message):
response = jsonify(message)
response.status_code = code
return response
def searchResults(result):
if result == None:
return getJsonResponse(INVALID_SEARCH_STATUS, INVALID_SEARCH_MESSAGE)
return getJsonResponse(SUCCESS_STATUS, result)
def validateAttendant(eventid, userid, tags):
if (
eventid == None or
userid == None or
tags == None or
not isinstance(tags, list)
):
return False
return True
def getAttendantObject(userid, tags):
return {
USERID_KEY: str(userid),
USERTAGS_KEY: tags
}
# GET REQUESTS
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/events/id/<string:eventid>', methods=['GET'])
def getEventByID(eventid):
return searchResults(Database.getEventFromID(eventid))
@app.route('/events/dayof/<int:year>/<int:month>/<int:day>', methods=['GET'])
def getEventByDay(year, month, day):
return searchResults(Database.eventsFromDay(year, month, day))
@app.route('/events/weekof/<int:year>/<int:month>/<int:day>', methods=['GET'])
def getEventByWeek(year, month, day):
return searchResults(Database.eventsFromWeek(year, month, day))
@app.route('/events/range/<latitude>,<longitude>,<radius>')
def getEventByMapRadius(latitude, longitude, radius):
try:
lat = float(latitude)
lon = float(longitude)
rad = float(radius)
return searchResults(Database.eventsFromRange(lat, lon, rad))
except TypeError as err:
return getJsonResponse(INVALID_SEARCH_STATUS, 'Search Error: Wrong Format')
# GET, POST REQUESTS
@app.route('/newclient', methods=['GET', 'POST'])
def createNewClientID():
password = request.args.get(USER_PASSWORD)
result = Auth.addNewClient(password)
if result == None:
return getJsonResponse(INVALID_PASSWORD, INVALID_PASSWORD_MESSAGE)
return getJsonResponse(SUCCESS_STATUS, result)
@app.route('/toggleDatabase', methods=['GET', 'POST'])
def toggleRoute():
clientID = request.args.get(CLIENT_ID_KEY)
datasourceId = request.args.get(SWITCH_KEY)
if Auth.validateClient(clientID) == False:
return getJsonResponse(INVALID_PASSWORD, INVALID_ClIENT_MESSAGE)
elif datasourceId == None or not isinstance(datasourceId, str):
return getJsonResponse(INVALID_QUERY, 'Invalid Query Type')
elif datasourceId not in Database.DATABASE_SWITCHES:
return getJsonResponse(INVALID_QUERY, 'Invalid DB Name')
previous = Database.DATABASE_SWITCHES[datasourceId]
Database.DATABASE_SWITCHES[datasourceId] = not previous
result = str(Database.DATABASE_SWITCHES[datasourceId])
return getJsonResponse(SUCCESS_STATUS, result)
@app.route('/update', methods=['GET', 'POST'])
def updateDatabase():
clientID = request.args.get(CLIENT_ID_KEY)
city = request.args.get(CITY_KEY)
if Auth.validateClient(clientID) == False:
return getJsonResponse(INVALID_PASSWORD, INVALID_ClIENT_MESSAGE)
elif city == None:
return getJsonResponse(INVALID_QUERY, 'Invalid City Query')
Database.fillDatabase(str(city))
return getJsonResponse(SUCCESS_STATUS, 'Updated')
@app.route('/delete', methods=['GET', 'POST'])
def deleteOne():
clientID = request.args.get(CLIENT_ID_KEY)
eventId = request.args.get(EVENT_ID_KEY)
if Auth.validateClient(clientID) == False:
return getJsonResponse(INVALID_PASSWORD, INVALID_ClIENT_MESSAGE)
elif eventId == None:
return getJsonResponse(INVALID_QUERY, 'Invalid Event ID')
results = Database.safe_delete(str(eventId))
return getJsonResponse(results[STATUS_KEY], results[RESULT_KEY])
@app.route('/clean', methods=['GET', 'POST'])
def cleanDatabase():
clientID = request.args.get(CLIENT_ID_KEY)
if Auth.validateClient(clientID) == False:
return getJsonResponse(INVALID_PASSWORD, INVALID_ClIENT_MESSAGE)
result = Database.safe_clean()
return getJsonResponse(SUCCESS_STATUS, ("%d Deleted" % result))
# Passing arrays = tags[]=car&tags[]=radio
@app.route('/addAttendant', methods=['GET', 'POST'])
def addAttendant():
clientID = request.args.get(CLIENT_ID_KEY)
eventId = request.args.get(EVENT_ID_KEY)
userID = request.args.get(USERID_KEY)
tags = request.args.getlist(USERTAGS_KEY + '[]')
if validateAttendant(eventId, userID, tags) == False:
return getJsonResponse(INVALID_QUERY, 'Invalid Query')
results = Database.safe_addAttendant(str(eventId), getAttendantObject(userID, tags))
return getJsonResponse(results[STATUS_KEY], results[RESULT_KEY])
@app.route('/removeAttendant', methods=['GET', 'POST'])
def removeAttendant():
clientID = request.args.get(CLIENT_ID_KEY)
eventId = request.args.get(EVENT_ID_KEY)
userID = request.args.get(USERID_KEY)
tags = request.args.getlist(USERTAGS_KEY + '[]')
if validateAttendant(eventId, userID, tags) == False:
return getJsonResponse(INVALID_QUERY, 'Invalid Query')
results = Database.safe_removeAttendant(str(eventId), getAttendantObject(userID, tags))
return getJsonResponse(results[STATUS_KEY], results[RESULT_KEY])
# INIT
if __name__ == '__main__':
app.run()