-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·92 lines (66 loc) · 2.01 KB
/
server.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
import json
from flask import Flask, request
from flask.ext.cors import CORS
app = Flask(__name__, static_url_path='', static_folder='.')
cors = CORS(app)
CORS(app, resources=r'/api/*', allow_headers='Content-Type')
class Task(object):
"""A task."""
def __init__(self, text):
self.text = text
def to_dict(self):
return {"text": self.text}
class TaskList(object):
"""A list of Task objects."""
def __init__(self, name, tasks):
self.name = name
self.tasks = tasks
def to_dict(self):
return {
"name": self.name,
"tasks": [task.to_dict() for task in self.tasks]
}
class Board(object):
"""A collection of TaskLists."""
def __init__(self, lists):
self.lists = lists
def to_dict(self):
return {
"lists": [list.to_dict() for list in self.lists]
}
DB = Board([
TaskList(name="Todo",
tasks=[
Task("Write example React app"),
Task("Write documentation")
]),
TaskList(name="Done",
tasks=[
Task("Learn the basics of React")
])
])
@app.route("/api/board/")
def get_board():
"""Return the state of the board."""
return json.dumps(DB.to_dict())
@app.route("/api/<int:list_id>/task", methods=["PUT"])
def add_task(list_id):
# Add a task to a list.
try:
DB.lists[list_id].tasks.append(Task(text=request.form.get("text")))
except IndexError:
return json.dumps({"status": "FAIL"})
return json.dumps({"status": "OK"})
@app.route("/api/<int:list_id>/task/<int:task_id>", methods=["DELETE"])
def delete_task(list_id, task_id):
# Remove a task from a list.
try:
del DB.lists[list_id].tasks[task_id]
except IndexError:
return json.dumps({"status": "FAIL"})
return json.dumps({"status": "OK"})
@app.route("/")
def index():
return app.send_static_file('index.html')
if __name__ == "__main__":
app.run(port=8000, debug=True)