-
Notifications
You must be signed in to change notification settings - Fork 0
/
supabase_dao.py
551 lines (464 loc) · 13.9 KB
/
supabase_dao.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
# pylint: disable=E0401
"""
A seris of functions that interact with the Supabase database.
"""
import os
from supabase import create_client
from dotenv import load_dotenv
from models.alert import Alert
from models.banned_person import BannedPerson
from models.banned_person_image import BannedPersonImage
from models.logging import Logging
from models.user import User
from models.store import Store
load_dotenv()
url: str = os.getenv("SUPABASE_URL")
key: str = os.getenv("SUPABASE_KEY")
def get_client():
"""
Get the Supabase client.
Returns:
Supabase client: The Supabase client object.
"""
return create_client(url, key)
def add_user(user: User):
"""
Add a user to the database. A user is an employee of a store.
"""
supabase = get_client()
data, _ = (
supabase.table("users")
.insert(
{
"full_name": user.full_name,
"email": user.email,
"phone_number": user.phone_number,
"is_admin": user.is_admin,
"store_id": user.store_id,
}
)
.execute()
)
user = User(
data[1][0]["full_name"],
data[1][0]["email"],
data[1][0]["phone_number"],
data[1][0]["is_admin"],
data[1][0]["store_id"],
data[1][0]["id"],
)
return user
def get_user_by_id(user_id):
"""
Get a user by their id
"""
supabase = get_client()
data, _ = supabase.table("users").select("*").eq("id", user_id).execute()
user = None
if len(data[1]) > 0:
user = User(
data[1][0]["full_name"],
data[1][0]["email"],
data[1][0]["phone_number"],
data[1][0]["is_admin"],
data[1][0]["store_id"],
data[1][0]["id"],
)
return user
def delete_user_by_id(user_id):
"""
Delete a user by their id
"""
supabase = get_client()
data, _ = supabase.table("users").delete().eq("id", user_id).execute()
user = None
if len(data[1]) > 0:
user = User(
data[1][0]["full_name"],
data[1][0]["email"],
data[1][0]["phone_number"],
data[1][0]["is_admin"],
data[1][0]["store_id"],
data[1][0]["id"],
)
return user
def add_store(store: Store):
"""
Add a store to the database
"""
supabase = get_client()
data, _ = (
supabase.table("stores")
.insert(
{
"name": store.name,
"address": store.address,
"billing_info": store.billing_info,
}
)
.execute()
)
store = Store(
data[1][0]["name"],
data[1][0]["address"],
data[1][0]["billing_info"],
data[1][0]["id"],
)
return store
def get_store_by_id(store_id):
"""
Get a store by its id
"""
supabase = get_client()
data, _ = supabase.table("stores").select("*").eq("id", store_id).execute()
store = None
if len(data[1]) > 0:
store = Store(
data[1][0]["name"],
data[1][0]["address"],
data[1][0]["billing_info"],
data[1][0]["id"],
)
return store
def delete_store_by_id(store_id):
"""
Delete a store by its id.
Doing this will also delete all employees of the store
as well as all people banned by that store and their images
"""
supabase = get_client()
data, _ = supabase.table("stores").delete().eq("id", store_id).execute()
store = Store(
data[1][0]["name"],
data[1][0]["address"],
data[1][0]["billing_info"],
data[1][0]["id"],
)
return store
def get_store_employees_from_device(device_id):
"""
Get all employees of a store from the store that is associated with a device_id
"""
supabase = get_client()
data, _ = supabase.table("device").select("*").eq("id", device_id).execute()
# Check if a matching device was found
if data[1]:
# Get the 'store_id' from the matching device
store_id = data[1][0]["store_id"]
return get_store_employees(store_id)
return []
def get_store_employees(store_id):
"""
Get all employees of a store
"""
supabase = get_client()
data, _ = supabase.table("users").select("*").eq("store_id", store_id).execute()
employees = []
for employee in data[1]:
employee = User(
employee["full_name"],
employee["email"],
employee["phone_number"],
employee["is_admin"],
employee["store_id"],
employee["id"],
)
employees.append(employee)
return employees
def get_store_admins(store_id):
"""
Get those employees of a store that have admin permissions
"""
supabase = get_client()
data, _ = (
supabase.table("users")
.select("*")
.eq("store_id", store_id)
.eq("is_admin", "TRUE")
.execute()
)
admins = []
for admin in data[1]:
admin = User(
admin["full_name"],
admin["email"],
admin["phone_number"],
admin["is_admin"],
admin["store_id"],
admin["id"],
)
admins.append(admin)
return admins
def add_banned_person(banned_person: BannedPerson, image):
"""
Ban a person from a store. If is private is true, they are only banned from the
original reporting store. Verification is required to ban them from other stores.
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person")
.insert(
{
"full_name": banned_person.full_name,
"license": banned_person.drivers_license,
"est_value_stolen": banned_person.est_value_stolen,
"reporting_store_id": banned_person.reporting_store_id,
"report_date": banned_person.report_date,
"is_private": banned_person.is_private,
"description": banned_person.description,
}
)
.execute()
)
banned_person_id = data[1][0]["id"]
banned_person_image = BannedPersonImage(banned_person_id, image)
add_banned_person_image(banned_person_image)
banned_person = BannedPerson(
data[1][0]["full_name"],
data[1][0]["license"],
data[1][0]["est_value_stolen"],
data[1][0]["reporting_store_id"],
data[1][0]["report_date"],
data[1][0]["is_private"],
data[1][0]["description"],
data[1][0]["id"],
)
return banned_person
def add_banned_person_image(banned_person_image: BannedPersonImage):
"""
Add an image to an existing banned person. A banned person can have multiple images.
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person_images")
.insert(
{
"banned_person_id": banned_person_image.banned_person_id,
"image": banned_person_image.image,
}
)
.execute()
)
banned_person_image = BannedPersonImage(
data[1][0]["banned_person_id"], data[1][0]["image"], data[1][0]["id"]
)
return banned_person_image
def remove_banned_person_by_id(banned_person_id):
"""
Remove a banned person from the database.
This is set to cascade meaning that the images are removed as well
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person").delete().eq("id", banned_person_id).execute()
)
banned_person_image = BannedPerson(
data[1][0]["full_name"],
data[1][0]["license"],
data[1][0]["est_value_stolen"],
data[1][0]["reporting_store_id"],
data[1][0]["report_date"],
data[1][0]["is_private"],
data[1][0]["description"],
data[1][0]["id"],
)
return banned_person_image
def get_banned_person(banned_person_id):
"""
Get a banned person by their id
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person").select("*").eq("id", banned_person_id).execute()
)
banned_person = BannedPerson(
data[1][0]["full_name"],
data[1][0]["license"],
data[1][0]["est_value_stolen"],
data[1][0]["reporting_store_id"],
data[1][0]["report_date"],
data[1][0]["is_private"],
data[1][0]["description"],
data[1][0]["id"],
)
return banned_person
def get_all_banned_people():
"""
Get all banned people
"""
supabase = get_client()
data, _ = supabase.table("banned_person").select("*").execute()
banned_people = []
for banned_person in data[1]:
banned_person = BannedPerson(
banned_person["full_name"],
banned_person["license"],
banned_person["est_value_stolen"],
banned_person["reporting_store_id"],
banned_person["report_date"],
banned_person["is_private"],
banned_person["description"],
banned_person["id"],
)
banned_people.append(banned_person)
return banned_people
def get_people_banned_by_store(store_id):
"""
Get all people banned by a store
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person")
.select("*")
.eq("reporting_store_id", store_id)
.execute()
)
banned_people = []
for banned_person in data[1]:
banned_person = BannedPerson(
banned_person["full_name"],
banned_person["license"],
banned_person["est_value_stolen"],
banned_person["reporting_store_id"],
banned_person["report_date"],
banned_person["is_private"],
banned_person["description"],
banned_person["id"],
)
banned_people.append(banned_person)
return banned_people
def get_banned_person_images(banned_person_id):
"""
Get all images of a banned person
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person_images")
.select("*")
.eq("banned_person_id", banned_person_id)
.execute()
)
banned_person_images = []
for banned_person_image in data[1]:
banned_person_image = BannedPersonImage(
banned_person_image["banned_person_id"],
banned_person_image["image"],
banned_person_image["id"],
)
banned_person_images.append(banned_person_image)
return banned_person_images
def get_all_banned_person_images():
"""
Get all images of banned people. Used for the model training
"""
supabase = get_client()
data, _ = supabase.table("banned_person_images").select("*").execute()
banned_person_images = []
for banned_person_image in data[1]:
banned_person_image = BannedPersonImage(
banned_person_image["banned_person_id"],
banned_person_image["image"],
banned_person_image["id"],
)
banned_person_images.append(banned_person_image)
return banned_person_images
def remove_banned_person_image_by_id(banned_person_id):
"""
Remove an image of a banned person by the image id
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person_images")
.delete()
.eq("id", banned_person_id)
.execute()
)
banned_person_image = BannedPersonImage(
data[1][0]["banned_person_id"], data[1][0]["image"], data[1][0]["id"]
)
return banned_person_image
def update_banned_person(banned_person: BannedPerson):
"""
Update a banned person
"""
supabase = get_client()
data, _ = (
supabase.table("banned_person")
.update(
{
"full_name": banned_person.full_name,
"license": banned_person.drivers_license,
"est_value_stolen": banned_person.est_value_stolen,
"reporting_store_id": banned_person.reporting_store_id,
"report_date": banned_person.report_date,
"is_private": banned_person.is_private,
"description": banned_person.description,
}
)
.eq("id", banned_person.id)
.execute()
)
banned_person = BannedPerson(
data[1][0]["full_name"],
data[1][0]["license"],
data[1][0]["est_value_stolen"],
data[1][0]["reporting_store_id"],
data[1][0]["report_date"],
data[1][0]["is_private"],
data[1][0]["description"],
data[1][0]["id"],
)
return banned_person
# add conviction
# add event to person
def database_log(log: Logging):
"""
Add a row in the logging database
"""
supabase = get_client()
data, _ = (
supabase.table("logging")
.insert(
{
"severity": log.severity,
"message": log.message,
"device_id": log.device_id,
}
)
.execute()
)
log = Logging(
data[1][0]["device_id"],
data[1][0]["severity"],
data[1][0]["message"],
data[1][0]["id"],
)
return log
def log_alert(alert: Alert):
"""
Add a row in the logging database
"""
supabase = get_client()
data, _ = (
supabase.table("alerts").insert(
[
{
"banned_person_id": alert.banned_person_id,
"banned_person_image": alert.banned_person_image,
"matched_frame": alert.matched_frame,
"description": alert.description,
"alerted_store": alert.alerted_store,
}
]
).execute()
)
alert = Alert(
data[1][0]["alert_id"],
data[1][0]["banned_person_id"],
data[1][0]["banned_person_image"],
data[1][0]["matched_frame"],
data[1][0]["timestamp"],
data[1][0]["description"],
data[1][0]["alerted_store"],
)
return alert