-
Notifications
You must be signed in to change notification settings - Fork 17
/
mongo.py
154 lines (108 loc) · 3.83 KB
/
mongo.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
import Item as it
import requests
import json
import os
import pymongo
import datetime
from bson import ObjectId
from user import User
def fetch_all_items():
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
item_list = []
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
ToDoCollection = db.ToDoCollection
for ToDo in ToDoCollection.find():
item_list.append(it.Item(id=ToDo['_id'], status=ToDo['status'], title=ToDo['name'], last_modified=str(ToDo['last_modified'])))
return item_list
def create_new_item(name):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
todo = db.ToDoCollection
item = {"name": name, "status": "To Do", "last_modified": datetime.datetime.now()}
todo.insert_one(item).inserted_id
def update_item_doing(id):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoCollection
myquery = {"_id": ObjectId(id)}
newvalues = {"$set": { "status": "Doing"}}
col.update_one(myquery, newvalues)
def update_item_done(id):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoCollection
myquery = {"_id": ObjectId(id)}
newvalues = {"$set": { "status": "Done"}}
col.update_one(myquery, newvalues)
def delete_item(id):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoCollection
myquery = {"_id": ObjectId(id)}
col.delete_one(myquery)
def create_trello_board():
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
client[Mongo_db]
return "todo_test_db"
def delete_trello_board():
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
client.drop_database(Mongo_db)
def add_user_mongo(current_user):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoUsers
user = {"name": current_user.name, "id": current_user.id, "role": current_user.role}
col.insert_one(user).inserted_id
def fetch_user(user_id):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoUsers
user = []
user = col.find_one({'id': int(user_id)})
return user
def fetch_all_users():
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
item_list = []
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoUsers
for user in col.find():
item_list.append({'name':user['name'], 'role':user['role'], 'id':user['id']})
return item_list
def make_admin(userid):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoUsers
myquery = {"id": int(userid)}
newvalues = {"$set": { "role": "admin"}}
col.update_one(myquery, newvalues)
def make_reader(userid):
Mongo_Url = os.environ.get('Mongo_Url')
Mongo_db = os.environ.get('Mongo_db')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_db]
col = db.ToDoUsers
myquery = {"id": int(userid)}
newvalues = {"$set": { "role": "reader"}}
col.update_one(myquery, newvalues)