-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore.py
54 lines (41 loc) · 1.62 KB
/
store.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
from tinydb import TinyDB, Query
class TinyDBStore(object):
def __init__(self):
self.drafts_db = TinyDB('event_drafts.json')
self.events_db = TinyDB('events.json')
# Drafts
def contains_draft(self, user_id):
return self.drafts_db.contains(Query().user_id == user_id)
def new_draft(self, user_id):
if self.contains_draft(user_id):
self.drafts_db.remove(Query().user_id == user_id)
self.drafts_db.insert({
'user_id': user_id,
'current_field': 0,
'event': {}
})
def update_draft(self, user_id, event, current_field):
self.drafts_db.update({
'user_id': user_id,
'current_field': current_field,
'event': event
}, Query().user_id == user_id)
def get_draft(self, user_id):
return self.drafts_db.get(Query().user_id == user_id)
def remove_draft(self, user_id):
self.drafts_db.remove(Query().user_id == user_id)
# Events
def insert_event(self, event):
event_id = self.events_db.insert(event)
event['id'] = event_id
return event
def update_event(self, event):
self.events_db.update(event, eids=[event.eid])
def remove_event(self, event):
self.events_db.remove(eids=[event['id']])
def get_events(self, user_id, name=None):
if name:
return self.events_db.search((Query().user_id == user_id) & (Query().name.test(lambda v: name in v)))
return self.events_db.search(Query().user_id == user_id)
def get_event(self, event_id):
return self.events_db.get(eid=int(event_id))