-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.py
192 lines (156 loc) · 6.06 KB
/
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
import pymongo
from bson import ObjectId
class Model(dict):
"""
A simple model that wraps mongodb document
"""
__getattr__ = dict.get
__delattr__ = dict.__delitem__
__setattr__ = dict.__setitem__
def save(self):
if not self._id:
self.collection.insert(self)
else:
self.collection.update(
{"_id": ObjectId(self._id)}, self)
self._id = str(self._id)
def merge_dict(self, cur, old):
for field in old.keys():
if field not in cur:
cur.update({field: old[field]})
elif isinstance(old[field], dict) and (field in cur):
self.merge_dict(cur[field], old[field])
def patch(self):
if not self._id:
self.collection.insert(self)
else:
old_entry = self.collection.find_one({"_id": ObjectId(self._id)})
self.merge_dict(self, old_entry)
self.collection.update(
{"_id": ObjectId(self._id)}, self)
self._id = str(self._id)
def replace_dict(self, cur, old):
new = {"_id": ObjectId(self._id)}
for field in old.keys():
if field not in cur:
new.update({field: old[field]})
return new
def delete_field(self):
if not self._id:
self.collection.insert(self)
else:
old_entry = self.collection.find_one({"_id": ObjectId(self._id)})
new_entry = self.replace_dict(self, old_entry)
self.collection.update(
{"_id": ObjectId(self._id)}, new_entry)
self._id = str(self._id)
def reload(self):
if self._id:
result = self.collection.find_one({"_id": ObjectId(self._id)})
if result:
self.update(result)
self._id = str(self._id)
return True
return False
def remove(self):
if self._id:
resp = self.collection.delete_one({"_id": ObjectId(self._id)})
return resp.deleted_count
class User(Model):
db_client = pymongo.MongoClient(
"mongodb+srv://Chris:MakeAMatch@match-maker-db.62sjf.mongodb.net/Match-Maker-DB?retryWrites=true&w=majority")
collection = db_client["users"]["users_list"]
def find_all(self):
users = list(self.collection.find())
for user in users:
user["_id"] = str(user["_id"])
return users
def find_by_name(self, name):
users = list(self.collection.find({"name": name}))
for user in users:
user["_id"] = str(user["_id"])
return users
def secure_find_by_name(self, name):
# does not return password ect. for security reasons. Used for search page
cleaned_users = []
users = self.find_all()
users_copy = users.copy()
for user in users_copy:
if user["name"].lower() != name.lower(): # search is case insensitive
users.remove(user)
for user in users:
user_copy = {"_id": str(user["_id"]), "name": user["name"], "profile_info": user["profile_info"]}
cleaned_users.append(user_copy)
return cleaned_users
def find_by_email(self, email):
users = list(self.collection.find({"email": email}))
for user in users:
user["_id"] = str(user["_id"])
return users
class Group(Model):
db_client = pymongo.MongoClient(
"mongodb+srv://Chris:MakeAMatch@match-maker-db.62sjf.mongodb.net/Match-Maker-DB?retryWrites=true&w=majority")
collection = db_client["groups"]["groups_list"]
def find_all(self):
groups = list(self.collection.find())
for group in groups:
group["_id"] = str(group["_id"])
return groups
class Game(Model):
db_client = pymongo.MongoClient(
"mongodb+srv://Chris:MakeAMatch@match-maker-db.62sjf.mongodb.net/Match-Maker-DB?retryWrites=true&w=majority")
collection = db_client["games"]["games_list"]
def find_all(self):
games = list(self.collection.find())
for game in games:
game["_id"] = str(game["_id"])
return games
def find_by_name(self, game_name):
games = list(self.collection.find({"game_name": game_name}))
for game in games:
game["_id"] = str(game["_id"])
return games
def append_to_queue(self, game_name, new_lobby):
self.collection.update(
{"game_name": game_name},
{'$push': {'queue': new_lobby}}
)
# print(self)
# for lobby in self.queue:
# print(lobby)
# for group in lobby["groups"]:
# print(group)
# for player in group["players"]:
# print(player)
# if player in new_lobby["groups"][0]["players"]:
# print("found")
return new_lobby
# return "lobby not added to queue"
def update_window_size(self, game_name, queue):
self.collection.update(
{"game_name": game_name},
{'$set': {'queue': queue}}
)
class Lobby(Model):
db_client = pymongo.MongoClient(
"mongodb+srv://Chris:MakeAMatch@match-maker-db.62sjf.mongodb.net/Match-Maker-DB?retryWrites=true&w=majority")
collection = db_client["lobbies"]["lobbies_list"]
def find_all(self):
lobbies = list(self.collection.find())
for lobby in lobbies:
lobby["_id"] = str(lobby["_id"])
return lobbies
class Discord(Model):
db_client = pymongo.MongoClient(
"mongodb+srv://Chris:MakeAMatch@match-maker-db.62sjf.mongodb.net/Match-Maker-DB?retryWrites=true&w=majority")
collection = db_client["discords"]["discords_list"]
def find_all(self):
discords = list(self.collection.find())
for discord in discords:
discord["_id"] = str(discord["_id"])
return discords
def find_by_name(self, room_name):
discords = list(self.collection.find({"room_name": room_name}))
for discord in discords:
discord["_id"] = str(discord["_id"])
return discords