-
Notifications
You must be signed in to change notification settings - Fork 0
/
booking_database.py
62 lines (51 loc) · 1.87 KB
/
booking_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
from tinydb import TinyDB, Query
from urllib.parse import urlparse, parse_qs
from google.cloud import storage
import user_database
storage_client = storage.Client()
bucket_name = "autotennis"
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob("bookings")
db = TinyDB("./bookingDB.json")
Booking = Query()
def sync():
blob.download_to_filename("./bookingDB.json")
def is_in_db(email, event_url):
if db.search((Booking.email == email) & (Booking.event_url == event_url)) == []:
return False
else:
return True
def is_recurring(email, event_url):
if db.search((Booking.email == email) & (Booking.event_url == event_url) & (Booking.recurring == True)) == []:
return False
else:
return True
def add_entry(email, event_url, recurring, type):
if not is_valid_url(event_url):
return
db.insert({'email': email, 'event_url': event_url, 'recurring': recurring,'type': type})
blob.upload_from_filename('./bookingDB.json')
def remove_entry(email, event_url):
db.remove((Booking.email == email) & (Booking.event_url == event_url))
blob.upload_from_filename('./bookingDB.json')
def get_all_entries():
return db.all()
def get_active_bookings_per_user():
active_bookings = {user: [] for user in user_database.get_registered_emails()}
for booking in get_all_entries():
active_bookings[booking['email']].append(booking['event_url'])
return active_bookings
def is_valid_url(event_url):
url_parsed = urlparse(event_url)
query = parse_qs(url_parsed.query)
if url_parsed.netloc != 'www.ntnuitennis.no':
return False
required_query = ['timeid', 'spilletid']
for q in required_query:
if q not in query.keys():
return False
allowed_query = ['timeid', 'spilletid', 'lang']
for q in query:
if q not in allowed_query:
return False
return True