-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.py
47 lines (39 loc) · 1.25 KB
/
database.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
import pymongo
import collections
import sys
import settings
enable = settings.get('db_enable')
if enable:
client = pymongo.MongoClient(settings.get('db_login'))
database = client[settings.get('db_name')]
collection = database['whiteboards']
else:
print('Warning: Database not enabled')
def action_push(bid, action, time):
"""Push an action to a whiteboard"""
if enable:
collection.update({'_id': bid}, {'$push': {'layers': action}, '$set': {'timestamp': time}}, upsert = True)
def action_remove(bid, action_id, time):
"""Remove an action from a whiteboard"""
if enable:
collection.update({'_id': bid}, {'$pull': {'layers': {'action_id': action_id}}, '$set': {'timestamp': time}}, upsert = True)
def rewrite(bid, data):
"""Rewrite all data associated with a whiteboard"""
if enable:
collection.update({'_id': bid}, data, upsert = True)
def load(bid):
"""Load all data for a particular whiteboard"""
if enable:
return collection.find_one({'_id': bid})
def load_meta():
"""Load metadata for all whiteabords
Metadata includes the `name` and `timestamp`
"""
results = []
if enable:
for i in collection.find({}, {'_id': True, 'timestamp': True}):
results.append({
'name': i['_id'],
'timestamp': i.get('timestamp', 0)
})
return results