-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (64 loc) · 2.01 KB
/
app.py
File metadata and controls
79 lines (64 loc) · 2.01 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
from flask import Flask, render_template, request, url_for, redirect, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import sys
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://nureddin@localhost:5432/todoapp'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
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, default=False)
def __repr__(self):
return f'<Todo {self.id} {self.description}>'
@app.route('/')
def index():
data = Todo.query.order_by('id').all()
return render_template('index.html', data=data)
@app.route('/todos/create', methods=['POST'])
def create_todo():
error = False
body = {}
try:
description = request.get_json()['description']
todo = Todo(description=description)
db.session.add(todo)
db.session.commit()
body['description'] = todo.description
except:
error = True
db.session.rollback()
print(sys.exc_info)
finally:
db.session.close()
if error:
abort(400)
else:
return jsonify(body)
@app.route('/todos/<todoId>/set-completed', methods=['POST'])
def set_compleated_todo(todoId):
try:
completed = request.get_json()['completed']
todo = Todo.query.get(todoId)
todo.completed = completed
db.session.commit()
except:
db.session.rollback()
print(sys.exc_info)
finally:
db.session.close()
return jsonify({'success': True})
@app.route('/todos/<todoId>/delete', methods=['DELETE'])
def delete_todo_item(todoId):
try:
Todo.query.filter_by(id=todoId).delete()
db.session.commit()
except:
db.session.rollback()
print(sys.exc_info)
finally:
db.session.close()
return jsonify({'success': True})