forked from gamesbrainiac/Pony-Todo-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
132 lines (94 loc) · 3.25 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
# encoding=utf-8
# app.py
__author__ = "Quazi Nafiul Islam"
import json
from flask import Flask, request
import flask.ext.restful as rest
from models import *
# Boilerplate
app = Flask(__name__)
api = rest.Api(app)
# Binding and generating mapping
db.bind('sqlite', 'todo_api.db', create_db=True)
db.generate_mapping(create_tables=True)
# Resource #######################################################################
class Todos(rest.Resource):
def get(self):
"""Will give you all the todo items"""
try:
with po.db_session:
return {
i.id: [
i.data,
["http://localhost:5000/tags/{}".format(t.id) for t in i.tags]
]
for i in po.select(item for item in Todo)
}
except Exception:
return {}, 404
def put(self):
"""Payload contains information to create new todo item"""
info = json.loads(request.data)
with po.db_session:
item = Todo(data=info['data'])
created_tags = {
t.name: t
for t in po.select(tag for tag in Tag)}
for tag in info['tags']:
if tag in created_tags:
item.tags += [created_tags[tag]]
else:
item.tags += Tag(name=tag)
return {}, 200
class TodoItem(rest.Resource):
def get(self, todo_id):
"""
Get specific information on a Todo item
:param todo_id: The Todo Item's ID, which is unique and a primary key
:type todo_id: int
"""
with po.db_session:
todo = po.select(x for x in Todo if x.id == todo_id)[:][0]
todo_tag_ids = [tag.id for tag in todo.tags]
tags = [
tag.name for tag in
po.select(t for t in Tag if t.id in todo_tag_ids)
]
return {
"Task": todo.data,
"Tags": tags
}
class Tags(rest.Resource):
def get(self):
"""Will show you all tags"""
with po.db_session:
return {
t.name: "http://localhost:5000/tags/{}".format(t.id)
for t in po.select(_ for _ in Tag)
}
class TagItem(rest.Resource):
def get(self, tag_id):
"""
Will show you information about a specific tag
:param tag_id: ID for the tag
:type tag_id: int
"""
with po.db_session:
tag = po.select(t for t in Tag if t.id == tag_id)[:][0]
tag_todo_ids = [todo.id for todo in tag.todos]
todos = [
todo.data for todo in
po.select(t for t in Todo if t.id in tag_todo_ids)
]
return {
"Tag": tag.name,
"Todos": todos
}
# Paths ##########################################################################
api.add_resource(Todos, '/', endpoint='Home')
api.add_resource(TodoItem, '/<int:todo_id>', endpoint='TodoItem')
api.add_resource(Tags, '/tags/', endpoint='Tags')
api.add_resource(TagItem, '/tags/<int:tag_id>', endpoint='TagItem')
if __name__ == '__main__':
po.sql_debug(True)
app.run(debug=True)