-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
125 lines (108 loc) · 3.72 KB
/
views.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
from forms import AddTaskForm, RegisterForm, LoginForm
from functools import wraps
from flask import Flask, flash, redirect, render_template, request, \
session, url_for
from flask.ext.sqlalchemy import SQLAlchemy
import datetime
# config
app = Flask(__name__)
app.config.from_object('_config')
db = SQLAlchemy(app)
from models import Task, User
# helper functions
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to log in first')
return redirect(url_for('login'))
return wrap
def flash_errors(form):
for field, errors in forms.errors.items():
for error in errors:
flash(u"Error in the %s field - %s" (getattr(form, field).label.text, error), 'error')
# route handlers
@app.route('/logout/')
def logout():
session.pop('logged_in', None)
session.pop('user_id', None)
flash('Goodbye')
return redirect(url_for('login'))
@app.route('/', methods=['GET', 'POST'])
def login():
error = None
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
user = User.query.filter_by(name=request.form['name']).first()
if user is not None and user.password == request.form['password']:
session['logged_in'] = True
session['user_id'] = user.id
flash('Welcome')
return redirect(url_for('tasks'))
else:
error = 'Invalid credentials'
else:
error = 'Both fields are required'
return render_template('login.html', form=form, error=error)
@app.route('/tasks/')
@login_required
def tasks():
open_tasks = db.session.query(Task) \
.filter_by(status='1').order_by(Task.due_date.asc())
closed_tasks = db.session.query(Task) \
.filter_by(status='0').order_by(Task.due_date.asc())
return render_template('tasks.html',
form=AddTaskForm(request.form),
open_tasks=open_tasks,
closed_tasks=closed_tasks)
@app.route('/add/', methods=['GET', 'POST'])
@login_required
def new_task():
form = AddTaskForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
new_task = Task(form.name.data,
form.due_date.data,
form.priority.data,
datetime.datetime.utcnow(),
'1',
session['user_id'])
db.session.add(new_task)
db.session.commit()
flash('New entry was successfully posted.')
return redirect(url_for('tasks'))
@app.route('/complete/<int:task_id>/')
@login_required
def complete(task_id):
new_id = task_id
db.session.query(Task).filter_by(task_id=new_id).update({"status": "0"})
db.session.commit()
flash('The task was marked as complete.')
return redirect(url_for('tasks'))
@app.route('/delete/<int:task_id>/')
@login_required
def delete_entry(task_id):
new_id = task_id
db.session.query(Task).filter_by(task_id=new_id).delete()
db.session.commit()
flash('The task was deleted.')
return redirect(url_for('tasks'))
@app.route('/register/', methods=['GET', 'POST'])
def register():
error = None
form = RegisterForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
new_user = User(
form.name.data,
form.email.data,
form.password.data,
)
db.session.add(new_user)
db.session.commit()
flash('Thanks for registering.')
return redirect(url_for('login'))
return render_template('register.html', form=form, error=error)