This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
748 lines (578 loc) · 22 KB
/
database.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
from collections import defaultdict
import datetime
import os
import sqlalchemy
import sqlalchemy.orm
import cloudinary
import cloudinary.uploader
import models
from flask import session as sesh
# ----------------------------------------------------------------------
DATABASE_URL = os.getenv("TEST_DB_URL")
engine = sqlalchemy.create_engine(DATABASE_URL)
# ----------------------------------------------------------------------
def get_spaces():
with sqlalchemy.orm.Session(engine) as session:
space_query = session.query(models.Space).filter(
models.Space.approved == "true"
)
photos_query = session.query(models.Photo)
amenities_query = session.query(models.Amenity)
spaces = [space.to_json() for space in space_query.all()]
photos = [photo.to_json() for photo in photos_query.all()]
amenities = [amenity.to_json() for amenity in amenities_query.all()]
# add amenities to each space object. This runs
# so much faster than iterating through in models.py
hm = defaultdict(list)
for amenity in amenities:
if amenity["spaceid"]:
hm[amenity["spaceid"]].append(amenity["amenity"])
for space in spaces:
space.update({"amenities": hm[space["id"]]})
data = {"spaces": spaces, "photos": photos}
return data
def get_space(name):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Space).filter(models.Space.name == name)
table = query.first()
return table
def get_details(id):
with sqlalchemy.orm.Session(engine) as session:
space = session.query(models.Space).get(id)
reviews = (
session.query(models.Review)
.filter(models.Review.space_id == space.id)
.all()
)
photos = (
session.query(models.Photo).filter(models.Photo.space_id == space.id).all()
)
amenities = (
session.query(models.Amenity)
.filter(models.Amenity.space_id == space.id)
.all()
)
space = space.to_json()
reviews = [review.to_json() for review in reviews]
photos = [photo.to_json() for photo in photos]
amenities = [amenity.to_json() for amenity in amenities]
details = {
"space": space,
"reviews": reviews,
"photos": photos,
"amenities": amenities,
}
return details
def add_space(puid, name, capacity, location, type, approved=True):
return_id = None
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Space).filter(
models.Space.user_id == puid, models.Space.name == name
)
table = query.all()
if table:
# return existing space id
return False, table[0].id
new_space = models.Space(
user_id=puid,
name=name,
capacity=capacity,
location=location,
type=type,
approved=approved,
)
session.add(new_space)
session.commit()
return_id = new_space.id
return True, return_id
def update_space(space_id, dict_of_changes):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Space).filter(models.Space.id == space_id)
table = query.all()
if table:
query.update(dict_of_changes, synchronize_session=False)
ret = f"space with id '{space_id}' updated"
session.commit()
return ret
def update_space_helper(
space, rating, noisiness, privacy, lighting, cleanliness, amenities_rating, adding
):
mapping = {
"rating": rating,
"noisiness": noisiness,
"privacy": privacy,
"lighting": lighting,
"cleanliness": cleanliness,
"amenities_rating": amenities_rating,
}
if space["numreviews"] is None or space["numreviews"] < 0:
numreviews = 1
else:
if adding:
numreviews = space["numreviews"] + 1
else:
numreviews = space["numreviews"] - 1
for key, value in mapping.items():
if value is not None:
if numreviews <= 0:
mapping[key] = None
elif space[key]:
# equation from https://stackoverflow.com/a/53618572/15561634
old_avg = space[key]
effect_of_val = (value - old_avg) / numreviews
if adding:
mapping[key] = old_avg + effect_of_val
else:
mapping[key] = old_avg - effect_of_val
else:
# if no previous average, start a new one
mapping[key] = value
return {
"numreviews": numreviews,
"rating": mapping["rating"],
"avgnoise": mapping["noisiness"],
"avgprivacy": mapping["privacy"],
"avglighting": mapping["lighting"],
"avgcleanliness": mapping["cleanliness"],
"avgamenities": mapping["amenities_rating"],
}
def update_space_from_review(
space_id,
rating,
noisiness,
privacy,
lighting,
cleanliness,
amenities_rating,
adding,
):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Space).filter(models.Space.id == space_id)
table = query.all()
space = table[0].to_json()
dict_of_changes = update_space_helper(
space,
rating,
noisiness,
privacy,
lighting,
cleanliness,
amenities_rating,
adding,
)
if table:
query.update(dict_of_changes, synchronize_session=False)
ret = f"space with id '{space_id}' updated"
session.commit()
return ret
def remove_space(space_id):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Space).filter(models.Space.id == space_id)
table = query.all()
if table:
query.delete(synchronize_session=False)
ret = f"deleted space with id {space_id}"
else:
ret = f"space with id {space_id} does not exist"
session.commit()
return ret
# returns spaces created by a specific user
def get_user_spaces(puid):
with sqlalchemy.orm.Session(engine) as session:
# query for all spaces that match a user_id
table = session.query(models.Space).filter(models.Space.user_id == puid).all()
# table is a list of {space_id: space_id}
space_ids = [t.id for t in table]
# query for all spaces that match a space_id
space_query = session.query(models.Space).filter(models.Space.id.in_(space_ids))
photos_query = session.query(models.Photo).filter(
models.Photo.space_id.in_(space_ids)
)
spaces = space_query.all()
photos = photos_query.all()
data = {
"spaces": [item.to_json() for item in spaces],
"photos": [item.to_json() for item in photos],
}
return data
def get_locations():
with sqlalchemy.orm.Session(engine) as session:
table = session.query(models.Space.location).distinct().all()
locations = [location for (location,) in table]
return locations
def get_types():
with sqlalchemy.orm.Session(engine) as session:
table = session.query(models.Space.type).distinct().all()
# some of the EMS location types have hyphens, so we'll remove them
# here, but they should really be removed when seeding
types = [type.split(" - ")[0] for (type,) in table]
return list(set(types)) # keep only unique types
# ----------------------------------------------------------------------
def get_user(puid):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.User).filter(models.User.puid == puid)
table = query.all()
return table
def post_user(puid):
ret = ""
admins = ["tb19", "chenhanz", "evesely", "tbegum", "tgdinh", "xl2493", 'mtouil', 'daphnegb', 'dpw']
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.User).filter(models.User.puid == puid)
table = query.all()
if table:
ret = "puid already in db"
else:
if puid in admins:
new_user = models.User(puid=puid, admin=True)
else:
new_user = models.User(puid=puid)
session.add(new_user)
session.commit()
ret = "user created"
return ret
# ----------------------------------------------------------------------
# Functions for favoriting
# ----------------------------------------------------------------------
# returns spaces that are favorited
def get_favorites(puid):
with sqlalchemy.orm.Session(engine) as session:
table = (
session.query(models.Favorite).filter(models.Favorite.user_id == puid).all()
)
# table is a list of {space_id: space_id}
space_ids = [t.space_id for t in table]
# query for all spaces that match a space_id
space_query = session.query(models.Space).filter(models.Space.id.in_(space_ids))
photos_query = session.query(models.Photo).filter(
models.Photo.space_id.in_(space_ids)
)
spaces = space_query.all()
photos = photos_query.all()
data = {
"spaces": [item.to_json() for item in spaces],
"photos": [item.to_json() for item in photos],
}
return data
# returns whether or not a user has favorited a specific space
def get_favorite(puid, space_id):
with sqlalchemy.orm.Session(engine) as session:
table = (
session.query(models.Favorite)
.filter(
models.Favorite.user_id == puid, models.Favorite.space_id == space_id
)
.all()
)
return len(table) == 1
def post_favorite(puid, space_id):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Favorite).filter(
models.Favorite.user_id == puid, models.Favorite.space_id == space_id
)
table = query.all()
if table:
# item is already favorited, unfavorite it
query.delete(synchronize_session=False)
ret = "space already favorited, removing from table"
else:
new_fav = models.Favorite(user_id=puid, space_id=space_id)
session.add(new_fav)
ret = f"favorited space with spaceid={space_id}"
session.commit()
return ret
# -----------------------------------------------------
# Functions for report
# -----------------------------------------------------
# Get all reports in the database.
def get_reports():
with sqlalchemy.orm.Session(engine) as session:
table = session.query(models.Report).all()
reports = [report.to_json() for report in table]
space_ids = [rep["review"]["spaceid"] for rep in reports]
spaces = (
session.query(models.Space)
.filter(models.Space.id.in_(space_ids))
.with_entities(models.Space.id, models.Space.name)
.all()
)
hm = {spaceid: space_name for (spaceid, space_name) in spaces}
for report in reports:
report.update({"space_name": hm[report["review"]["spaceid"]]})
return reports
# Any user can add a report for a review (review_id)
def add_report(user_id=None, review_id=None, content=None):
report_id = None
date = datetime.datetime.now()
with sqlalchemy.orm.Session(engine) as session:
new_report = models.Report(
user_id=user_id,
review_id=review_id,
content=content,
date=date,
)
session.add(new_report)
ret = f"created report for review {review_id} by user {user_id} with "
ret += f"content: {content} at date: {date}"
session.commit()
print(ret)
report_id = new_report.id
return report_id
# A user (user_id) can delete a report for a review (review_id) if the user is admin
def delete_report(report_id, user_id, admin):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Report).filter(models.Report.id == report_id)
table = query.all()
# add verification for permissions on delete.
if not table:
ret = f"report with id {report_id} does not exist"
elif admin:
query.delete(synchronize_session=False)
ret = f"deleted review with id {report_id}"
else:
ret = f"{user_id} has insufficient permissions to delete report with report_id {report_id}"
session.commit()
return ret
# -----------------------------------------------------
# Functions for moderation
# -----------------------------------------------------
def get_awaiting_approval():
with sqlalchemy.orm.Session(engine) as session:
# models.Space.approved is False doesn't work, but == False does:
query = session.query(models.Space).filter(models.Space.approved == False)
table = query.all()
space_ids = [t.id for t in table]
# query for all photos that match a space_id
photos_query = session.query(models.Photo).filter(
models.Photo.space_id.in_(space_ids)
)
spaces = table
photos = photos_query.all()
data = {
"spaces": [item.to_json() for item in spaces],
"photos": [item.to_json() for item in photos],
}
return data
def check_user_admin():
if "username" in sesh:
user_id = sesh.get("username")
else:
return False
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.User).filter(models.User.puid == user_id)
table = query.all()
if table:
return table[0].admin
else:
return False
def handle_approval(space_id, approval):
if approval:
with sqlalchemy.orm.Session(engine) as session:
space = session.query(models.Space).get(space_id)
space.approved = True
ret = f"space {space.name} approved"
session.commit()
else:
remove_space(space_id)
ret = f"space {space_id} deleted"
return ret
# -----------------------------------------------------
# Functions for reviews
# -----------------------------------------------------
# get the reviews associated with a space specified by space_id
def get_reviews(space_id=None, puid=None):
with sqlalchemy.orm.Session(engine) as session:
if space_id:
q = session.query(models.Review).filter(models.Review.space_id == space_id)
elif puid:
q = session.query(models.Review).filter(models.Review.user_id == puid)
table = q.all()
reviews = [review.to_json() for review in table]
space_ids = [rev["spaceid"] for rev in reviews]
spaces = (
session.query(models.Space)
.filter(models.Space.id.in_(space_ids))
.with_entities(models.Space.id, models.Space.name)
.all()
)
hash_map = dict(spaces)
for review in reviews:
review.update({"space_name": hash_map[review["spaceid"]]})
return reviews
else:
q = session.query(models.Review)
table = q.all()
reviews = [review.to_json() for review in table]
return reviews
# add a review consisting of rating and content
def add_review(
space_id,
puid,
rating,
content,
noisiness,
privacy,
lighting,
cleanliness,
amenities_rating,
):
review_id = None
with sqlalchemy.orm.Session(engine) as session:
new_review = models.Review(
space_id=space_id,
user_id=puid,
rating=rating,
content=content,
noisiness=noisiness,
privacy=privacy,
lighting=lighting,
cleanliness=cleanliness,
amenities_rating=amenities_rating,
)
session.add(new_review)
ret = f"created review for space {space_id} by user {puid} with "
ret += f"rating {rating} and content: {content}"
session.commit()
print(ret)
review_id = new_review.id
return review_id
# update a review for space with space_id by user with id of user_id
def update_review(review_id, user_id, dict_of_changes):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Review).filter(models.Review.id == review_id)
table = query.all()
if not table:
ret = f"no review with id {review_id} exists"
elif table[0].user_id == user_id: # permission handling
query.update(dict_of_changes, synchronize_session=False)
ret = f"review with id '{review_id}' updated"
else:
ret = f"{user_id} has insufficient permissions to udpate review with id {review_id}"
session.commit()
return ret
# delete a review for space with space_id created by user specified by user_id
def remove_review(review_id, user_id, admin):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Review).filter(models.Review.id == review_id)
table = query.all()
# add verification for permissions on delete.
if not table:
ret = f"review with id {review_id} does not exist"
elif table[0].user_id == user_id or admin:
update_space_from_review(
table[0].space_id,
table[0].rating,
table[0].noisiness,
table[0].privacy,
table[0].lighting,
table[0].cleanliness,
table[0].amenities_rating,
False,
)
query.delete(synchronize_session=False)
ret = f"deleted review with id {review_id}"
else:
ret = f"{user_id} has insufficient permissions to delete review with id {review_id}"
session.commit()
return ret
# ----------------------------------------------------------------------
def get_tags():
with sqlalchemy.orm.Session(engine) as session:
table = session.query(models.Tag.tag).distinct().all()
tags = [tag for (tag,) in table]
return tags
def add_tag(tag, space_id=None, review_id=None):
with sqlalchemy.orm.Session(engine) as session:
new_tag = models.Tag(space_id=space_id, review_id=review_id, tag=tag)
session.add(new_tag)
ret = f"added tag '{tag}'"
session.commit()
print(ret)
return ret
def remove_tag(tag_id):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Tag).filter(models.Tag.id == tag_id)
table = query.first()
if table:
query.delete(synchronize_session=False)
ret = f"deleted tag with id {tag_id}"
else:
ret = f"tag with id {tag_id} does not exist"
session.commit()
print(ret)
return ret
# ----------------------------------------------------------------------
def get_amenities():
with sqlalchemy.orm.Session(engine) as session:
table = session.query(models.Amenity.amenity).distinct().all()
amenities = [amenity for (amenity,) in table]
return amenities
def add_amenity(amenity, space_id=None, review_id=None):
with sqlalchemy.orm.Session(engine) as session:
if space_id:
query = session.query(models.Amenity).filter(
models.Amenity.space_id == space_id, models.Amenity.amenity == amenity
)
table = query.all()
if table:
return (
f"amenity '{amenity}' for space with id {space_id} already exists"
)
new_amenity = models.Amenity(
space_id=space_id, review_id=review_id, amenity=amenity
)
session.add(new_amenity)
ret = f"added amenity '{amenity}' for space with id {space_id}"
session.commit()
print(ret)
return ret
def remove_amenity(amenity_id):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Amenity).filter(models.Amenity.id == amenity_id)
table = query.first()
if table:
query.delete(synchronize_session=False)
ret = f"deleted amenity with id {amenity_id}"
else:
ret = f"amenity with id {amenity_id} does not exist"
session.commit()
print(ret)
return ret
# ----------------------------------------------------------------------
def upload_photo_to_cloudinary(src):
cloudinary.config(
cloud_name=os.getenv("CLOUD_NAME"),
api_key=os.getenv("CLOUD_API_KEY"),
api_secret=os.getenv("CLOUD_API_SECRET"),
)
upload_result = cloudinary.uploader.upload(
src, resource_type="raw", folder="SpaceTiger/photos/"
)
return upload_result["url"]
def add_photo(space_id, src, review_id=None):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Photo).filter(
models.Photo.space_id == space_id, models.Photo.src == src
)
table = query.all()
if table:
ret = f"photo with given src for space with id {space_id} already exists"
else:
new_photo = models.Photo(space_id=space_id, review_id=review_id, src=src)
session.add(new_photo)
ret = f"added photo with given src for space with id {space_id}"
session.commit()
return ret
def remove_photo(photo_id):
with sqlalchemy.orm.Session(engine) as session:
query = session.query(models.Photo).filter(models.Photo.id == photo_id)
table = query.first()
if table:
query.delete(synchronize_session=False)
ret = f"deleted photo with id {photo_id}"
else:
ret = f"photo with id {photo_id} does not exist"
session.commit()
return ret
# ----------------------------------------------------------------------
if __name__ == "__main__":
pass