-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_mongodb.py
197 lines (156 loc) · 5.48 KB
/
event_mongodb.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
#A rewrite of the database interfaces using mongodb
from pymongo import MongoClient
from passlib.hash import sha256_crypt
import uuid
class User():
def __init__(self, mongo_doc):
self.user_id = mongo_doc["user_id"]
self.pass_hash = mongo_doc["pass_hash"]
self.username = mongo_doc["username"]
self.admin = mongo_doc["admin"]
self.story_dict = mongo_doc["story_dict"]
self.is_authenticated = mongo_doc["is_authenticated"]
self.is_active = True #unnessecary for loom
self.is_anonymous = False #unessecary for loom
def get_id(self):
return self.user_id
def get_client(self, story_id):
if story_id in self.story_dict.keys():
return self.story_dict[story_id]
else:
return None
def validate_pass(self, password):
self.is_authenticated = sha256_crypt.verify(password, self.pass_hash)
return self.is_authenticated
def change_pass(self, new_pass):
self.pass_hash = sha256_crypt.encrypt(new_pass)
return True
def to_doc(self):
return {
"username":self.username,
"pass_hash":self.pass_hash,
"user_id":self.user_id,
"story_dict":self.story_dict,
"admin":self.admin,
"is_authenticated":self.is_authenticated,
}
#only returns information of interest to the browser
def to_json(self):
return {
"username":self.username,
"user_id":self.user_id,
"story_dict":self.story_dict,
"admin":self.admin,
}
def __repr__(self):
return str(self.to_doc())
def clean_mongo_doc(doc):
#the mongo-internal object id is irrelevant
if doc is None:
return doc
else:
del doc["_id"]
return doc
class RootCollection():
def __init__(self, db):
self.db = db
self.stories = db.stories
self.users = db.users
self.codes = db.codes
def add_story(self, story_doc):
self.stories.insert_one(story_doc)
def get_story(self, story_id):
return clean_mongo_doc(self.stories.find_one({"story_id":story_id}))
def update_story(self, story_doc):
self.stories.replace_one({"story_id":story_doc["story_id"]}, story_doc)
def get_all_stories(self):
return [clean_mongo_doc(item) for item in self.stories.find()]
###Users
def new_user(self, username, password, admin=False):
doc = {
"username":username,
"pass_hash":sha256_crypt.encrypt(password),
"user_id":str(uuid.uuid4()),
"story_dict":{},
"admin":admin,
"is_authenticated":False
}
#check if username exists in db yet
if self.users.find({"username":username}).count() == 0:
self.users.insert_one(doc)
user = User(doc)
return True, user
else:
return False, self.get_user(username)
def get_user(self, username):
got = self.users.find_one({"username":username})
if got is not None:
return User(got)
else:
return None
def get_user_by_id(self, user_id):
#this is just for the flask-login mixin
got = self.users.find_one({"user_id":user_id})
if got is not None:
return User(got)
else:
return None
def get_all_users(self):
return [User(doc) for doc in self.users.find()]
def save_user(self, User):
self.users.replace_one({"user_id":User.user_id}, User.to_doc())
return User
###Invitation Codes
def add_code(self, code, story):
self.codes.insert({"code":code, "story_id":story, "used":False, "used_by":None})
def get_code(self, code):
return self.codes.find_one({"code":code})
def get_story_codes(self,story_id):
return [clean_mongo_doc(code) for code in self.codes.find({"story_id":story_id})]
def update_code(self, code_doc):
self.codes.replace_one({"code":code_doc["code"]}, code_doc)
class StoryCollection():
def __init__(self, db, story_id):
self.story_id = story_id
self.db = db
self.events = db.events
self.clients = db.clients
self.passages = db.passages
def add_client(self, client_doc):
client_doc["story_id"] = self.story_id
self.clients.insert_one(client_doc)
def update_client(self, client_doc):
self.clients.replace_one({"story_id":self.story_id, "client_id":client_doc["client_id"]}, client_doc)
def add_passage(self, passage_doc):
passage_doc["story_id"] = self.story_id
self.passages.insert_one(passage_doc)
def add_event(self, event_doc):
event_doc["story_id"] = self.story_id
self.events.insert_one(event_doc)
def get_all_events(self):
return [clean_mongo_doc(item) for item in self.events.find({"story_id":self.story_id})]
def get_all_clients(self):
as_list = [clean_mongo_doc(item) for item in self.clients.find({"story_id":self.story_id})]
return as_list
def get_all_passages(self):
return [clean_mongo_doc(item) for item in self.passages.find({"story_id":self.story_id})]
def get_client(self, client_id):
return clean_mongo_doc(self.clients.find_one({"story_id":self.story_id, "client_id":client_id}))
def get_all_client_events(self, client_id):
all_client_events = self.events.find({"story_id": self.story_id, "client_id":client_id})
as_list = [clean_mongo_doc(item) for item in all_client_events]
return as_list
def get_current_client_location_event(self, client_id):
client_events = self.get_all_client_events(client_id)
if len(client_events) != 0:
return max(client_events, key=lambda x: x["time"])
else:
return None
def get_all_current_client_location_events(self, get_exited=False):
all_current_events = [{"event": self.get_current_client_location_event(client["client_id"]), "client":client}
for client in self.get_all_clients()]
all_current_events = [event for event in all_current_events if event["event"] is not None]
if get_exited:
return all_current_events
else:
return list(filter(lambda x: (x["event"]["passage_id"] != "event:exit"), all_current_events))