-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
210 lines (168 loc) · 4.56 KB
/
app.py
File metadata and controls
210 lines (168 loc) · 4.56 KB
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
from flask import Flask, render_template, abort, url_for, jsonify, abort, redirect
from flask_sqlalchemy import SQLAlchemy
from flask import request
from flask_migrate import Migrate
import sys
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:Fakiha024@localhost:5433/todos'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# TODOS CRUD
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, nullable=False)
list_id = db.Column(db.Integer, db.ForeignKey('todolists.id'), nullable=False)
def __repr__(self):
return f'<Todo {self.id} {self.description}>'
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)
# Create todo item
@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:
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
abort (500)
else:
return jsonify(body)
# Delete a todo item
@app.route('/todos/<todo_id>', methods=['DELETE'])
def delete_todo(todo_id):
error = False
try:
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-complete', methods=['POST'])
def update_todo(todo_id):
error = False
try:
complete = request.get_json()['complete']
todo = Todo.query.get(todo_id)
print('Todo: ', todo)
todo.complete = 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>/set-completed', methods=['POST'])
def set_completed_todo(todo_id):
error = False
try:
completed = request.get_json()['completed']
# print('completed', completed)
todo = Todo.query.get(todo_id)
todo.completed = completed
db.session.commit()
except:
db.session.rollback()
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))
# LISTS CRUD
@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>/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
@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/<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})
if __name__ == '__main__':
app.run(debug=True)