-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
218 lines (173 loc) · 5.29 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from flask import Flask, render_template, request, redirect, url_for, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import sys
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://nicholasbates@localhost:5432/todoapp'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# Our file app.py is refered to as our server in the model, view, controller context
# Our Todo class is the model and the index.html is the view
class Todo(db.Model):
__tablename__ = 'todos'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(), nullable=False)
completed = db.Column(db.Boolean, default=False)
list_id = db.Column(db.Integer, db.ForeignKey(
'todolists.id'), nullable=False)
def __repr__(self):
return f'<Todo ID: {self.id}, description: {self.description}, completed: {self.completed}>'
class TodoList(db.Model):
__tablename__ = 'todolists'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(), nullable=False)
todos = db.relationship('Todo', backref='list', lazy=True)
def __repr__(self):
return f'<TodoList ID: {self.id}, name: {self.name}, todos: {self.todos}>'
# index is the controller in this app
# note: more conventionally, we would write a
# POST endpoint to /todos for the create endpoint:
# @app.route('/todos', method=['POST'])
@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']
todo = Todo(description=description, completed=False, list_id=list_id)
db.session.add(todo)
db.session.commit()
body['id'] = todo.id
body['completed'] = todo.completed
body['description'] = todo.description
except():
db.session.rollback()
error = True
print(sys.exc_info())
finally:
db.session.close()
if error:
abort(500)
else:
return jsonify(body)
@app.route('/todos/<todo_id>/set-complete', methods=['POST'])
def update_todo(todo_id):
error = False
try:
todo = Todo.query.get(todo_id)
print('Todo: ', todo)
todo.completed = request.get_json()['complete']
db.session.commit()
except():
db.session.rollback()
error = True
print(sys.exc_info())
finally:
db.session.close()
if error:
abort(500)
else:
return redirect(url_for('index'))
@app.route('/todos/<todo_id>/delete', methods=['DELETE'])
def delete_todo(todo_id):
error = False
try:
# Todo.query.filter_by(id=todo_id).delete()
todo = Todo.query.get(todo_id)
db.session.delete(todo)
db.session.commit()
except():
db.session.rollback()
error = True
finally:
db.session.close()
if error:
abort(500)
else:
return jsonify({'success': True})
@app.route('/todos/<todo_id>/set-completed', methods=['POST'])
def set_completed_todo(todo_id):
error = False
try:
completed = request.get_json()['completed']
todo = Todo.query.get(todo_id)
todo.completed = completed
db.session.commit()
except:
db.session.rollback()
error = True
finally:
db.session.close()
if error:
abort(500)
else:
return '', 200
@app.route('/')
def index():
return redirect(url_for('get_list_todos', list_id=1))
@app.route('/lists/<list_id>')
def get_list_todos(list_id):
lists = TodoList.query.all()
active_list = TodoList.query.get(list_id)
todos = Todo.query.filter_by(list_id=list_id).order_by('id').all()
return render_template('index.html', todos=todos, lists=lists, active_list=active_list)
@app.route('/lists/create', methods=['POST'])
def create_list():
error = False
body = {}
try:
name = request.get_json()['name']
todolist = TodoList(name=name)
db.session.add(todolist)
db.session.commit()
body['id'] = todolist.id
body['name'] = todolist.name
except():
db.session.rollback()
error = True
print(sys.exc_info)
finally:
db.session.close()
if error:
abort(500)
else:
return jsonify(body)
@app.route('/lists/<list_id>/delete', methods=['DELETE'])
def delete_list(list_id):
error = False
try:
list = TodoList.query.get(list_id)
for todo in list.todos:
db.session.delete(todo)
db.session.delete(list)
db.session.commit()
except():
db.session.rollback()
error = True
finally:
db.session.close()
if error:
abort(500)
else:
return jsonify({'success': True})
@app.route('/lists/<list_id>/set-completed', methods=['POST'])
def set_completed_list(list_id):
error = False
try:
list = TodoList.query.get(list_id)
for todo in list.todos:
todo.completed = True
db.session.commit()
except:
db.session.rollback()
error = True
finally:
db.session.close()
if error:
abort(500)
else:
return '', 200
if __name__ == '__main__':
app.run(debug=True)