-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompy_flask.py
827 lines (741 loc) · 33.2 KB
/
compy_flask.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#
# ━━━━━━━━━━━━━
# ┏┓┏┓┳┳┓┏┓┓┏
# ┃ ┃┃┃┃┃┃┃┗┫
# ┗┛┗┛┛ ┗┣┛┗┛
# ━━━━━━━━━━━━━
#
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#
# Competition organization tool
# for freediving competitions.
#
# Copyright 2023 - Arno Mayrhofer
#
# Licensed under the GNU AGPL
#
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#
# Authors:
#
# - Arno Mayrhofer
#
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
import logging
from flask import Flask, render_template, request, send_file, Response, make_response, current_app
from os import path, mkdir
from werkzeug.utils import secure_filename
try:
import country_converter
except ImportError:
print("Could not find country_converter. Install with 'pip3 install country_converter'")
exit(-1)
class CompyFlask:
def __init__(self, app, data, start_flask):
self.data_ = data
self.app_ = app
app.config['UPLOAD_FOLDER'] = self.data_.config.upload_folder
@app.route('/admin', methods=['GET'])
def admin():
return self.admin()
@app.route('/upload_file', methods=['POST'])
def uploadFile():
return self.uploadFile()
@app.route('/upload_sponsor_img', methods=['POST'])
def uploadSponsorImg():
return self.uploadSponsorImg()
@app.route('/change_comp_name', methods=['POST'])
def changeCompName():
return self.changeCompName()
@app.route('/change_special_ranking_name', methods=['POST'])
def changeSpecialRankingName():
return self.changeSpecialRankingName()
@app.route('/change_registration', methods=['POST'])
def changeRegistration():
return self.changeRegistration()
@app.route('/load_comp', methods=['POST'])
def loadComp():
return self.loadComp()
@app.route('/start_list', methods=['GET', 'PUT'])
def startList():
if request.method == 'GET':
return self.startList()
elif request.method == 'PUT':
return self.updateStartList()
@app.route('/start_list_pdf', methods=['GET'])
def startListPDF():
return self.startListPDF()
@app.route('/breaks', methods=['GET'])
def breaks():
return self.breaks()
@app.route('/lane_list', methods=['GET'])
def laneList():
return self.laneList()
@app.route('/lane_list_pdf', methods=['GET'])
def laneListPDF():
return self.laneListPDF()
@app.route('/result', methods=['GET', 'PUT'])
def result():
if request.method == 'GET':
return self.result(False)
elif request.method == 'PUT':
return self.updateResult()
@app.route('/result_pdf', methods=['GET'])
def resultPDF():
return self.result(True)
@app.route('/change_lane_style', methods=['POST'])
def changeLaneStyle():
return self.changeLaneStyle()
@app.route('/change_comp_type', methods=['POST'])
def changeCompType():
return self.changeCompType()
@app.route('/change_selected_country', methods=['POST'])
def changeSelectedCountry():
return self.changeSelectedCountry()
@app.route('/judge', methods=['DELETE', 'POST'])
def judge():
if request.method == 'DELETE':
return self.deleteJudge()
elif request.method == 'POST':
return self.addJudge()
@app.route('/judge/qr_code', methods=['GET'])
def judgeQrCode():
return self.getJudgeQrCode()
@app.route('/judges', methods=['GET'])
def judges():
return self.getJudges()
@app.route('/athlete', methods=['DELETE', 'POST'])
def athlete():
if request.method == 'DELETE':
return self.deleteAthlete()
elif request.method == 'POST':
return self.addAthlete()
@app.route('/athletes', methods=['GET'])
def athletes():
return self.getAthletes()
@app.route('/national_records', methods=['GET'])
def nationalRecords():
return self.nationalRecords()
@app.route('/judge/<int:comp_id>/<int:judge_id>', methods=['GET'])
def judgeComp(comp_id, judge_id):
return self.getJudgeComp(comp_id, judge_id)
@app.route('/judge_json/<int:comp_id>/<int:judge_id>', methods=['GET'])
def judgeJsonComp(comp_id, judge_id):
return self.getJudgeComp(comp_id, judge_id, True)
@app.route('/judge/athletes', methods=['GET'])
def judgeAthlete():
return self.getJudgeAthletes()
@app.route('/judge/athlete/result', methods=['GET'])
def judgeAthleteResult():
if request.method == 'GET':
return self.getJudgeAthleteResult()
@app.route('/disciplines/<federation>', methods=['GET'])
def disciplines(federation):
return self.disciplines(federation)
@app.route('/block', methods=['POST', 'UPDATE', 'DELETE'])
def block():
if request.method == 'POST':
return self.modifyBlock(True)
elif request.method == 'UPDATE':
return self.modifyBlock(False)
elif request.method == 'DELETE':
return self.deleteBlock()
@app.route('/clock/<int:comp_id>/<int:current>', methods=['GET'])
def clock(comp_id, current):
return self.getClock(comp_id, current)
if start_flask:
app.run()
def uploadFile(self):
if 'file' not in request.files:
logging.debug("Post request without file upload")
return {}, 400
logging.debug("Received file upload")
data_file = request.files['file']
status_msg = ""
data = {"status": "success"}
if data_file.filename == '':
logging.debug("File upload with empty filename")
status_msg = "No file uploaded due to empty filename"
else:
filename = secure_filename(data_file.filename)
ext = path.splitext(filename)[1].lower()
if ext != ".xlsx":
status_msg = "File uploaded (" + filename + ") is not a *.xlsx file"
else:
status_msg = "File '" + filename + "' uploaded successfully"
fpath = path.join(self.app_.config['UPLOAD_FOLDER'], filename)
data_file.save(fpath)
self.data_.compFileChange(fpath)
self.data_.getAthleteData(data)
self.data_.getJudgeData(data)
self.setSubmenuData(data)
self.data_.setOTs(data)
data["status_msg"] = status_msg
return data, 200
def uploadSponsorImg(self):
if 'sponsor_img' not in request.files:
logging.debug("Post request without image upload")
return {}, 400
logging.debug("Received sponsor image upload")
img_file = request.files['sponsor_img']
status_msg = ""
data = {"status": "success"}
if img_file.filename == '':
logging.debug("Image upload with empty filename")
status_msg = "No image uploaded due to empty filename"
else:
filename = secure_filename(img_file.filename)
ext = path.splitext(filename)[1].lower()
if ext != ".png":
status_msg = "Image uploaded (" + filename + ") is not a *.png file"
else:
status_msg = "Image '" + filename + "' uploaded successfully"
fpath = path.join(self.app_.config['UPLOAD_FOLDER'], filename)
img_content = img_file.read()
self.data_.changeSponsorImage(img_content)
data["status_msg"] = status_msg
return data, 200
def changeRegistration(self):
content = request.json
if "id" not in content and "checked" not in content and "type" not in content:
logging.debug("Post request to change_special_ranking without id, type and checked")
return {}, 400
athlete_id = content["id"]
is_special_ranking = content["checked"]
change_type = content["type"]
if self.data_.setRegistration(athlete_id, is_special_ranking, change_type) == 0:
data = {"status": "success", "status_msg": "Successfully updated athlete with id '" + athlete_id + "' to value '" + str(is_special_ranking) + "'"}
else:
data = {"status": "success", "status_msg": "Failed to update athlete with id '" + athlete_id + "' to value '" + str(is_special_ranking) + "'"}
data["disciplines"] = self.data_.getDisciplines()
return data, 200
def changeCompName(self):
content = request.json
if "comp_name" not in content and "overwrite" not in content:
logging.debug("Post request to change_comp_name without comp_name and overwrite")
return {}, 400
comp_name = content["comp_name"]
overwrite = content["overwrite"]
file_exists, name = self.data_.changeName(comp_name, overwrite)
data = {}
if file_exists == 0:
data = {"status": "success", "status_msg": "Successfully changed competition name to '" + comp_name + "'", "file_exists": False, "prev_name": ""}
else:
data = {"status": "success", "status_msg": "File exists", "file_exists": True, "prev_name": name}
return data, 200
def loadComp(self):
content = request.json
if "comp_id" not in content:
logging.debug("Post request to load_comp without comp_id")
return {}, 400
comp_id = content["comp_id"]
comp_name = self.data_.load(comp_id)
data = {}
self.data_.getAthleteData(data)
self.data_.getJudgeData(data)
data["comp_name"] = comp_name
self.setSubmenuData(data)
self.data_.setSpecialRankingName(data)
self.data_.setOTs(data)
data["lane_style"] = self.data_.lane_style
data["comp_type"] = self.data_.comp_type
data["selected_country"] = self.data_.selected_country
data["status"] = "success"
data["status_msg"] = "Loaded competition with name " + comp_name
logging.debug("Loaded comp " + comp_name + " with " + str(self.data_.number_of_athletes) + " athletes")
return data, 200
def startList(self):
day = request.args.get('day')
block = request.args.get('block')
if day is None or block is None:
logging.debug("Get request to start_list without day and block")
return {}, 400
data = {}
start_list = self.data_.getStartList(day, block)
if not start_list is None:
data["start_list"] = start_list
data["status"] = "success"
data["status_msg"] = "Transfered start list for " + day + ": " + block
return data, 200
else:
logging.debug("Could not get start list for " + day + ": " + block)
return {}, 400
def updateStartList(self):
content = request.json
if not self.dictHas(content, {'day', 'block', 'to_remove', 'startlist'}):
logging.debug("Put request to start_list missing content: " + str(content.keys()))
return {}, 400
day = content["day"]
block = content["block"]
to_remove = content["to_remove"]
startlist = content["startlist"]
ret = self.data_.updateStartList(day, block, to_remove, startlist)
start_list = self.data_.getStartList(day, block)
if ret == 0 and not start_list is None:
data = {"status": "success", "status_msg": "Successfully updated start list", "start_list": start_list}
return data, 200
elif ret != 0 and not start_list is None:
data = {"status": "success", "status_msg": "Failed database update", "start_list": start_list}
return data, 400
else:
logging.debug("Could not update start list for " + day + " & " + block)
return {}, 400
def startListPDF(self):
day = request.args.get('day')
block = request.args.get('block')
req_type = request.args.get('type')
if (day is None or block is None) and req_type is None:
logging.debug("Get request to start_list without day, block or type")
return {}, 400
data = {}
if req_type is not None and req_type == "all":
start_list_pdf = self.data_.getStartListPDF()
else:
start_list_pdf = self.data_.getStartListPDF(day, block)
if not start_list_pdf is None:
logging.debug("Sending: " + start_list_pdf)
return send_file(start_list_pdf, as_attachment=True)
else:
logging.debug("Could not get start list for " + day + ": " + block)
return {}, 400
def breaks(self):
day = request.args.get('day')
if day is None:
logging.debug("Get request to breaks without day")
return {}, 400
data = {}
breaks = self.data_.getBreaks(day)
if not breaks is None:
data["min_break"] = breaks["min_break"]
data["breaks_list"] = breaks["breaks_list"]
data["status"] = "success"
data["status_msg"] = "Transfered breaks for " + day
return data, 200
else:
logging.debug("Could not get breaks for " + day)
return {}, 400
def laneList(self):
day = request.args.get('day')
block = request.args.get('block')
lane = request.args.get('lane')
if day is None or block is None or lane is None:
logging.debug("Get request to lane_list without day, block or lane")
return {}, 400
data = {}
lane_list = self.data_.getLaneList(day, block, lane)
if not lane_list is None:
data["lane_list"] = lane_list
data["status"] = "success"
data["status_msg"] = "Transfered lane list for " + day + ": " + block + "/" + lane
return data, 200
else:
logging.debug("Could not get lane list for " + day + ": " + block + "/" + lane)
return {}, 400
def laneListPDF(self):
day = request.args.get('day')
block = request.args.get('block')
lane = request.args.get('lane')
req_type = request.args.get('type')
if (day is None or block is None or lane is None) and req_type is None:
logging.debug("Get request to lane_list without day, block, lane or type")
return {}, 400
data = {}
if req_type is not None and (req_type == "all" or req_type == "safety"):
lane_list_pdf = self.data_.getLaneListPDF(req_type == "safety")
else:
lane_list_pdf = self.data_.getLaneListPDF(False, day, block, lane)
if not lane_list_pdf is None:
logging.debug("Sending: " + lane_list_pdf)
return send_file(lane_list_pdf, as_attachment=True)
else:
logging.debug("Could not get lane list for " + day + ": " + block + "/" + lane)
return {}, 400
def changeLaneStyle(self):
content = request.json
if "lane_style" not in content:
logging.debug("Change request for lane style missing variable")
return {}, 400
option = content["lane_style"]
if self.data_.changeLaneStyle(option) == 0:
data = {}
data["days_with_disciplines_lanes"] = self.data_.getDaysWithDisciplinesLanes()
data["blocks"] = self.data_.getBlocks()
data["status_msg"] = "Successfully changed lane style"
data["status"] = "success"
return data, 200
else:
logging.debug("Invalid lane style")
return {}, 400
def changeCompType(self):
content = request.json
if "comp_type" not in content:
logging.debug("Change request for comp type missing variable")
return {}, 400
option = content["comp_type"]
if self.data_.changeCompType(option) == 0:
data = {}
data["days_with_disciplines_lanes"] = self.data_.getDaysWithDisciplinesLanes()
data["blocks"] = self.data_.getBlocks()
data["status_msg"] = "Successfully changed comp type"
data["status"] = "success"
return data, 200
else:
logging.debug("Invalid comp type")
return {}, 400
def changeSelectedCountry(self):
content = request.json
if "selected_country" not in content:
logging.debug("Change request for selected country missing variable")
return {}, 400
option = content["selected_country"]
if self.data_.changeSelectedCountry(option) == 0:
data = {}
self.setSubmenuData(data)
data["status_msg"] = "Successfully changed selected country"
data["status"] = "success"
return data, 200
else:
logging.debug("Invalid country selected")
return {}, 400
def setSubmenuData(self, data):
data["days_with_disciplines_lanes"] = self.data_.getDaysWithDisciplinesLanes()
data["blocks"] = self.data_.getBlocks()
data["disciplines"] = self.data_.getDisciplines()
data["countries"] = self.data_.getCountries()
data["result_countries"] = self.data_.getCountries(True)
def dictHas(self, d, keys):
if isinstance(keys, set):
return d.keys() >= keys
else:
return keys in d
def result(self, pdf):
discipline = request.args.get('discipline')
gender = request.args.get('gender')
country = request.args.get('country')
req_type = request.args.get('type')
if (discipline is None or gender is None or country is None) and (req_type is None or not pdf):
logging.debug("Get request to result without discipline, gender, country or type")
return {}, 400
if pdf:
if req_type == "all":
result_pdf = self.data_.getResultPDF()
elif req_type == "top3":
result_pdf = self.data_.getResultPDF("all", "all", "all", False, True)
elif req_type == "single" or req_type is None:
result_pdf = self.data_.getResultPDF(discipline, gender, country)
if not result_pdf is None:
logging.debug("Sending: " + result_pdf)
return send_file(result_pdf, as_attachment=True)
else:
return self.getResultDiscipline(discipline, gender, country)
logging.debug("Could not get result for " + discipline + "/" + gender + " country: " + country)
return {}, 400
def getResultDiscipline(self, discipline, gender, country):
result, result_keys = self.data_.getResult(discipline, gender, country, True)
data = {}
if not result is None:
data["result"] = result
data["result_keys"] = result_keys
data["status"] = "success"
data["status_msg"] = "Transfered result for " + discipline + "/" + gender + " country: " + country
return data, 200
def updateResult(self):
content = request.json
if not self.dictHas(content, {"id", "rp", "penalty", "card", "remarks", "judge_remarks"}):
breakpoint()
logging.debug("Put request to result without discipline, gender, country, id, rp, penalty, card, remarks, judge_remarks")
return {}, 400
s_id = content["id"]
rp = content["rp"]
penalty = content["penalty"]
card = content["card"]
remarks = content["remarks"]
judge_remarks = content["judge_remarks"]
res = self.data_.updateResult(s_id, rp, penalty, card, remarks, judge_remarks)
if res == 1:
logging.debug("Failed to set result")
return {}, 400
if self.dictHas(content, {"discipline", "gender", "country"}):
discipline = content["discipline"]
gender = content["gender"]
country = content["country"]
return self.getResultDiscipline(discipline, gender, country)
else:
data = self.data_.getAthleteResult(s_id)
if data is None:
logging.debug("Get request to athlete result failed.")
return {}, 400
data["status"] = "success"
data["status_msg"] = "Completed request for athlete result of " + data["Name"]
return data, 200
def changeSpecialRankingName(self):
content = request.json
if "special_ranking_name" not in content:
logging.debug("Post request to change_special_ranking_name without special_ranking_name")
return {}, 400
special_ranking_name = content["special_ranking_name"]
self.data_.changeSpecialRankingName(special_ranking_name)
data = {"status": "success", "status_msg": "Successfully changed special ranking name to '" + special_ranking_name + "'"}
self.setSubmenuData(data)
return data, 200
def deleteJudge(self):
judge_id = request.json.get('judge_id')
if judge_id is None:
logging.info("Could not delete judge without getting an id")
return {}, 400
try:
judge_id = int(judge_id)
except (ValueError, TypeError):
logging.info("judge id has wrong type")
return {}, 400
data = {}
judge_id = self.data_.isJudgeInCompetition(judge_id)
if judge_id is not None:
self.data_.deleteJudge(judge_id)
data = {"status": "success", "status_msg": "Successfully deleted judge with id " + str(judge_id)}
self.data_.getJudgeData(data)
return data, 200
else:
logging.info('Invalid judge id provided')
return {}, 400
def getJudgeQrCode(self):
judge_id = request.args.get('judge_id')
if judge_id is None:
logging.info("Could not get judge qr code without getting an id")
return {}, 400
try:
judge_id = int(judge_id)
except (ValueError, TypeError):
logging.info("judge id has wrong type")
return {}, 400
data = {}
judge_id = self.data_.isJudgeInCompetition(judge_id)
if judge_id is not None:
qr_data = self.data_.getJudgeQrCode(judge_id, request.url_root)
if qr_data is None:
logging.info('Could not get qr code for judge with id ' + str(judge_id))
return {}, 400
else:
data = {"status": "success", "status_msg": "Successfully got judge qr_code with id " + str(judge_id), "judge_qr_code": qr_data[0], "judge_first_name": qr_data[1], "judge_last_name": qr_data[2], "judge_url": qr_data[3]}
return data, 200
else:
logging.info('Invalid judge id provided')
return {}, 400
def addJudge(self):
content = request.json
if False in [key in content for key in ['first_name', 'last_name']]:
logging.info("Could not add judge due to missing data")
return {}, 400
first_name = content['first_name']
last_name = content['last_name']
if first_name is None or last_name is None:
logging.info("Could not add judge with incomplete information")
return {}, 400
status = self.data_.addJudge(first_name, last_name)
data = {}
if status == 0:
data = {"status": "success", "status_msg": "Successfully added judge"}
self.data_.getJudgeData(data)
return data, 200
elif status == 1:
data = {"status": "error", "status_msg": "Judge already exists"}
self.data_.getJudgeData(data)
return data, 200
def getJudges(self):
data = {"status": "success", "status_msg": "Successfully received judge data"}
self.data_.getJudgeData(data)
return data, 200
def deleteAthlete(self):
athlete_id = request.json.get('athlete_id')
if athlete_id is None:
logging.info("Could not delete athlete without getting an id")
return {}, 400
try:
athlete_id = int(athlete_id)
except (ValueError, TypeError):
logging.info("athlete id has wrong type")
return {}, 400
data = {}
ca_id, in_other_comp = self.data_.isAthleteInCompetition(athlete_id)
if ca_id is not None:
self.data_.deleteAthlete(ca_id, athlete_id, in_other_comp)
data = {"status": "success", "status_msg": "Successfully deleted athlete with id " + str(athlete_id) + (" completely" if not in_other_comp else "")}
self.data_.getAthleteData(data)
self.setSubmenuData(data)
self.data_.setOTs(data)
return data, 200
else:
logging.info('Invalid athlete id provided')
return {}, 400
def addAthlete(self):
content = request.json
if False in [key in content for key in ['first_name', 'last_name', 'gender', 'country', 'club', 'aida_id']]:
logging.info("Could not add athlete due to missing data")
return {}, 400
first_name = content['first_name']
last_name = content['last_name']
gender = content['gender']
country = content['country']
club = content['club']
aida_id = content['aida_id']
if first_name is None or last_name is None or gender not in ["M", "F"] or \
country is None or club is None or aida_id is None:
logging.info("Could not add athlete with incomplete information")
return {}, 400
status = self.data_.addAthlete(first_name, last_name, gender, country, club, aida_id)
data = {}
if status == 0:
data = {"status": "success", "status_msg": "Successfully added athlete"}
self.data_.getAthleteData(data)
return data, 200
elif status == 1:
data = {"status": "error", "status_msg": "Athlete already exists"}
return data, 200
def getAthletes(self):
data = {"status": "success", "status_msg": "Successfully received athlete data"}
self.data_.getAthleteData(data)
return data, 200
def nationalRecords(self):
status = self.data_.updateNationalRecords()
if status == 0:
return {"status": "success", "status_msg": "Successfully updated national records"}, 200
else:
return {"status": "error", "status_msg": "Error while updating national records"}, 500
def getJudgeComp(self, comp_id, judge_id, return_json = False):
judge_hash = request.args.get('hash')
comp_data = self.data_.getCompDataAndValidateJudge(comp_id, judge_id, judge_hash)
if comp_data is None:
content = {"version": self.data_.version}
return make_response(render_template('404.html', **content), 404)
comp_name = comp_data[0]
first_name = comp_data[1]
last_name = comp_data[2]
federation = comp_data[3]
self.data_.load(comp_id)
content = {"version": self.data_.version,
"comp_id": comp_id,
"comp_name": comp_name,
"judge_id": judge_id,
"judge_hash": judge_hash,
"judge_first_name": first_name,
"judge_last_name": last_name,
"federation": federation,
"days_with_disciplines_lanes": self.data_.getDaysWithDisciplinesLanes(),
"blocks": self.data_.getBlocks()}
if return_json:
return content, 200
else:
return render_template('judge.html', **content)
def isValidJudge(self):
judge_hash = request.args.get('judge_hash')
judge_id = request.args.get('judge_id')
comp_id = request.args.get('comp_id')
comp_data = self.data_.getCompDataAndValidateJudge(comp_id, judge_id, judge_hash)
return comp_data is not None
def getJudgeAthletes(self):
if not self.isValidJudge():
content = {"version": self.data_.version}
return make_response(render_template('404.html', **content), 404)
return self.laneList()
def getJudgeAthleteResult(self):
if not self.isValidJudge():
logging.debug("Not a valid judge")
return {}, 400
day = request.args.get('day')
block = request.args.get('block')
lane = request.args.get('lane')
s_id = request.args.get('s_id')
if day is None or block is None or lane is None or s_id is None:
logging.debug("Get request to athlete result without day, block, lane or start id")
return {}, 400
data = self.data_.getAthleteResult(s_id)
if data is None:
logging.debug("Get request to athlete result failed.")
return {}, 400
data["status"] = "success"
data["status_msg"] = "Completed request for athlete result of " + data["Name"]
return data, 200
def disciplines(self, federation):
disciplines = self.data_.getAllDisciplines(federation)
if disciplines is None:
return {}, 400
else:
data = {"status": "success", "disciplines": disciplines}
return data, 200
def modifyBlock(self, add):
content = request.json
if False in [key in content for key in ['day', 'dis', 'block']]:
logging.info("Could not add/edit block due to missing data")
return {}, 400
day = content['day']
disciplines = content['dis']
block = content['block']
if disciplines is None or day is None or (not add and block is None):
logging.info("Could not add/edit block with incomplete information")
return {}, 400
ret = self.data_.modifyBlock(day, disciplines, block, add)
data = None
if ret == 0:
data = {"status": "success", "status_msg": "Successfully updated block"}
elif ret == 1:
data = {"status": "success", "status_msg": "Could not add block, already exists"}
elif ret == 2:
data = {"status": "success", "status_msg": "Could not edit block, does not exist"}
if data is not None:
data["days_with_disciplines_lanes"] = self.data_.getDaysWithDisciplinesLanes()
data["blocks"] = self.data_.getBlocks()
return data, 200
else:
return {}, 400
def deleteBlock(self):
content = request.json
if False in [key in content for key in ['block']]:
logging.info("Could not remove block due to missing data")
return {}, 400
block = content['block']
if block is None:
logging.info("Could not remove block with incomplete information")
return {}, 400
ret = self.data_.removeBlock(block)
data = None
if ret == 0:
data = {"status": "success", "status_msg": "Successfully removed block"}
elif ret == 1:
data = {"status": "success", "status_msg": "Could not remove block, does not exist"}
if data is not None:
data["days_with_disciplines_lanes"] = self.data_.getDaysWithDisciplinesLanes()
data["blocks"] = self.data_.getBlocks()
return data, 200
else:
return {}, 400
def getClock(self, comp_id, current):
current = (current+1) % 2
comp_name = self.data_.load(comp_id)
alist = self.data_.getFourStarts(current == 0)
if alist is None:
current = (current+1) % 2
alist = self.data_.getFourStarts(current == 0)
if comp_name != None:
refresh_url = request.base_url[:request.base_url.rfind('/')+1] + str(current)
content = {"comp_name": comp_name,
"comp_id": comp_id,
"alist": alist,
"current": current,
"refresh_url": refresh_url}
return render_template('clock.html', **content)
else:
return {}, 400
def admin(self):
auth = request.args.get('auth')
if auth != current_app.config["SECRET_KEY"][:6]:
content = {"version": self.data_.version}
return make_response(render_template('404.html', **content), 404)
all_countries = country_converter.CountryConverter().data["IOC"].dropna().to_list()
first_records = None #foo.get_records(Federation.AIDA, all_countries[0], Gender.FEMALE)
content = {"version": self.data_.version,
"competitions": self.data_.getSavedCompetitions(),
"comp_name": self.data_.name,
"all_countries": all_countries,
"record_sta": None}
return render_template('template.html', **content)