-
Notifications
You must be signed in to change notification settings - Fork 2
/
skala_db.py
1089 lines (794 loc) · 31.6 KB
/
skala_db.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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import glob
import random
from datetime import datetime, date, timedelta, timezone
import time
from io import BytesIO
from src.User import User
# Copyright (C) 2023 David Mossakowski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from collections import Counter
import sqlite3 as lite
import uuid
from threading import RLock
import requests
sql_lock = RLock()
from flask import Flask, redirect, url_for, session, request, render_template, send_file, jsonify, Response, \
stream_with_context, copy_current_request_context
import logging
DATA_DIRECTORY = os.getenv('DATA_DIRECTORY')
print("DATA_DIRECTORY="+str(DATA_DIRECTORY))
SPOTIFY_APP_ID = os.getenv('SPOTIFY_APP_ID')
print("SPOTIFY_APP_ID="+str(SPOTIFY_APP_ID))
ENV_VAR = os.getenv('ENV_VAR')
print("ENV_VAR="+str(ENV_VAR))
LINKED_VAR = os.getenv('LINKED_VAR')
print("LINKED_VAR="+str(LINKED_VAR))
if DATA_DIRECTORY is None:
DATA_DIRECTORY = os.getcwd()
#PLAYLISTS_DB = DATA_DIRECTORY + "/db/playlists.sqlite"
COMPETITIONS_DB = DATA_DIRECTORY + "/db/competitions.sqlite"
# ID, date, name, location
COMPETITIONS_TABLE = "competitions"
# ID, name, club, m/f, list of climbs
USERS_TABLE = "climbers"
ROUTES_TABLE = "routes"
ROUTES_CLIMBED_TABLE = "routes_climbed"
GYM_TABLE = "gyms"
CHALLENGE_TABLE = "challenges"
IMG_TABLE = "images"
def init():
logging.info('initializing skala_db...')
if not os.path.exists(DATA_DIRECTORY):
os.makedirs(DATA_DIRECTORY)
if os.path.exists(DATA_DIRECTORY):
db = lite.connect(COMPETITIONS_DB)
# ptype 0-public
cursor = db.cursor()
cursor.execute('''CREATE TABLE if not exists ''' + COMPETITIONS_TABLE + '''(
id text NOT NULL UNIQUE,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null,
jsondata json)''')
cursor.execute('''CREATE TABLE if not exists ''' + USERS_TABLE + '''(
id text NOT NULL UNIQUE,
email text not null,
jsondata integer not null,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null)''')
cursor.execute('''CREATE TABLE if not exists ''' + GYM_TABLE + '''(
id text NOT NULL UNIQUE,
routesid text not null,
jsondata json not null,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null)''')
cursor.execute('''CREATE TABLE if not exists ''' + ROUTES_TABLE + '''(
id text NOT NULL UNIQUE,
gym_id text NOT NULL,
jsondata json,
added_at DEFAULT CURRENT_TIMESTAMP not null)''')
cursor.execute('''CREATE TABLE if not exists ''' + ROUTES_CLIMBED_TABLE + '''(
id text NOT NULL UNIQUE,
competitionId text not null,
climberId text not null,
routes json not null,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null)''')
cursor.execute('''CREATE TABLE if not exists ''' + IMG_TABLE + '''
(id text not null primary key,
picture BLOB not null,
source TEXT not null)''')
#cursor.execute('''CREATE TABLE if not exists ''' + CHALLENGE_TABLE + '''(
# id text NOT NULL UNIQUE,
# type text not null,
# climberId text not null,
# jsondata json not null,
# added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null)''')
db.commit()
#add_testing_data()
#print('loading routes from ' + COMPETITIONS_DB)
#routes = add_nanterre_routes()
#user_authenticated_fb("c1", "Bob Mob", "[email protected]",
# "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10224632176365169&height=50&width=50&ext=1648837065&hash=AeTqQus7FdgHfkpseKk")
#user_authenticated_fb("c1", "Bob Mob2", "[email protected]",
# "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10224632176365169&height=50&width=50&ext=1648837065&hash=AeTqQus7FdgHfkpseKk")
#user_authenticated_fb("c2", "Mary J", "[email protected]",
# "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10224632176365169&height=50&width=50&ext=1648837065&hash=AeTqQus7FdgHfkpseKk")
print('created ' + COMPETITIONS_DB)
def _add_competition(compId, competition):
if compId is None:
compId = str(uuid.uuid4().hex)
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute("INSERT INTO " + COMPETITIONS_TABLE +
"(id, jsondata, added_at) VALUES"+
" (?, ?, datetime('now')) ",
[compId, json.dumps(competition)])
logging.info('competition added: '+str(compId))
finally:
db.commit()
db.close()
sql_lock.release()
#internal method.. not locked!!!
# used when saving competiton details in admin
def _update_competition(compId, competition):
if compId is None:
raise ValueError("cannot update competition with None key");
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute("update " + COMPETITIONS_TABLE + " set jsondata=? where id=? ",
[json.dumps(competition), compId])
#logging.info('updated competition: '+str(compId))
db.commit()
db.close()
def delete_competition(compId):
if compId is None:
raise ValueError("cannot delete competition with None key");
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute("delete from " + COMPETITIONS_TABLE + " where id=? ",
[compId])
db.commit()
db.close()
def get_competition(compId):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
one = cursor.execute(
'''SELECT jsondata FROM ''' + COMPETITIONS_TABLE + ''' where id=? LIMIT 1;''',[compId])
one = one.fetchone()
if one is None or one[0] is None:
return None
else:
competition = json.loads(one[0])
return competition
def get_competitions_for_email(email):
email = email.lower()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
cursor.execute("SELECT DISTINCT json_extract(competitions.jsondata,'$.id') FROM competitions, json_tree(competitions.jsondata, '$.climbers') WHERE json_tree.key='email' AND json_tree.value=? order by added_at desc", [email])
# Extract the competition ids from the query results
competition_ids = [row[0] for row in cursor.fetchall()]
return competition_ids
def get_all_competitions():
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT jsondata FROM ''' + COMPETITIONS_TABLE + ''' order by added_at desc ;''')
comps = {}
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
comp = row[0]
comp = json.loads(row[0])
comps[comp['id']] = comp
return comps
def get_all_gyms():
return _get_gyms()
def get_user(id):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
one = cursor.execute(
'''SELECT jsondata FROM ''' + USERS_TABLE + ''' where id=? LIMIT 1;''',[id])
one = one.fetchone()
if one is None or one[0] is None:
return None
if one[0] is not None:
return json.loads(one[0])
else:
return None
def get_user_by_email(email):
if email is None:
return None
email = email.lower()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
one = cursor.execute(
'''SELECT jsondata FROM ''' + USERS_TABLE + ''' where email=? LIMIT 1;''', [email])
one = one.fetchone()
if one is None or one[0] is None:
return None
user = json.loads(one[0])
if user.get('email') is None:
user['email'] = email
return user
def get_users_by_gym_id(gym_id):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT jsondata FROM ''' + USERS_TABLE + ''' where json_extract(jsondata, '$.gymid')=? ;''', [gym_id])
users = []
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
user = json.loads(row[0])
users.append(user)
return users
def get_all_user_emails():
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT email FROM ''' + USERS_TABLE + ''' order by email ;''')
emails = []
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
emails.append(row[0])
return emails
def get_all_users():
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT id, jsondata FROM ''' + USERS_TABLE )
users = []
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
user = json.loads(row[1])
users.append(user)
return users
def search_all_users(search_string):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
query = '''
SELECT jsondata FROM ''' + USERS_TABLE + '''
WHERE (lower(trim(json_extract(jsondata, '$.fullname'))) LIKE lower(trim(?))
OR lower(trim(json_extract(jsondata, '$.firstname'))) LIKE lower(trim(?))
OR lower(trim(json_extract(jsondata, '$.lastname'))) LIKE lower(trim(?))
OR lower(trim(json_extract(jsondata, '$.nick'))) LIKE lower(trim(?)))
AND json_extract(jsondata, '$.is_confirmed') = true
LIMIT 10;
'''
search_pattern = f'%{search_string}%'
rows = cursor.execute(query, [search_pattern, search_pattern, search_pattern, search_pattern])
users = []
if rows is not None:
for row in rows.fetchall():
user = json.loads(row[0])
users.append(user)
return users
def get_all_competition_ids():
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT id FROM ''' + COMPETITIONS_TABLE + ''' ;''')
ids = []
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
ids.append(row[0])
return ids
def upsert_user(user):
try:
sql_lock.acquire()
existing_user = None
email = user.get('email')
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
if email is not None:
email = email.lower()
existing_user = get_user_by_email(email)
if existing_user is None:
_add_user(None, email, user)
logging.info('added user id ' + str(email))
else:
existing_user.update(user)
_update_user(user['id'], email, existing_user)
finally:
db.commit()
db.close()
sql_lock.release()
return existing_user
def user_authenticated_fb(fid, name, email, picture):
try:
sql_lock.acquire()
email = email.lower()
user = get_user_by_email(email)
_common_user_validation(user)
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
if user is None:
newuser = {'fid': fid, 'fname': name, 'email': email, 'fpictureurl': picture }
_add_user(None, email, newuser)
_common_user_validation(newuser)
logging.info('added fb user id ' + str(email))
else:
u = {'fid': fid, 'fname': name, 'email': email, 'fpictureurl': picture}
user.update(u)
_update_user(user['id'], email, user)
logging.info('updated user id ' + str(email))
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("done with user:"+str(email))
def user_authenticated_google(name, email, picture):
try:
sql_lock.acquire()
email = email.lower()
user = get_user_by_email(email)
_common_user_validation(user)
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
if user is None:
newuser = {'gname': name, 'email': email, 'gpictureurl': picture }
_common_user_validation(newuser)
_add_user(None, email, newuser)
logging.info('added google user id ' + str(email))
else:
u = {'gname': name, 'email': email, 'gpictureurl': picture}
user.update(u)
_update_user(user['id'], email, user)
logging.info('updated google user id ' + str(email))
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("done with user:"+str(email))
def _common_user_validation(user):
if user is None:
return
permissions = user.get('permissions')
if permissions is None:
permissions = get_permissions(user)
user['permissions'] = permissions
# returns base empty permissions dictionary
# who can create new competition? gym admins?
def get_permissions(user):
if user is None:
return User.generate_permissions()
if user.get('permissions') is None:
user['permissions'] = User.generate_permissions()
if user.get('email') == '[email protected]':
user['permissions']['godmode'] = True
user['permissions']['general'] = ['create_competition', 'edit_competition', 'update_routes']
user['permissions']['competitions'] = ['abc','def','ghi']
user['permissions']['gyms'] = ['1']
return user['permissions']
def has_permission_for_competition(competitionId, user):
permissions = get_permissions(user)
huh = competitionId in permissions['competitions']
return competitionId in permissions['competitions'] or permissions['godmode'] == True
def has_permission_for_gym(gym_id, user):
permissions = get_permissions(user)
huh = gym_id in permissions['gyms']
return gym_id in permissions['gyms'] or permissions['godmode'] == True
def add_user_permission(user, permission):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
permissions = user.get('permissions')
if permissions is None:
permissions = User.generate_permissions()
user['permissions'] = permissions
if permission not in permissions['general']:
permissions['general'].append(permission)
_update_user(user['id'], user['email'], user)
logging.info('updated user id ' + str(user['email']))
finally:
db.commit()
db.close()
sql_lock.release()
logging.info(str(permission)+" done with user:"+str(user['email']))
return user
# modify permission to edit specific competition to a user
def modify_user_permissions_to_competition(user, competition_id, action="ADD"):
return _modify_user_permissions(user, competition_id, 'competitions', action)
def modify_user_permissions_to_gym(user, gym_id, action="ADD"):
return _modify_user_permissions(user, gym_id, 'gyms', action)
# modify permission to edit specific competition to a user
def _modify_user_permissions(user, item_id, permission_type, action="ADD"):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
permissions = user.get('permissions')
if permissions is None:
permissions = User.generate_permissions()
user['permissions'] = permissions
if action == "ADD":
if item_id not in permissions[permission_type]:
permissions[permission_type].append(item_id)
_update_user(user['id'], user['email'], user)
logging.info('added user permissions id ' + str(user['email'])+ ' type='+str(permission_type)+
' action='+str(action))
elif action == "REMOVE":
permissions[permission_type].remove(item_id)
_update_user(user['id'], user['email'], user)
logging.info('removed user permissions id ' + str(user['email'])+ ' type='+str(permission_type)+
' action='+str(action))
else:
raise ValueError("Unknown action parameter. Only valid values are ADD or REMOVE")
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("done with user:"+str(user['email']))
return user
# this overwrites details from competition registration to the main user entry
# these details will be used for next competition registration
# these details are deemed the most recent and correct
def user_registered_for_competition(climberId, name, firstname, lastname, email, sex, club, dob):
email = email.lower()
user = get_user_by_email(email)
if climberId is None:
climberId = str(uuid.uuid4().hex)
newclimber = {}
newclimber['id'] = climberId
newclimber['name'] = name
newclimber['firstname'] = firstname
newclimber['lastname'] = lastname
newclimber['sex'] = sex
newclimber['club'] = club
newclimber['dob'] = dob
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
if user is None:
_common_user_validation(newclimber)
_add_user(climberId, email, newclimber)
climber = newclimber
logging.info('added user id ' + str(email))
else:
user.update(newclimber)
_update_user(climberId, email, user)
logging.info('updated user id ' + str(email))
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("done with user:"+str(name))
#return climber
def _add_user(climberId, email, climber):
email = email.lower()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
if climberId is None:
climberId = str(uuid.uuid4().hex)
climber['id'] = climberId
climber['created_on'] = datetime.now(timezone.utc).isoformat()
cursor.execute("INSERT INTO " + USERS_TABLE +
"(id, email, jsondata, added_at) " +
" values (?, ?, ?, datetime('now')) ",
[str(climberId), email, json.dumps(climber)])
logging.info('added user id ' + str(email))
db.commit()
db.close()
return climber
def _update_user(climberId, email, climber):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
email = email.lower()
if climberId is None:
if (climber.get('id') is None):
climberId = str(uuid.uuid4().hex)
climber['id'] = climberId
else:
climberId = climber['id']
cursor.execute("UPDATE " + USERS_TABLE + " set id=?, jsondata=? where email =? ",
[str(climberId), json.dumps(climber), str(email)])
logging.info('updated user id ' + str(email))
db.commit()
db.close()
return climber
def _get_gyms(status=None):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT jsondata FROM ''' + GYM_TABLE + ''' ;''')
gyms = {}
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
#comp = row[0]
gym = json.loads(row[0])
if status is None or gym.get('status') is None or gym.get('status') >= status:
gyms[gym['id']] = gym
db.close()
#for gymid in gyms:
# routes = _get_routes(gyms[gymid]['routesid'])
# gyms[gymid]['routes'] = routes
return gyms
def _get_gym(gymid):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT jsondata FROM ''' + GYM_TABLE + ''' where id=? ;''',[gymid])
gym = {}
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
#comp = row[0]
gym = json.loads(row[0])
#gyms[gym['id']] = gym
db.close()
#routes = _get_routes(gym['routesid'])
#gym['routes'] = routes
return gym
def _get_gyms_by_ids(gym_ids, status=None):
gyms = {}
if not gym_ids:
return gyms
placeholders = ','.join(['?'] * len(gym_ids))
query = f'SELECT id, jsondata FROM {GYM_TABLE} WHERE id IN ({placeholders})'
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
try:
cursor.execute(query, gym_ids)
rows = cursor.fetchall()
for row in rows:
gym_id, jsondata = row
gym = json.loads(jsondata)
if status is None or gym.get('status') == status:
gyms[gym_id] = gym
finally:
db.close()
return gyms
# should gym_id be added to ouput?
def get_routes_by_gym_id(gym_id):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT id,jsondata FROM ''' + ROUTES_TABLE + ''' where gym_id=? ;''', [gym_id])
allroutes = {}
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
#comp = row[0]
id = row[0]
routes = json.loads(row[1])
routes['id'] = id
if routes.get('name') is None:
routes['name'] = ''
allroutes[routes['id']] = routes
db.close()
return allroutes
def get_gym_by_routes_id(routesid):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''select a.jsondata from gyms a, routes b where b.id = ? and a.id=b.gym_id;''', [routesid])
one = rows.fetchone()
db.close()
if one is not None and one[0] is not None:
return json.loads(one[0])
return None
def get_gym_by_gym_name(gym_name):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''select jsondata from gyms where lower(trim(json_extract(jsondata, '$.name'))) like lower(trim(?));''', [gym_name])
one = rows.fetchone()
db.close()
if one is not None and one[0] is not None:
return json.loads(one[0])
return None
def get_all_routes_ids():
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
rows = cursor.execute(
'''SELECT id, gym_id FROM ''' + ROUTES_TABLE + ''' ;''')
routes = []
if rows is not None and rows.arraysize > 0:
for row in rows.fetchall():
#comp = row[0]
#routes = json.loads(row[0])
routes.append({'id':row[0],'gym_id':row[1]})
#gyms[gym['id']] = gym
db.close()
return routes
def get_routes_by_id(routesid):
routes = _get_routes(routesid)
return routes
def _get_routes(routesid):
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
count = 0
one = cursor.execute(
'''SELECT jsondata, gym_id FROM ''' + ROUTES_TABLE + ''' where id=? LIMIT 1;''', [routesid])
one = one.fetchone()
db.close()
if one is None or one[0] is None:
return None
routes = json.loads(one[0])
routes['gym_id'] = one[1]
return routes
def _add_gym(gymid, routesid, gym):
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
jsondata = json.dumps(gym)
cursor.execute("INSERT INTO " + GYM_TABLE + " (id, routesid, jsondata, added_at ) "
"values ( ?, ?, ?, datetime('now'))",
[str(gymid), str(routesid), jsondata])
logging.info('added gym: '+str(jsondata))
db.commit()
db.close()
def delete_routes(routesid):
if routesid is None:
return
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
cursor.execute("delete from " + ROUTES_TABLE + " where id=?",
[str(routesid)])
logging.info('deleted routes: '+str(routesid))
db.commit()
db.close()
def _delete_routes_by_gymid(gymid):
if gymid is None:
return
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
cursor.execute("delete from " + ROUTES_TABLE + " where gym_id=?",
[str(gymid)])
logging.info('deleted routes for gym: '+str(gymid))
db.commit()
db.close()
def _delete_gym(gymid):
if gymid is None:
return
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
cursor.execute("delete from " + GYM_TABLE + " where id=?",
[str(gymid)])
logging.info('deleted gym: '+str(gymid))
db.commit()
db.close()
def _update_gym_routes(gymid, routesid, jsondata):
db = lite.connect(COMPETITIONS_DB)
#db.in_transaction
cursor = db.cursor()
cursor.execute("update " + GYM_TABLE + " set routesid = ? , jsondata = ? , added_at=datetime('now' ) where id=?",
[ str(routesid), json.dumps(jsondata), str(gymid)])
logging.info('updated gym with routes: '+str(routesid))
db.commit()
db.close()
def _update_gym(gymid, jsondata):
db = lite.connect(COMPETITIONS_DB)
#db.in_transaction
cursor = db.cursor()
cursor.execute("update " + GYM_TABLE + " set jsondata = ? , added_at=datetime('now' ) where id=?",
[json.dumps(jsondata), str(gymid)])
logging.info('updated gym: '+jsondata['name'])
db.commit()
db.close()
def _add_routes(routesid, gym_id, jsondata):
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
cursor.execute("INSERT INTO " + ROUTES_TABLE + " (id, gym_id, jsondata, added_at ) "
"values (?, ?, ?, datetime('now'))",
[str(routesid), str(gym_id), json.dumps(jsondata)])
logging.info('added routes: '+str(jsondata))
db.commit()
db.close()
def _update_routes(routesid, jsondata):
db = lite.connect(COMPETITIONS_DB)
db.in_transaction
cursor = db.cursor()
cursor.execute("Update " + ROUTES_TABLE + " set jsondata = ? where id = ? ",
[json.dumps(jsondata), str(routesid)])
logging.info('updated route: '+str(routesid))
db.commit()
db.close()
def save_image(img_id, logourl):
logourl_response = requests.get(logourl)
picture = lite.Binary(logourl_response.content)
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute('''INSERT INTO ''' +IMG_TABLE+''' (id, picture, source) VALUES (?, ?, ?)''',
[img_id, picture, logourl])
db.commit()
db.close()
def get_image(img_id):
# This can only serve one image at a time, so match by id
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute("select picture from " +IMG_TABLE+" WHERE id = ?", (img_id,))
result = cursor.fetchone()
image_bytes = result[0]
bytes_io = BytesIO(image_bytes)
return bytes_io
#migration and one time use methods
# sample query to get non confirmed users
# select email from climbers where lower(trim(json_extract(jsondata, '$.is_confirmed'))) like '0' ;
def update_gym_data(reference_data):
# Connect to the database
conn = lite.connect(COMPETITIONS_DB)
cursor = conn.cursor()
# check if all gyms from the local reference list are in the database
for club_id, club_name in reference_data.get('clubs').items():
gym = get_gym_by_gym_name(club_name)
if gym is not None:
logging.info(f"Club '{club_name}' exists with ID: {gym.get('id')}")
else:
logging.warning(f"gym doesn't exist in db: {club_name}")
# Retrieve all gyms from the GYM_TABLE
cursor.execute('''SELECT id, jsondata FROM ''' + GYM_TABLE + ''' ;''')
gyms = cursor.fetchall()
for gym in gyms:
gym_id, jsondata = gym
gym_data = json.loads(jsondata)
if 'added_by' in gym_data:
email = gym_data['added_by']