forked from leancloud/python-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodos.py
More file actions
39 lines (31 loc) · 896 Bytes
/
todos.py
File metadata and controls
39 lines (31 loc) · 896 Bytes
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
# coding: utf-8
from leancloud import Object
from leancloud import Query
from leancloud import LeanCloudError
from flask import Blueprint
from flask import request
from flask import redirect
from flask import url_for
from flask import render_template
class Todo(Object):
pass
todos_view = Blueprint('todos', __name__)
@todos_view.route('')
def show():
try:
todos = Query(Todo).descending('createdAt').find()
except LeanCloudError as e:
if e.code == 101: # 服务端对应的 Class 还没创建
todos = []
else:
raise e
return render_template('todos.html', todos=todos)
@todos_view.route('', methods=['POST'])
def add():
content = request.form['content']
todo = Todo(content=content)
try:
todo.save()
except LeanCloudError as e:
return e.error, 502
return redirect(url_for('todos.show'))