-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (56 loc) · 1.99 KB
/
main.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
import unittest
from flask import request, make_response, redirect, render_template, session, url_for, flash
from flask_login import login_required, current_user
from app import create_app
from app.forms import TodoForm, DeleteTodoForm, UpdateTodoForm
from app.firestore_service import get_todos, put_todo, delete_todo, update_todo
app = create_app()
@app.cli.command()
def test():
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner().run(tests)
@app.errorhandler(500)
def error_500(error):
return render_template('500.html', error=error)
@app.errorhandler(404)
def not_found(error):
return render_template('404.htm', error=error)
@app.route('/')
def index():
user_ip = request.remote_addr
response = make_response(redirect('/hello'))
session['user_ip'] = user_ip
return response
@app.route('/hello', methods=['GET', 'POST'])
@login_required
def hello():
user_ip = session.get('user_ip')
username = current_user.id
todo_form = TodoForm()
delete_form = DeleteTodoForm()
update_form = UpdateTodoForm()
context = {
'user_ip': user_ip,
'todos': get_todos(user_id=username),
'username': username,
'todo_form': todo_form,
'delete_form': delete_form,
'update_form': update_form,
}
if todo_form.validate_on_submit():
put_todo(user_id=username, description=todo_form.description.data)
flash(' Tarea creada con exito')
return redirect(url_for('hello'))
return render_template('hello.html', **context)
@app.route('/todos/delete/<todo_id>', methods=['POST'])
def delete(todo_id):
user_id = current_user.id
delete_todo(user_id=user_id, todo_id=todo_id)
return redirect(url_for('hello'))
@app.route('/todos/update/<todo_id>/<int:done>', methods=['POST'])
def update(todo_id, done):
user_id = current_user.id
update_todo(user_id=user_id, todo_id=todo_id, done=done)
return redirect(url_for('hello'))
# if __name__ == '__main__':
# pass