1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
flask_jsondash.mongo_adapter |
5
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
6
|
|
|
|
7
|
|
|
Adapters for various storage engines. |
8
|
|
|
|
9
|
|
|
:copyright: (c) 2016 by Chris Tabor. |
10
|
|
|
:license: MIT, see LICENSE for more details. |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
from datetime import datetime as dt |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Db(object): |
17
|
|
|
"""Adapter for all mongo operations.""" |
18
|
|
|
|
19
|
|
|
def __init__(self, client, conn, coll, formatter): |
20
|
|
|
"""Setup connection. |
21
|
|
|
|
22
|
|
|
Args: |
23
|
|
|
client (object): The database client to use. |
24
|
|
|
conn (str): The connection URI. |
25
|
|
|
coll (str): The collection name. |
26
|
|
|
formatter (function): A formatter function to use when formatting |
27
|
|
|
chart data. |
28
|
|
|
""" |
29
|
|
|
self.client = client |
30
|
|
|
self.conn = conn |
31
|
|
|
self.coll = coll |
32
|
|
|
self.formatter = formatter |
33
|
|
|
|
34
|
|
|
def count(self, **kwargs): |
35
|
|
|
"""Standard db count.""" |
36
|
|
|
return self.coll.count(**kwargs) |
37
|
|
|
|
38
|
|
|
def filter(self, *args, **kwargs): |
39
|
|
|
"""Separeately allow more nuanced filtering specific to mongo.""" |
40
|
|
|
return self.coll.find(*args, **kwargs) |
41
|
|
|
|
42
|
|
|
def read(self, **kwargs): |
43
|
|
|
"""Read a record.""" |
44
|
|
|
if kwargs.get('c_id') is None: |
45
|
|
|
return self.coll.find(**kwargs) |
46
|
|
|
else: |
47
|
|
|
return self.coll.find_one(dict(id=kwargs.pop('c_id'))) |
48
|
|
|
|
49
|
|
|
def update(self, c_id, data=None, fmt_charts=True): |
50
|
|
|
"""Update a record. |
51
|
|
|
|
52
|
|
|
Args: |
53
|
|
|
c_id (int): The records id. |
54
|
|
|
data (None, optional): data to update the record with. |
55
|
|
|
fmt_charts (True, optional): A flag to fmt_charts with the |
56
|
|
|
default provided formatter. |
57
|
|
|
""" |
58
|
|
|
if data is None: |
59
|
|
|
return |
60
|
|
|
charts = self.formatter(data) if fmt_charts else data.get('modules') |
61
|
|
|
save_conf = { |
62
|
|
|
'$set': { |
63
|
|
|
'layout': data.get('layout', 'freeform'), |
64
|
|
|
'name': data.get('name', 'NONAME'), |
65
|
|
|
'modules': charts, |
66
|
|
|
'date': dt.now() |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
save_conf['$set'].update(**data) |
70
|
|
|
self.coll.update(dict(id=c_id), save_conf) |
71
|
|
|
|
72
|
|
|
def create(self, data=None): |
73
|
|
|
"""Add a new record. |
74
|
|
|
|
75
|
|
|
Args: |
76
|
|
|
data (dict): The "record" to insert. |
77
|
|
|
""" |
78
|
|
|
if data is None: |
79
|
|
|
return |
80
|
|
|
self.coll.insert(data) |
81
|
|
|
|
82
|
|
|
def delete(self, c_id): |
83
|
|
|
"""Delete a record. |
84
|
|
|
|
85
|
|
|
Args: |
86
|
|
|
c_id (int): The records id. |
87
|
|
|
""" |
88
|
|
|
self.coll.delete_one(dict(id=c_id)) |
89
|
|
|
|
90
|
|
|
def delete_all(self): |
91
|
|
|
"""Delete ALL records. Separated function for safety. |
92
|
|
|
|
93
|
|
|
This should never be used for production. |
94
|
|
|
""" |
95
|
|
|
self.coll.remove() |
96
|
|
|
|