-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoDB.py
More file actions
212 lines (199 loc) · 9.95 KB
/
mongoDB.py
File metadata and controls
212 lines (199 loc) · 9.95 KB
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
from backend import *
import pymongo
from pymongo import MongoClient
import uuid
from datetime import datetime
import os
from decouple import config
cluster = MongoClient(config('MongoDB_ClientURL'))
db = cluster["database"]
collection = db["rooms"]
Organisations = {}
taskID=0
# organisation1 = {"_id": 2, "userList": ["Anis", "John", "Yash"], "AdminList": ["Anis", "Yash"]}
# organisation2 = {"_id": 3, "userList": ["Ani", "Joh", "Yas"], "AdminList": ["Ani", "Yas"]}
# collection.insert_many([organisation1, organisation2])
# results = collection.find_one({"_id": 1})
# if(results==None):
# print("yup i am here")
# for result in results:
# print(result)
# collection.delete_one({"_id":2})
# collection.delete_one({"_id":3})
# collection.delete_one({"_id":1})
# def getUserID(username, groupID):
# organisation = collection.find_one({"_id": groupID})
# for userID in organisation["userList"].keys():
# if(organisation["userList"][userID].username == username):
# return userID
# return "User not registered or the username has been updated"
def register(userID, username, profile, isAdmin, groupID):
try:
user = User(userID, username, profile, isAdmin)
organisation = collection.find_one({"_id": groupID})
if(organisation==None):
collection.insert_one({"_id": groupID, "userList": {}, "adminList": {}, "tasks": {}, "bibliography": {}, "notifyMessages": {}})
organisation = collection.find_one({"_id": groupID})
for i in organisation.keys():
print(i)
# print(userID)
# print(user)
userID = str(userID)
# collection.update_one({"_id": groupID}, {"$set": {"userList": {userID: {}}}})
collection.update_one({"_id": groupID}, {"$set": {"userList."+userID :user.toDictionary()}})
return username + " is now registered as a "+ profile
except Exception as e:
print(e)
# user enters task id
def catch(groupID, userID, taskID):
organisation = collection.find_one({"_id": groupID})
if(organisation["userList"].has_key(str(userID))):
flag=0
memory_task = organisation["tasks"][0].clone()
for i in range(0, len(organisation["tasks"][0])):
if(memory_task[i].ID == taskID):
memory_task[i].assignedUser = organisation["userList"][userID]
memory_task[i].status = "assigned"
updated_taskList = organisation["tasks"][userID].append(memory_task[i]).clone()
collection.update_one({"_id": groupID}, {"$set": {organisation["tasks"][userID]:updated_taskList, organisation["tasks"][0][i]:memory_task[i]}})
flag=1
break
if(flag==0):
return "invalid task"
else:
return "You are not registered"
# Return the tasks
# attr is all
def task_list(attr, selfID, groupID):
organisation = collection.find_one({"_id": groupID})
if(attr=="all"):
# for task in organisation["tasks"][0]:
# print(task["ID"], " ", task["message"], " ", task["timeOfCreation"], " ", task["deadline"], " ", task["status"], " ", task["assignedUser"])
return organisation["tasks"][0]
elif(attr=="personal"):
# for task in organisation["tasks"][str(selfID)]:
# print(task["ID"], " ", task["message"], " ", task["timeOfCreation"], " ", task["deadline"], " ", task["status"], " ", task["assignedUser"])
return organisation["tasks"][str(selfID)]
else:
if(organisation["adminList"].has_key(str(selfID))):
# for task in organisation["tasks"][attr]:
# print(task["ID"], " ", task["message"], " ", task["timeOfCreation"], " ", task["deadline"], " ", task["status"], " ", task["assignedUser"])
return organisation["tasks"][attr]
else:
return "You are not the admin"
# to promte demote admin
def admin(param, selfID, userID, groupID):
organisation = collection.find_one({"_id": groupID})
if(organisation["adminList"].has_key(str(selfID))):
if(param=="promote"):
if(organisation["userList"].has_key(str(userID))):
user = convertToUser(organisation["userList"][str(userID)])
if organisation["userList"][str(userID)]["isAdmin"]==True:
return "User is already an Admin"
else:
collection.update_one({"_id": groupID}, {"$set": {organisation["adminList"][userID]:user, organisation["userList"][userID]:user}})
collection.update({"_id":groupID, "adminList":str(userID)}, {"$set": {"adminList".str(userID).isAdmin:True }})
return user.username + " has been promoted to Admin"
else:
return "No such user is present in this group"
elif(param=="demote"):
if(organisation["userList"].has_key(userID)):
if organisation["adminList"].has_key(userID):
user = organisation["userList"][userID]
user.isAdmin = True
collection.update_one({"_id": groupID}, {"$unset": {organisation["adminList"][userID]}})
collection.update_one({"_id": groupID}, {"$set": {organisation["userList"][userID]:user}})
return "User demoted from Admin"
else:
user = organisation["userList"][userID]
collection.update_one({"_id": groupID}, {"$unset": {organisation["userList"][userID]}})
return "User removed from group"
else:
return "No such user is present in this group"
elif(param=="remove"):
if(organisation["userList"].has_key(userID)):
if organisation["adminList"].has_key(userID):
collection.update_one({"_id": groupID}, {"$unset": {organisation["adminList"][userID]}})
collection.update_one({"_id": groupID}, {"$unset": {organisation["userList"][userID]}})
return "User removed from group"
else:
return "No such user is present in this group"
else:
return "You are not an admin"
def update_task(taskID, param, message, userID, groupID):#user can change the status of the task and give updates to the admin
organisation = collection.find_one({"_id": groupID})
for task in organisation["tasks"][0]:
if(task.ID == taskID):
if(task.status == "active"):
organisation["tasks"][0].remove(task)
organisation["tasks"][task.assignedUser.ID].remove(task)
task.userUpdates = param
organisation["tasks"][0].append(task)
organisation["tasks"][task.assignedUser.ID].append(task)
collection.update_one({"_id": groupID}, {"$set": {organisation["tasks"][0]: organisation["tasks"][0]}})
collection.update_one({"_id": groupID}, {"$set", {organisation["tasks"][task.assignedUser]: organisation["tasks"][task.assignedUser.ID]}})
return "Task updated successfully"
else:
return "Task is already completed"
return "Task is isn't created"
def add_task(message, deadline, userID, groupID):
global taskID
organisation = collection.find_one({"_id": groupID})
if(organisation["adminList"].has_key(userID)):
timeOfCreation = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
task = Task(taskID, None, message, timeOfCreation, deadline, "active", "")
taskID=taskID+1
organisation
organisation["tasks"][0].append(task)
collection.update_one({"_id": groupID}, {"$set": {organisation["tasks"][0]: organisation["tasks"][0]}})
return "Task added successfully"
else:
return "You are not an admin and cannot create a task"
def remove_task(taskID, userID, groupID):
organisation = collection.find_one({"_id": groupID})
if(organisation["adminList"].has_key(userID)):
for task in organisation["tasks"][0]:
if(task.ID == taskID):
organisation["tasks"][0].remove(task)
if(task.assignedUser!=None):
organisation["tasks"][task.assignedUser.ID].remove(task)
collection.update_one({"_id": groupID}, {"$set": {organisation["tasks"][task.assignedUser.ID]: organisation["tasks"][task.assignedUser.ID]}})
collection.update_one({"_id": groupID}, {"$set": {organisation["tasks"][0]: organisation["tasks"][0]}})
return "Task removed successfully"
return "Task not found"
else:
return "You are not an Admin and can't delete a task"
def update_bibliography(param, link, message, groupID):
organisation = collection.find_one({"_id": groupID})
if(param=="add"):
organisation["bibliography"][message].append(link)
collection.update_one({"_id": groupID}, {"$addToSet": {organisation["bibliography"][message]: link}})
elif(param=="remove"):
if(message==""):
for i in organisation["bibliography"]:
if(organisation["bibliography"][i]==link):
collection.update_one({"_id": groupID}, {"$unset": {organisation["bibliography"][i]}})
else:
collection.update_one({"_id": groupID}, {"$unset": {organisation["bibliography"][message]}})
def reminder(param, message):
pass
def notify(message):
return
# def createRoom(groupID, userList, adminList):
# try:
# insertion = {"_id" : groupID}
# userDict = {}
# adminDict = {}
# for user in userList:
# userDict[user.name] = user
# for admin in adminList:
# userDict[admin.name] = admin
# bibliography = {}
# notifyMessages = {}
# insertion["userList"] = userDict
# insertion["adminList"] = adminDict
# insertion["bibliography"] = bibliography
# insertion["notifyMessages"] = notifyMessages
# collection.insert_one(insertion)
# except Exception as e:
# print(e)