-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.py
85 lines (67 loc) · 2.29 KB
/
db_connect.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
import pymongo
from bson.objectid import ObjectId
from task_model import Task
from config import CONNECTION_STRING
def get_connection(conn_string):
return pymongo.MongoClient(conn_string)
def get_db(conn, db_name):
return conn[db_name]
def get_collection(db, collection_name):
return db[collection_name]
def get_cron_list():
cursor = collection.find()
if cursor:
res = []
for cron in list(cursor):
if cron.get('action'):
if cron.get('reboot'):
res.append(Task(action=cron['action'],
reboot=True,
_id=cron['_id']))
else:
res.append(Task(cron['minute'],
cron['hour'],
cron['day_of_month'],
cron['month'],
cron['day_of_week'],
cron['action'],
cron['_id']))
return res
def db_update(cron):
if cron.reboot:
data = {'reboot': True,
'action': cron.action
}
else:
data = {'minute': cron.minute,
'hour': cron.hour,
'day_of_month': cron.day_of_month,
'month': cron.month,
'day_of_week': cron.day_of_week,
'action': cron.action
}
try:
collection.update({'_id': cron._id}, data)
except Exception as e:
print 'Unable to update DB entry.\nError:', e
def db_add(cron):
if cron.reboot:
data = {'reboot': True,
'action': cron.action
}
else:
data = {'minute': cron.minute,
'hour': cron.hour,
'day_of_month': cron.day_of_month,
'month': cron.month,
'day_of_week': cron.day_of_week,
'action': cron.action
}
try:
collection.insert_one(data)
except Exception as e:
print 'Unable to create DB entry.\nError:', e
def db_delete(_id):
collection.remove({'_id': ObjectId(_id)})
db = get_db(get_connection(CONNECTION_STRING), 'test')
collection = get_collection(db, 'cron-manager')