forked from andrecp/flask-restplus-sqlalchemy-locust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
114 lines (87 loc) · 2.8 KB
/
backend.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
from flask import Flask
from flask_restplus import Api, Resource, fields
from werkzeug.contrib.fixers import ProxyFix
from flask_sqlalchemy import SQLAlchemy
# App definition.
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.wsgi_app = ProxyFix(app.wsgi_app)
db = SQLAlchemy(app)
# Db definition.
class TODOModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
task = db.Column(db.String(80))
def __init__(self, task):
self.task = task
def __repr__(self):
return '<Task %r>' % self.task
# Api definition.
api = Api(app, version='1.0', title='TodoMVC API',
description='A simple TodoMVC API',
)
ns = api.namespace('todos', description='TODO operations')
todo = api.model('Todo', {
'id': fields.Integer(readOnly=True, description='The task unique identifier'),
'task': fields.String(required=True, description='The task details')
})
class TodoDAO(object):
@property
def todos(self):
return TODOModel.query.all()
def get(self, id):
todo = TODOModel.query.filter_by(id=id).first()
if todo:
return {"id": todo.id, "task": todo.task}
api.abort(404, "Todo {} doesn't exist".format(id))
def create(self, data):
todo = TODOModel(data['task'])
db.session.add(todo)
db.session.commit()
return todo
def update(self, id, data):
todo = TODOModel.query.filter_by(id=id).first()
todo.task = data['task']
db.session.commit()
return todo
def delete(self, id):
todo = TODOModel.query.filter_by(id=id).first()
db.session.delete(todo)
db.session.commit()
DAO = TodoDAO()
@ns.route('/')
class TodoList(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@ns.doc('list_todos')
@ns.marshal_list_with(todo)
def get(self):
'''List all tasks'''
return DAO.todos
@ns.doc('create_todo')
@ns.expect(todo)
@ns.marshal_with(todo, code=201)
def post(self):
'''Create a new task'''
return DAO.create(api.payload), 201
@ns.route('/<int:id>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
'''Show a single todo item and lets you delete them'''
@ns.doc('get_todo')
@ns.marshal_with(todo)
def get(self, id):
'''Fetch a given resource'''
return DAO.get(id)
@ns.doc('delete_todo')
@ns.response(204, 'Todo deleted')
def delete(self, id):
'''Delete a task given its identifier'''
DAO.delete(id)
return '', 204
@ns.expect(todo)
@ns.marshal_with(todo)
def put(self, id):
'''Update a task given its identifier'''
return DAO.update(id, api.payload)
if __name__ == '__main__':
app.run(debug=True)