-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
339 lines (309 loc) · 10.7 KB
/
db.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
import pymongo
from flask_login import UserMixin
from constants import ATLAS_ADMIN_PWD
from datetime import datetime
connectionURL = (
"mongodb+srv://adhiAtlasAdmin:%[email protected]/myFirstDatabase?retryWrites=true&w=majority"
% ATLAS_ADMIN_PWD
)
dbclient = pymongo.MongoClient(connectionURL)
db = dbclient.get_database("Reception")
UsersCollection = db.Users
RoomsCollection = db.Rooms
ParticipantsCollection = db.Participants
class User(UserMixin):
def __init__(self, id_, name, first_name, email, profile_pic):
self.id = id_
self.name = name
self.first_name = first_name
self.email = email
self.profile_pic = profile_pic
# Fetch user using id
@staticmethod
def get(user_id):
user = UsersCollection.find_one({"userID": user_id})
if not user:
return None
user = User(
user["_id"],
user["name"],
user["first_name"],
user["email"],
user["profile_pic"],
)
return user
# Fetch user using id
@staticmethod
def getUserByEmail(email, projection=None):
userDetails = UsersCollection.find_one({"email": email}, projection)
if not userDetails:
return None
return userDetails
# Create a new user
@staticmethod
def create(id_, name, first_name, email, profile_pic):
UsersCollection.insert_one(
{
"userID": id_,
"name": name,
"first_name": first_name,
"email": email,
"profile_pic": profile_pic,
}
)
class Rooms:
# Fetch room details using room ID
@staticmethod
def getRoomByID(room_id, projection=None):
room = RoomsCollection.find_one(
{"_id": room_id}, projection=projection
)
print("Room Details:", room)
return room
# Fetch all rooms created by a user
@staticmethod
def getRoomsByCreator(email, projection=None):
rooms = RoomsCollection.find(
{"creator": email},
sort=[("start_date", pymongo.ASCENDING)],
projection=projection,
)
return list(rooms)
# Create a new room
@staticmethod
def createRoom(roomDetails):
RoomsCollection.insert_one(roomDetails)
class Participants:
@staticmethod
def getJoiningDetails(room_id, email):
roomDetails = RoomsCollection.find_one({"_id": room_id})
interviewerDetails = UsersCollection.find_one(
{"email": roomDetails["creator"]},
{"name": 1, "profile_pic": 1},
)
participantDetails = ParticipantsCollection.find_one(
{"email": email, "roomID": room_id},
{
"windowLowerBound": 1,
"windowUpperBound": 1,
"status": 1,
"queuePosition": 1,
},
)
participantDetails["interviewer_name"] = interviewerDetails["name"]
participantDetails["interviewer_profile_pic"] = interviewerDetails[
"profile_pic"
]
participantDetails.update(roomDetails)
return participantDetails
# List of rooms where the user is a participant
@staticmethod
def getRoomsByParticipant(email):
rooms = ParticipantsCollection.aggregate(
[
{"$match": {"email": email}},
{"$project": {"roomID": 1, "_id": 0}},
{
"$lookup": {
"from": "Rooms",
"localField": "roomID",
"foreignField": "_id",
"as": "roomID",
}
},
{"$unwind": {"path": "$roomID"}},
{
"$match": {
"roomID.start_date": {
"$gte": datetime.utcnow().replace(
hour=0, minute=0, second=0
)
}
}
},
{
"$sort": {
"roomID.start_date": pymongo.ASCENDING,
}
},
]
)
return rooms
# List of participants in a room
@staticmethod
def getInvitedParticipantsInRoom(room_id):
invited_participants = ParticipantsCollection.aggregate(
[
{"$match": {"roomID": room_id, "queuePosition": -1}},
{
"$lookup": {
"from": "Users",
"localField": "email",
"foreignField": "email",
"as": "user",
}
},
{"$unwind": "$user"},
{
"$project": {
"_id": 0,
"user.email": "$user.email",
"user.name": "$user.name",
"user.profile_pic": "$user.profile_pic",
}
},
{"$sort": {"queuePosition": pymongo.ASCENDING}},
]
)
# Additionally sort by invite timestamp later
return list(invited_participants)
@staticmethod
def getUnInvitedParticipantsInRoom(room_id):
uninvited_participants = ParticipantsCollection.aggregate(
[
{"$match": {"roomID": room_id, "queuePosition": {"$gt": 0}}},
{
"$lookup": {
"from": "Users",
"localField": "email",
"foreignField": "email",
"as": "user",
}
},
{"$unwind": "$user"},
{
"$project": {
"_id": 0,
"queuePosition": 1,
"user.email": "$user.email",
"user.name": "$user.name",
"user.profile_pic": "$user.profile_pic",
}
},
{"$sort": {"queuePosition": pymongo.ASCENDING}},
]
)
return list(uninvited_participants)
@staticmethod
def getAllParticipantsInRoom(room_id):
participants = ParticipantsCollection.aggregate(
[
{"$match": {"roomID": room_id}},
{
"$lookup": {
"from": "Users",
"localField": "email",
"foreignField": "email",
"as": "user",
}
},
{"$unwind": "$user"},
{
"$project": {
"_id": 0,
"queuePosition": 1,
"user.email": "$user.email",
"user.name": "$user.name",
"user.profile_pic": "$user.profile_pic",
}
},
{"$sort": {"queuePosition": pymongo.ASCENDING}},
]
)
return list(participants)
@staticmethod
def getParticipantsEmailsByRoom(room_id):
# Find list of participants in a room
participants = ParticipantsCollection.find(
{"roomID": room_id}, {"_id": 0, "email": 1}
)
result = []
for participant in participants:
result.append(participant["email"])
return result
# Add participants to a room
@staticmethod
def addParticipants(room_id, emails):
documents = []
queuePosition = 1
for email in emails:
documents.append(
{
"roomID": room_id,
"email": email,
"status": "open",
"windowLowerBound": None,
"windowUpperBound": None,
"queuePosition": queuePosition,
"notifiedByWebsite": False,
"notifiedByEmail": False,
}
)
queuePosition += 1
ParticipantsCollection.insert_many(documents)
@staticmethod
def ifParticipantInRoom(room_id, email):
"""
Checks if participant is in the room.
"""
return ParticipantsCollection.find_one(
{"roomID": room_id, "email": email}
)
@staticmethod
def removeParticipantFromQueue(room_id, email):
present_queue_position = ParticipantsCollection.find(
{"roomID": room_id, "email": email}, {"queuePosition": 1}
)
present_queue_position = present_queue_position[0]["queuePosition"]
if present_queue_position != -1:
ParticipantsCollection.update_many(
{
"roomID": room_id,
"queuePosition": {"$gt": present_queue_position},
},
{"$inc": {"queuePosition": -1}},
)
ParticipantsCollection.update_one(
{"roomID": room_id, "email": email},
{"$set": {"queuePosition": -1}},
)
return True
else:
return False
@staticmethod
def addInviteTimestamp(room_id, email):
ParticipantsCollection.update_one(
{"roomID": room_id, "email": email},
{"$set": {"inviteTimeStamp": datetime.utcnow()}},
)
return True
@staticmethod
def reorderParticipants(room_id, email, new_position):
present_queue_position = ParticipantsCollection.find(
{"roomID": room_id, "email": email}, {"queuePosition": 1}
)
present_queue_position = present_queue_position[0]["queuePosition"]
if present_queue_position != -1:
ParticipantsCollection.update_many(
{
"roomID": room_id,
"queuePosition": {"$gt": present_queue_position},
},
{"$inc": {"queuePosition": -1}},
)
ParticipantsCollection.update_many(
{"roomID": room_id, "queuePosition": {"$gte": new_position}},
{"$inc": {"queuePosition": 1}},
)
ParticipantsCollection.update_one(
{"roomID": room_id, "email": email},
{"$set": {"queuePosition": new_position}},
)
return True
return False
@staticmethod
def getQueuePosition(room_id, email):
return ParticipantsCollection.find_one(
{"roomID": room_id, "email": email},
projection={"queuePosition": 1, "_id": 0},
)