-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
63 lines (50 loc) · 1.97 KB
/
routes.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
from app import app
from flask import render_template, redirect, url_for, flash, get_flashed_messages
from forms import AddTaskForm, DeleteTaskForm
from models import Task, db
from datetime import datetime
@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
tasks = Task.query.all()
return render_template('index.html', tasks=tasks)
@app.route('/add', methods=['GET', 'POST'])
def add():
form = AddTaskForm()
if form.validate_on_submit():
task = Task(text=form.text.data, date=datetime.now())
db.session.add(task)
db.session.commit()
flash('The Task was successfully added to the Database.!')
return redirect(url_for('index'))
return render_template('add.html', form=form)
@app.route('/edit/<int:task_id>', methods=['GET', 'POST'])
def edit(task_id):
task = Task.query.get(task_id)
form = AddTaskForm()
if task:
if form.validate_on_submit():
task.text = form.text.data
task.data = datetime.now()
db.session.commit()
flash('The Task was successfully updated.!')
return redirect(url_for('index'))
form.text.data = task.text
return render_template('edit.html', form=form, task_id=task_id)
else:
flash("The Task ID does not exist.!")
return redirect(url_for('index'))
@app.route('/delete/<int:task_id>', methods=['GET', 'POST'])
def delete(task_id):
task = Task.query.get(task_id)
form = DeleteTaskForm()
if task:
if form.validate_on_submit():
db.session.delete(task)
db.session.commit()
flash("The Task was successfully deleted from the Database.!")
return redirect(url_for('index'))
return render_template('delete.html', form=form, task_id=task_id, text=task.text)
else:
flash("The Task ID does not exist.!")
return redirect(url_for('index'))