-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_app.py
More file actions
112 lines (82 loc) · 3.06 KB
/
Copy pathsql_app.py
File metadata and controls
112 lines (82 loc) · 3.06 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
from flask import Flask, redirect, render_template, request, url_for, jsonify
from flask_cors import CORS, cross_origin
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
@app.route('/delete', methods=['OPTIONS'])
@cross_origin()
def options():
return '', 204
CORS(app, resources={
r"/add": {"origins": "*"},
r"/todo": {"origins": "*"},
r"/complete/*": {"origins": "*"},
r"/delete/*": {"origins": "*"},
r"/delete": {"origins": "*"},
})
#with app.app_context():
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir + "app.sqlite")
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://apapu:october@localhost/todos'
db = SQLAlchemy(app)
# todo
class Todo(db.Model):
__tablename__ = "todos"
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(200))
complete = db.Column(db.Boolean)
def todo_to_json(todo):
return {
'id': todo.id,
'text': todo.text,
'complete': todo.complete
}
@app.route('/todo')
def index():
incomplete = Todo.query.filter_by(complete=False).all()
complete = Todo.query.filter_by(complete=True).all()
incomplete_json = [todo_to_json(todo) for todo in incomplete]
complete_json = [todo_to_json(todo) for todo in complete]
return jsonify(incomplete=incomplete_json, complete=complete_json)
@app.route('/add', methods=['POST'])
def add():
try:
data = request.get_json()
new_task_text = data.get('todoitem') # Obtiene el campo 'todoitem'
if new_task_text:
new_task = Todo(text=new_task_text, complete=False)
db.session.add(new_task)
db.session.commit()
return jsonify(message='Tarea agregada con éxito')
return jsonify(error='Falta el campo "todoitem" en la solicitud'), 400
except Exception as e:
return jsonify(error='Error al procesar la solicitud'), 500
@app.route('/complete/<id>', methods=['PUT'])
def complete(id):
try:
todo = Todo.query.filter_by(id=int(id)).first()
if todo:
todo.complete = True
db.session.commit()
return jsonify(message='Tarea marcada como completa con éxito')
else:
return jsonify(error='Tarea no encontrada'), 404
except Exception as e:
return jsonify(error='Error al procesar la solicitud'), 500
@app.route('/delete/<id>', methods=['DELETE'])
def delete(id):
try:
todo = Todo.query.filter_by(id=int(id)).first()
if todo:
if todo.complete:
db.session.delete(todo)
db.session.commit()
return jsonify(message='Tarea eliminada con éxito')
else:
return jsonify(error='Tarea no está marcada como completa'), 400
else:
return jsonify(error='Tarea no encontrada'), 404
except Exception as e:
return jsonify(error='Error al procesar la solicitud'), 500
if __name__ == '__main__':
app.run(debug=True)