forked from FloatingOctothorpe/python-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (76 loc) · 2.76 KB
/
main.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
#/usr/bin/env python
"""A simple Kanban board application using Flask and SQLLite"""
from flask import Flask, send_from_directory, request, abort
from flask.json import jsonify
from database import db
import cards
def create_app():
"""Create a new instance of the flask app"""
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///kanban.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['kanban.columns'] = ['To Do', 'Doing', 'Done']
db.init_app(app)
app.app_context().push()
db.create_all()
@app.route('/')
def index():
"""Serve the main index page"""
return send_from_directory('static', 'index.html')
@app.route('/static/<path:path>')
def static_file(path):
"""Serve files from the static directory"""
return send_from_directory('static', path)
@app.route('/cards')
def get_cards():
"""Get an order list of cards"""
return jsonify(cards.all_cards())
@app.route('/columns')
def get_columns():
"""Get all valid columns"""
return jsonify(app.config.get('kanban.columns'))
@app.route('/card', methods=['POST'])
def create_card():
"""Create a new card"""
# TODO: validation
cards.create_card(
text=request.form.get('text'),
column=request.form.get('column', app.config.get('kanban.columns')[0]),
color=request.form.get('color', None),
)
# TODO: handle errors
return 'Success'
@app.route('/card/reorder', methods=["POST"])
def order_cards():
"""Reorder cards by moving a single card
The JSON payload should have a 'card' and 'before' attributes where card is
the card ID to move and before is the card id it should be moved in front
of. For example:
{
"card": 3,
"before": 5,
}
"before" may also be "all" or null to move the card to the beginning or end
of the list.
"""
if not request.is_json:
abort(400)
cards.order_cards(request.get_json())
return 'Success'
@app.route('/card/<int:card_id>', methods=['PUT'])
def update_card(card_id):
"""Update an existing card, the JSON payload may be partial"""
if not request.is_json:
abort(400)
# TODO: handle errors
cards.update_card(card_id, request.get_json(), app.config.get('kanban.columns'))
return 'Success'
@app.route('/card/<int:card_id>', methods=['DELETE'])
def delete_card(card_id):
"""Delete a card by ID"""
# TODO: handle errors
cards.delete_card(card_id)
return 'Success'
return app
if __name__ == '__main__':
create_app().run(debug=True)