-
Notifications
You must be signed in to change notification settings - Fork 5
/
todo.py
73 lines (51 loc) · 1.82 KB
/
todo.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
import flask.ext.restless
import flask.ext.sqlalchemy
from flask import Flask, render_template
app = Flask(__name__)
# Create the Flask-SQLAlchemy object and an SQLite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
db = flask.ext.sqlalchemy.SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Text, unique=False)
is_completed = db.Column(db.Boolean)
# When we create a new Todo it should be incomplete and have a title
def __init__(self, title, is_completed=False):
self.title = title
self.is_completed = is_completed
# Create the database tables.
db.create_all()
# Create the Flask-Restless API manager.
restless_manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
def post_collection_formatter(results):
return {'todos': results['objects']} if 'page' in results else results
def post_singlular_formatter(results):
return {'todo': results}
def pre_ember_formatter(results):
return results['todo']
def pre_patch_ember_formatter(instid, results):
return results['todo']
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
restless_manager.create_api(
Todo,
methods=['GET', 'POST', 'DELETE', 'PUT', 'PATCH'],
url_prefix='/api',
collection_name='todos',
results_per_page=-1,
postprocessors={
'GET_MANY': [post_collection_formatter],
'POST': [post_singlular_formatter],
'PUT_SINGLE': [post_singlular_formatter]
},
preprocessors={
'POST': [pre_ember_formatter],
'PUT_SINGLE': [pre_patch_ember_formatter]
}
)
app.debug = True
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0')