-
Notifications
You must be signed in to change notification settings - Fork 2
/
dal.py
62 lines (45 loc) · 1.76 KB
/
dal.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
"""
This thingy is Mr. DAL, everyone's favorite Data Access Layer!
"""
import datetime
from mongoengine import *
class PrintableDocument(Document):
meta = {'abstract': True}
def __repr__(self):
return f'<{type(self).__name__} {self.to_mongo().to_dict()}>'
class User(PrintableDocument):
name = StringField(required=True)
tid = StringField(required=True, unique=True)
class Pair(PrintableDocument):
is_active = BooleanField(default=True)
tid1 = StringField()
tid2 = StringField()
confidence1 = IntField(min_value=0, max_value=100)
confidence2 = IntField(min_value=0, max_value=100)
round_num = IntField(required=True)
start_time = DateTimeField(required=True, default=datetime.datetime.now)
end_time = DateTimeField()
turn = IntField(min_value=1, max_value=2, default=1)
class Message(PrintableDocument):
pair = ReferenceField(Pair, required=True)
sender = StringField()
message = StringField(required=True)
timestamp = DateTimeField(required=True, default=datetime.datetime.now)
def get_user_by_tid(tid):
try:
return User.objects(tid=str(tid))[0]
except IndexError as e:
return None
def get_name_by_tid(tid):
user = get_user_by_tid(tid)
if user is None:
return f'John Doe ({tid})'
return user['name']
def get_pair_by_tid(tid, is_active=True):
if is_active is None:
return Pair.objects(Q(tid1=str(tid)) | Q(tid2=str(tid)))
return Pair.objects(Q(is_active=is_active) & (Q(tid1=str(tid)) | Q(tid2=str(tid))))
def get_pair_by_tid_in_round(tid, round_num):
return Pair.objects(Q(round_num=int(round_num)) & (Q(tid1=str(tid)) | Q(tid2=str(tid))))
# establish connection to mongodb instance
db = connect('tinder_turing', host='localhost', port=27017)