-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_storage.py
119 lines (75 loc) · 2.69 KB
/
mongo_storage.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
#Convert to use mongodb
#import sqlite3
import os
from bottle import get, post, template, request, redirect
import pymongo
client = pymongo.MongoClient("mongodb+srv://tester_1:[email protected]/todo?retryWrites=true&w=majority",
connectTimeoutMS=30000,
socketTimeoutMS=None,
#socketKeepAlive=True,
connect=False,
maxPoolsize=1)
db = client.todo
#are we executing on PythonAnywhere?
PYTHONANYWHERE = "PYTHONANYWHERE_DOMAIN" in os.environ
#assert PYTHONANYWHERE == False
if PYTHONANYWHERE:
from bottle import default_app, route
else:
from bottle import run, debug
#from storage import get_item, get_items, create_item, delete_item, update_item, update_status
from postgres_storage import get_item, get_items, create_item, delete_item, update_item, update_status
#from mongo_storage import get_item, get_items, create_item, delete_item, update_item, update_status
@get('/')
def get_show_list():
result = db.task.find()
return template("show_list", rows=result)
@get("/new_item")
def get_new_item():
return template("new_item")
@post("/new_item")
def post_new_item():
new_item = request.forms.get("new_item").strip()
create_item(new_item, 1)
redirect("/")
@get("/delete_item/<id:int>")
def get_delete_item(id):
db.task.delete_one({"id":id})
redirect("/")
@get("/set_status/<id:int>/<value:int>")
def get_set_status(id, value):
update_status(id, value)
redirect("/")
@get("/update_item/<id:int>")
def get_update_item(id):
result = get_item(id)
return template("update_item", row=result)
@post("/update_item")
def post_update_item():
update = request.forms.get("update_item").strip()
id = request.forms.get("id").strip()
db.todo.update_one({"id":id}, {"$set" : {"task":new_task})
redirect("/")
if PYTHONANYWHERE:
application = default_app()
else:
run(host='localhost', port=8080)
def update_status(id):
item = db.todo.find_one({"id":id})
result = [item['id'], item['task'], item['status']]
db.todo.update_one({"id":id}, {"$set" : {"status":((result[2]+1)%2)})
def create_item(task, status):
id = next_id()
db.todo.insert_one({"id":id, "task":task, "status":status})
return id
def get_item(id):
item = db.todo.find_one({"id":id})
if len(item) == 0:
return None
result = [(item['id'], item['task'], item['status'])]
return result
def update_item(new_task, id):
db.todo.update_one({"id":id}, {"$set" : {"task":new_task})
#call tests if file is run by itself only
if __name__ == "__main__":
print("Done")