-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsblog.py
86 lines (73 loc) · 3.03 KB
/
sblog.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
from quart import (abort, Quart, redirect, render_template, request, url_for)
from tinymongo import TinyMongoClient
app = Quart(__name__)
DB = TinyMongoClient().blog
@app.errorhandler(404)
async def page_not_found(e):
"""404 error handler-- 404 status set explicitly"""
return await render_template('404.html'), 404
@app.route('/')
async def index():
"""return all the pages to a user view"""
pages = DB.blog.find()
return await render_template('page_list.html', pages=pages)
@app.route('/view/<id>')
async def page_view(id):
"""view a page by id or 404 if does not exist"""
page = DB.blog.find_one({'_id':id})
if page is None:
#
abort(404)
return await render_template('view.html', page=page)
@app.route('/edit', methods=['GET','POST'])
@app.route('/edit/<id>', methods=['GET','POST'])
async def page_edit(id=None):
"""edit serves to edit an existing page with a particular id, or create a new page"""
status = ''
if id:
# find the page by its id, if it doesn't exist page = None, abort to 404 page
page = DB.blog.find_one({'_id':id})
if page is None:
abort(404)
else:
# new page starts as a blank document
status = 'Creating a new page.'
page = {'title': '', 'content': ''}
if request.method == 'POST':
# check if user cancel was pressed.
if (await request.form)['submit'] == 'cancel':
if id:
# user canceled a page edit, return to page view
return redirect(url_for('page_view', id=id))
else:
# user canceled a new page creation, return to index
return redirect(url_for('index'))
# user hit submit, so get the data from the form.
page['title'] = (await request.form).get('title')
page['content'] = (await request.form).get('editordata')
# look for required title and content
if page['title'] != '' and page['content'] != '':
# now, update or insert into database
if id:
# update an existing page
DB.blog.update_one({'_id':id}, page)
else:
# insert a new page and get its id.
id = DB.blog.insert_one(page).inserted_id
# redirect to page view
return redirect(url_for('page_view', id=id))
else:
# indicate a failure to enter required data
status = 'ERROR: page title and content are required!'
return await render_template('edit.html', page=page, status=status)
@app.route('/delete/<id>')
async def page_delete(id):
"""delete a page of a particular id, and return to top-level index"""
page = DB.blog.find_one({'_id':id})
if page is None:
abort(404)
DB.blog.delete_one({'_id':id})
return redirect(url_for('index'))
if __name__ == '__main__':
# note: app.run should not be used in production for a variety of reasons.
app.run(debug=False, port=5000, host='0.0.0.0')