/
mongodb.py
467 lines (408 loc) · 16.7 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import re
import sre_constants
import urllib
from collections import defaultdict
import bson
import pymongo
from pymongo import DESCENDING, ASCENDING
from pymongo.collection import Collection
from werkzeug.security import generate_password_hash, check_password_hash
from lib.DatabaseLayer import sanitize
from lib.DatabasePluginBase import DatabasePluginBase
from lib.Config import Configuration
config = Configuration()
DNSSRV = config.readSetting("Database", "DnsSrvRecord", config.default["mongoSrv"])
HOST = config.readSetting("Database", "Host", config.default["mongoHost"])
PORT = config.readSetting("Database", "Port", config.default["mongoPort"])
AUTH = config.readSetting("Database", "AuthDB", config.default["mongoAuth"])
DATABASE = config.getMongoDB()
USERNAME = urllib.parse.quote(
config.readSetting("Database", "Username", config.default["mongoUsername"])
)
PASSWORD = urllib.parse.quote(
config.readSetting("Database", "Password", config.default["mongoPassword"])
)
class MongoPlugin(DatabasePluginBase):
def __init__(self):
"""
Custom MongoDBPlugin
"""
super().__init__()
if USERNAME and PASSWORD and DNSSRV is True:
mongoURI = "mongodb+srv://{username}:{password}@{host}/{db}?authSource={auth}&retryWrites=true&w=majority".format(
username=USERNAME, password=PASSWORD, host=HOST, db=DATABASE, auth=AUTH
)
elif USERNAME and PASSWORD:
mongoURI = "mongodb://{username}:{password}@{host}:{port}/{db}?authSource={auth}".format(
username=USERNAME,
password=PASSWORD,
host=HOST,
port=PORT,
db=DATABASE,
auth=AUTH,
)
else:
mongoURI = "mongodb://{host}:{port}/{db}".format(
host=HOST, port=PORT, db=DATABASE
)
connect = pymongo.MongoClient(mongoURI, connect=False)
self.connection = connect[DATABASE]
self.user_store = connect[DATABASE]["mgmt_users"]
for each in self.connection.list_collection_names():
setattr(
self, f"store_{each}", Collection(database=self.connection, name=each)
)
def create_schema(self, **kwargs):
pass
def count(self, retrieve, dict_filter={}):
"""
Method for retrieving a document count
:param retrieve: Collection to retrieve the count from
:type retrieve: str
:param dict_filter: dict representing a filter see pyMongo doc
:type dict_filter: dict
:return: Count
:rtype: int
"""
if isinstance(dict_filter, dict):
return getattr(self, "store_{}".format(retrieve)).count_documents(
filter=dict_filter
)
else:
return getattr(self, "store_{}".format(retrieve)).count_documents(
filter={"$and": dict_filter}
)
def query_docs(
self,
retrieve=None,
dict_filter={},
sort=None,
limit=None,
skip=0,
query_filter=None,
sort_dir="DESC",
id_to_string=True,
):
"""
Method used to perform a extended query against the database in order to fetch documents.
:param retrieve: Collection name where to retrieve the documents from
:type retrieve: str
:param dict_filter: dict representing a filter see pyMongo doc
:type dict_filter: dict
:param sort: Field to sort on or list of tuples of field and direction
[("field", pymongo.DESCENDING), ("field2", pymongo.DESCENDING)]
:type sort: list
:param limit: Can be used to limit the amount of returned results
:type limit: int
:param skip: Can be used to skip the first x records of the returned results
:type skip: int
:param query_filter: Dict to include fields to return in the query results (e.g.: {field1: 1, field2: 1})
:type query_filter: dict
:param sort_dir: DESC (default) for DESCENDING, ASC for ASCENDING
:type sort_dir: str
:param id_to_string: Whether to convert mongodb '_id' from an ObjectId to a string
:type id_to_string: bool
:return: List of documents saved in the backend
:rtype: list
"""
documentlist = []
if sort is not None:
if isinstance(sort, str):
if sort_dir == "DESC":
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort, DESCENDING)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort, DESCENDING)
.skip(skip)
)
elif sort_dir == "ASC":
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort, ASCENDING)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort, ASCENDING)
.skip(skip)
)
if isinstance(sort, list):
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.sort(sort)
.skip(skip)
)
else:
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, {"json": 0})
.skip(skip)
)
if query_filter is not None:
if sort is not None:
if isinstance(sort, str):
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.sort(sort, DESCENDING)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.sort(sort, DESCENDING)
.skip(skip)
)
if isinstance(sort, list):
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.sort(sort)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.sort(sort)
.skip(skip)
)
else:
if limit is not None:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.limit(limit)
.skip(skip)
)
else:
resultset = (
getattr(self, "store_{}".format(retrieve))
.find(dict_filter, query_filter)
.skip(skip)
)
if id_to_string:
for doc in resultset:
doc["_id"] = str(doc["_id"])
documentlist.append(doc)
else:
for doc in resultset:
documentlist.append(doc)
return documentlist
def fetch_one(
self, retrieve=None, dict_filter={}, query_filter=None, id_to_string=True
):
"""
The fetch one method wraps the original find_one method from pyMongo and turns the _id into a string.
:param retrieve: Collection name where to retrieve the documents from
:type retrieve: str
:param dict_filter: dict representing a filter see pyMongo doc
:type dict_filter: dict
:param query_filter: Dict with fields to exclude or include; e.g. {"hosts": 0} or {"hosts": 1}
:type query_filter: dict
:param id_to_string: Whether to convert mongodb '_id' from an ObjectId to a string
:type id_to_string: bool
:return: Dict with items from database or None
:rtype: dict or None
"""
if "_id" in dict_filter:
if isinstance(dict_filter["_id"], str):
dict_filter["_id"] = bson.objectid.ObjectId(dict_filter["_id"])
if query_filter is not None:
result = getattr(self, "store_{}".format(retrieve)).find_one(
dict_filter, query_filter
)
else:
result = getattr(self, "store_{}".format(retrieve)).find_one(dict_filter)
if result is not None:
if id_to_string:
result["_id"] = str(result["_id"])
return result
def datatables_data_columns_ordering(self, request_values):
"""
Method responsible for retrieving the column and order details from the DataTables request values
:param request_values:
:type request_values:
:return: 2 Dictionaries namely: the column details and order details
:rtype: defaultdict, defaultdict
"""
col = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
order = defaultdict(lambda: defaultdict(dict))
col_regex = re.compile(r"columns\[(\d*)\]\[(\w*)\](?:\[(\w*)\])?")
order_regex = re.compile(r"order\[(\d*)\]\[(\w*)\]")
for each in sorted(request_values.keys()):
check_col_match = col_regex.match(each)
check_order_match = order_regex.match(each)
if check_col_match:
if check_col_match.group(2) == "search":
try:
col[check_col_match.group(1)][check_col_match.group(2)][
check_col_match.group(3)
] = int(request_values[each])
except ValueError:
col[check_col_match.group(1)][check_col_match.group(2)][
check_col_match.group(3)
] = request_values[each]
else:
col[check_col_match.group(1)][
check_col_match.group(2)
] = request_values[each]
if check_order_match:
order[check_order_match.group(1)][
check_order_match.group(2)
] = request_values[each]
return col, order
def datatables_data_filter(self, request_values, columns, additional_filters):
"""
Method responsible for retrieving the filter values entered in the search box of the DataTables.
:param request_values:
:type request_values:
:param columns:
:type columns:
:param additional_filters:
:type additional_filters:
:return: Prepared filter based on filterable columns and retrieved search value
:rtype: dict
"""
docfilter = defaultdict(list)
search_val = request_values["search[value]"]
if search_val != "":
try:
regex = re.compile(search_val, re.IGNORECASE)
# get list with searchable columns
column_search_list = [
columns[i]["data"] for i in columns if columns[i]["searchable"]
]
for each in column_search_list:
docfilter["$or"].append({each: {"$regex": regex}})
except sre_constants.error:
pass
if additional_filters is not None:
docfilter["$and"] = additional_filters
# create an additional column filter with entries in the columns[x]['search']['value']
colfilter = {
columns[key]["data"]: columns[key]["search"]["value"]
for (key, value) in columns.items()
if (
columns[key]["search"]["value"] != ""
and columns[key]["search"]["regex"] == "false"
)
}
colregexfilter = {
columns[key]["data"]: {"$regex": columns[key]["search"]["value"]}
for (key, value) in columns.items()
if (
columns[key]["search"]["value"] != ""
and columns[key]["search"]["regex"] == "true"
)
}
if len(colfilter) != 0:
docfilter["$and"].append(colfilter)
if len(colregexfilter) != 0:
docfilter["$and"].append(colregexfilter)
return docfilter
def datatables_data_order(self, ordering, columns):
"""
Method responsible for setting up the column sorting.
:param ordering:
:type ordering:
:param columns:
:type columns:
:return: list with sorting instructions
:rtype: list
"""
sort = []
for each in ordering:
if ordering[each]["dir"] == "asc":
sort.append(
(columns[ordering[each]["column"]]["data"], pymongo.ASCENDING)
)
else:
sort.append(
(columns[ordering[each]["column"]]["data"], pymongo.DESCENDING)
)
return sort
def addUser(self, user, pwd, admin=False, localOnly=False):
hashed = generate_password_hash(pwd, method="pbkdf2:sha512")
entry = {"username": user, "password": hashed}
if admin:
entry["master"] = True
if localOnly:
entry["local_only"] = True
self.user_store.insert(entry)
def changePassword(self, user, pwd):
hashed = generate_password_hash(pwd, method="pbkdf2:sha512")
self.user_store.update({"username": user}, {"$set": {"password": hashed}})
def verifyUser(self, user, pwd):
person = self.getUser(user)
return person and check_password_hash(person["password"], pwd)
def deleteUser(self, user):
self.user_store.remove({"username": user})
def setAdmin(self, user, admin=True):
if admin:
self.user_store.update({"username": user}, {"$set": {"master": True}})
else:
self.user_store.update({"username": user}, {"$unset": {"master": ""}})
def setLocalOnly(self, user, localOnly=True):
if localOnly:
self.user_store.update({"username": user}, {"$set": {"local_only": True}})
else:
self.user_store.update({"username": user}, {"$unset": {"local_only": ""}})
def isMasterAccount(self, user):
return (
False
if self.user_store.find({"username": user, "master": True}).count() == 0
else True
)
def userExists(self, user):
return (
True if self.user_store.count_documents({"username": user}) > 0 else False
)
def isSingleMaster(self, user):
return (
True
if self.user_store.count_documents(
{"username": {"$ne": user}, "master": True}
)
> 0
else False
)
def getUser(self, user):
return sanitize(self.user_store.find_one({"username": user}))