This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
293 lines (211 loc) · 7.65 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
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
from flask import Flask, jsonify, request, redirect
from pymongo import MongoClient
from datetime import datetime
from verification import verify_user, verify_api_key, verify_admin
from utils import generate_check_in_code, toggle_active_delete
import config as con
import os
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
client = MongoClient(con.MONGO_URI)
db = client.chicken
@app.route('/')
def index():
return redirect('/users/scores')
@app.route('/checkin', methods=['POST'])
@verify_api_key
def check_in():
check_in_code = request.get_json().get('check_in_code')
slack_id = request.headers.get('slack_id')
username = request.get_json().get('username')
if username is None:
return jsonify(con.MISSING_FIELDS), 400
# First verify the user is valid
if not verify_user(slack_id):
return jsonify(con.SLACK_ID_NOT_FOUND), 404
# Then verify the event exists and is active
events = db.events
event = events.find_one({'check_in_code': check_in_code})
if event is None or not event.get('active'):
return jsonify(con.WRONG_CHECK_IN_CODE), 403
# Then query for the user
users = db.users
user = users.find_one({'slack_id': slack_id})
if user is None:
user = {
'username': username,
'slack_id': slack_id,
'events': [],
'score': 0
}
db.users.insert_one(user)
# If they exist, add this check-in code
if check_in_code not in user.get('events'):
new_score = user.get('score') + event.get('weight')
users.update_one({'slack_id': slack_id}, {
'$push': {'events': check_in_code}})
users.update_one({'slack_id': slack_id}, {
'$set': {'score': new_score}})
# Then update the event with the user's slack_id
if slack_id not in event.get('attendees'):
events.update_one({'check_in_code': check_in_code}, {
'$push': {'attendees': slack_id}})
return jsonify(con.SUCCESS), 200
@app.route('/event/new', methods=['POST'])
@verify_api_key
@verify_admin
def start_checkin():
json = request.get_json()
name = json.get('name')
timestamp = json.get('timestamp')
weight = json.get('weight')
slack_id = request.headers.get('slack_id')
if timestamp is None:
timestamp = datetime.now()
if weight is None:
weight = 1.0
if name is None or slack_id is None:
return jsonify(con.MISSING_FIELDS), 400
check_in_code = generate_check_in_code()
events = db.events
while True:
event_in_db = events.find_one({'check_in_code': check_in_code})
if event_in_db is not None:
check_in_code = generate_check_in_code()
else:
break
# Create the event and insert it into the database
event = {
'name': name,
'timestamp': timestamp,
'weight': weight,
'slack_id': slack_id,
'check_in_code': check_in_code,
'active': True,
'deleted': False,
'attendees': []
}
result = events.insert_one(event)
return jsonify({
'event_id': str(result.inserted_id),
'check_in_code': check_in_code
}), 200
@app.route('/event/list', methods=['GET'])
@verify_api_key
@verify_admin
def list_all_events():
events = db.events
all_events = []
# Serialize the object id
for event in events.find({'deleted': False}):
event['_id'] = str(event['_id'])
all_events.append(event)
return jsonify({'events': all_events}), 200
@app.route('/event/list/active', methods=['GET'])
@verify_api_key
@verify_admin
def list_active_events():
events = db.events
all_events = []
# Serialize the object id
for event in events.find({'active': True}):
event['_id'] = str(event['_id'])
all_events.append(event)
return jsonify({'events': all_events}), 200
@app.route('/event/close', methods=['PUT'])
@verify_api_key
@verify_admin
def close_event():
check_in_code = request.get_json().get('check_in_code')
event = db.events.find_one({'check_in_code': check_in_code})
if event is None or event.get('deleted') or not event.get('active'):
return jsonify(con.WRONG_CHECK_IN_CODE), 403
toggle_active_delete(db, check_in_code, active=False)
return jsonify(con.SUCCESS), 200
@app.route('/event/reopen', methods=['PUT'])
@verify_api_key
@verify_admin
def reactivate_event():
check_in_code = request.get_json().get('check_in_code')
event = db.events.find_one({'check_in_code': check_in_code})
if event is None or event.get('active'):
return jsonify(con.WRONG_CHECK_IN_CODE), 403
toggle_active_delete(db, check_in_code, active=True)
return jsonify(con.SUCCESS), 200
@app.route('/event/delete', methods=['PUT'])
@verify_api_key
@verify_admin
def delete_event():
check_in_code = request.get_json().get('check_in_code')
event = db.events.find_one({'check_in_code': check_in_code})
if event is None or event.get('deleted'):
return jsonify(con.WRONG_CHECK_IN_CODE), 403
toggle_active_delete(db, check_in_code, active=False, delete=True)
return jsonify(con.SUCCESS), 200
@app.route('/event/reactivate', methods=['PUT'])
@verify_api_key
@verify_admin
def restore_event():
check_in_code = request.get_json().get('check_in_code')
event = db.events.find_one({'check_in_code': check_in_code})
if event is None or not event.get('deleted'):
return jsonify(con.WRONG_CHECK_IN_CODE), 403
toggle_active_delete(db, check_in_code, active=True, delete=False)
return jsonify(con.SUCCESS), 200
@app.route('/score', methods=['GET'])
@verify_api_key
def get_score():
slack_id = request.headers.get('slack_id')
# verify this is a slack user
if not verify_user(slack_id):
return jsonify(con.SLACK_ID_NOT_FOUND), 404
# check if the user is in db, add if not
user = db.users.find_one({'slack_id': slack_id})
if user is None:
return jsonify({'score': 0})
return jsonify({'score': user.get('score')})
@app.route('/users/scores', methods=['GET'])
def get_all_scores():
users = db.users.find({})
users = sorted(users, key=lambda x: x['score'], reverse=True)
for i in range(0, len(users)):
users[i].pop('_id', None)
return jsonify(users)
@app.route('/users/scores/edit', methods=['POST'])
@verify_api_key
@verify_admin
def edit_score():
slack_id = request.get_json().get('slack_id')
score = request.get_json().get('score')
if score is None:
return jsonify(con.MISSING_FIELDS), 400
# verify this is a slack user
if not verify_user(slack_id):
return jsonify(con.SLACK_ID_NOT_FOUND), 404
user = db.users.find_one({'slack_id': slack_id})
updated_score = user.get('score') + request.get_json().get('score')
if user is not None:
db.users.update_one({'slack_id': slack_id}, {
'$set': {'score': updated_score}})
return jsonify(con.SUCCESS), 200
return jsonify(con.SLACK_ID_NOT_FOUND), 404
@app.route('/events/<check_in_code>', methods=['GET'])
def view_attendees(check_in_code):
event = db.events.find_one({'check_in_code': check_in_code})
if event is None:
return jsonify(con.WRONG_CHECK_IN_CODE), 403
event.pop('_id', None)
users = []
if event is not None:
for attendee in event.get('attendees'):
user = db.users.find_one({'slack_id': attendee})
users.append(user.get('username'))
event.pop('attendees', None)
return jsonify({
"event": event,
"attendees": users,
"count": len(users)
})
if __name__ == '__main__':
app.run(port=int(os.environ.get('PORT', 5000)))