-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
151 lines (128 loc) · 4.21 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, render_template, request, redirect, url_for, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://akku:akku@localhost:5432/todoapp'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class TodoList(db.Model):
__tablename__ = 'todolists'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(), nullable=False)
done = db.Column(db.Boolean, nullable=False, default=False)
todos = db.relationship('Todo', backref='list', lazy=True)
class Todo(db.Model):
__tablename__ = 'todo'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(), nullable=False)
completed = db.Column(db.Boolean, nullable=False, default=False)
list_id = db.Column(db.Integer, db.ForeignKey(
'todolists.id'), nullable=False)
def __repr__(self):
return f'<Todo {self.id} {self.description}>'
# db.create_all()
@app.route('/todos/create', methods=['POST'])
def create_todo():
error = False
body = {}
try:
description = request.get_json()['description']
list_id = request.get_json()['list_id']
new_item = Todo(description=description, completed=False, list_id=list_id)
db.session.add(new_item)
db.session.commit()
body['id'] = new_item.id
body['completed'] = new_item.completed
body['description'] = new_item.description
except:
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
abort(400)
if not error:
return jsonify(body)
@app.route('/todos/<todo_id>/set-completed', methods=['POST'])
def set_completed_todo(todo_id):
try:
completed = request.get_json()['completed']
todo = Todo.query.get(todo_id)
todo.completed = completed
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
return redirect(url_for('index'))
@app.route('/todos/<todo_id>', methods=['DELETE'])
def delete_todo(todo_id):
try:
Todo.query.filter_by(id=todo_id).delete()
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
return jsonify({'success': True})
@app.route('/lists/create', methods=['POST'])
def create_list():
error = False
body = {}
try:
name = request.get_json()['name']
new_list = TodoList(name=name)
db.session.add(new_list)
db.session.commit()
body['name'] = new_list.name
body['id'] = new_list.id
except:
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
abort(400)
if not error:
return jsonify(body)
@app.route('/lists/<list_id>', methods=['DELETE'])
def delete_list(list_id):
try:
Todo.query.filter_by(list_id=list_id).delete()
TodoList.query.filter_by(id=list_id).delete()
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
return jsonify({'success': True})
@app.route('/lists/<list_id>/set-done', methods=['POST'])
def set_done_list(list_id):
try:
done = request.get_json()['done']
todolist = TodoList.query.get(list_id)
todolist.done = done
todos = Todo.query.filter_by(list_id=list_id).all()
for i in range(len(todos)):
todo = todos[i]
todo.completed = done
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
return redirect(url_for('index'))
@app.route('/lists/<list_id>')
def get_list_todos(list_id):
return render_template('index.html',
lists=TodoList.query.order_by('id').all(),
active_list = TodoList.query.get(list_id),
todos=Todo.query.filter_by(list_id=list_id).order_by('id').all())
@app.route('/')
def index():
return redirect(url_for('get_list_todos'))
if __name__ == "__main__":
app.run(debug=True)