-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
284 lines (232 loc) · 7.01 KB
/
app.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from bottle import default_app, run, route, get, post, request, template, static_file
from items import *
from mongo_items import *
import json
from bson import json_util
if __name__ == "__main__":
HOME = "./"
else:
HOME = "/home/ChangCode/mysite/"
import sqlite3
from peewee import *
database = SqliteDatabase(HOME+'todo.db')
class BaseModel(Model):
class Meta:
database = database
class Todo(BaseModel):
status = BooleanField()
task = CharField()
class Meta:
db_table = 'todo'
#API
@post('/nearbyAPI')
def nearby_list():
lon = request.forms.get('lon', '').strip()
lat = request.forms.get('lat', '').strip()
dis = request.forms.get('dis', '').strip()
conn = pymongo.MongoClient('mongodb://todo:[email protected]:27415/todoshuchang')
db = conn['todoshuchang']
coll = db.location
near = coll.find({"location":{ "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [ float(lon), float(lat) ] }, "$maxDistance": (float(dis)) * 1609.34 } }})
data_list = []
for n in near:
del n['_id']
data_list.append(n)
final = {'result': 'true','data':data_list}
return final
@get('/nearbyAPI')
def nearby_page():
return template('nearbyAPI')
#mongoDB
@get('/mongodb')
@get('/mongodb/<status:int>')
def mongo_get_list(status=-1):
items = mongo_get_items(status)
result = []
for item in items:
result.append((item['id'],item['task'],item['status']))
return template('mongo_list', rows=result)
@get('/mongodb/new')
def mongo_get_new():
return '''
<p>Enter a new item...</p><br/>
<form action="/mongodb/new" method="post">
To be done: <input name="task" type="text" />
<input value="Save" type="submit" />
</form>
'''
@post('/mongodb/new')
def mongo_post_new():
task = request.forms.get('task', '').strip()
mongo_new_item(task,1)
return mongo_get_list()
@get('/mongodb/edit/<id:re:[a-z0-9]+>')
def mongo_get_edit(id):
item = mongo_get_item(id)
print(item['task'])
return template('mongo_edit', id=id, task=item['task'], status=item['status'])
@post('/mongodb/edit/<id:re:[a-z0-9]+>')
def mongo_post_edit(id):
task = request.forms.get('task', '').strip()
status = request.GET.get('status','').strip()
if status == 'open':
status = 1
else:
status = 0
item = mongo_get_item(id)
item['task'] = task
item['status'] = status
mongo_save_item(item)
return mongo_get_list()
@get('/mongodb/delete/<id:re:[a-z0-9]+>')
def mongo_confirm_delete_item(id):
item = mongo_get_item(id=id)
return template('mongo_delete', id=id, task=item['task'], status=item['status'])
@post('/mongodb/delete/<id:re:[a-z0-9]+>')
def mongo_delete_item(id):
mongo_discard_item(id)
return mongo_get_list()
#tinyDB
@route('/tinydb')
@route('/tinydb/<status:int>')
def tinydb(status=-1):
items = tiny_get_items(status)
result = []
for item in items:
result.append((item['id'],item['task'],item['status']))
return template('tinydb', rows=result)
@get('/tinydb/new')
def get_new():
return '''
<p>Enter a new item...</p><br/>
<form action="/tinydb/new" method="post">
To be done: <input name="task" type="text" />
<input value="Save" type="submit" />
</form '''
@post('/tinydb/new')
def post_new():
task = request.forms.get('task', '').strip()
tiny_new_item(task,1)
return tinydb()
@get('/tinydb/edit/<id:int>')
def get_edit(id):
item = tiny_get_item(id)
return template('tinydb_edit', id=id, task=item['task'], status=item['status'])
@post('/tinydb/edit/<id:int>')
def post_edit(id):
task = request.forms.get('task', '').strip()
status = request.GET.get('status','').strip()
if status == 'open':
status = 1
else:
status = 0
item = tiny_get_item(id)
item['task'] = task
item['status'] = status
tiny_save_item(item)
return tinydb()
@get('/tinydb/delete/<id:int>')
def confirm_delete_item(id):
item = tiny_get_item(id)
return template('tinydb_delete_view', id=id, task=item['task'], status=item['status'])
@post('/tinydb/delete/<id:int>')
def delete_item(id):
tiny_discard_item(id)
return tinydb()
#PeeWee
@route('/model')
def model_list():
query = Todo.select().where(Todo.status).order_by(Todo.task.asc())
result = []
for todo in query:
result.append((todo.id,todo.task,todo.status))
output = template('model_list_view', rows=result)
return output
#sqlite3
@route('/todo')
@route('/todo/<status:int>')
def todo_list(status=-1):
conn = sqlite3.connect(HOME+'todo.db')
c = conn.cursor()
if (status >= 0):
c.execute("SELECT id, task, status FROM todo WHERE status LIKE '"+str(status)+"'")
else:
c.execute("SELECT id, task, status FROM todo")
result = c.fetchall()
output = template('make_table', rows=result)
return output
@get('/new') # or @route('/login')
def new_item_request():
return template('new')
@post('/new')
def new_item():
new = request.forms.get('task', '').strip()
conn = sqlite3.connect(HOME+'todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,1))
new_id = c.lastrowid
conn.commit()
c.close()
#return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
return todo_list(1)
@route('/edit/<no:int>', method='GET')
def edit_item(no):
if request.GET.get('save','').strip():
edit = request.GET.get('task','').strip()
status = request.GET.get('status','').strip()
if status == 'open':
status = 1
else:
status = 0
conn = sqlite3.connect(HOME+'todo.db')
c = conn.cursor()
c.execute("UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit, status, no))
conn.commit()
return '<p>The item number %s was successfully updated</p>' % no
else:
conn = sqlite3.connect(HOME+'todo.db')
c = conn.cursor()
c.execute("SELECT task FROM todo WHERE id LIKE ?", (str(no)))
cur_data = c.fetchone()
return template('edit_task', old=cur_data, no=no)
@route('/delete/<no:int>', method='GET')
def delete_item(no):
conn = sqlite3.connect(HOME+'todo.db')
c = conn.cursor()
c.execute("DELETE FROM todo WHERE id LIKE ?", (str(no)))
conn.commit()
return '<p>The new task was DELETED from the database, the ID was ' + str(no) + '</p>'
@route('/')
def hello_world():
output = template('index')
return output
@route('/<filename:path>')
def server_static(filename):
return static_file(filename, root='static/')
@get('/login') # or @route('/login')
def login():
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>
'''
def check_login(username, password):
if username == "greg" and password == "database":
return True
return False
@post('/login') # or @route('/login', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
print (username)
print (password)
if check_login(username, password):
return "<p>Your login information was correct.</p>"
else:
return "<p>Login failed.</p>"
if __name__ == "__main__":
run(reloader = True, host="0.0.0.0")
else:
application = default_app()