-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
118 lines (88 loc) · 2.88 KB
/
app.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
"""
------------------------------- Imports --------------------------------
"""
import os
from datetime import datetime
from flask import Flask, render_template, redirect, url_for, request, session
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from forms import AddTodo
"""
------------------------------- App Setup ------------------------------
"""
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(12).hex()
"""
--------------------------- DB Setup & Migrate -------------------------
"""
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///"+os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app, db)
"""
-------------------------------- Models -------------------------------
"""
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
task = db.Column(db.String(100), unique=True, nullable=False)
description = db.Column(db.Text, nullable=True)
done = db.Column(db.Boolean, nullable=False)
completed_on = db.Column(db.DateTime, nullable=True)
def __init__(self, task, done):
self.task = task
self.done = done
def __repr__(self):
return self.task
"""
-------------------------------- Views --------------------------------
"""
@app.route('/', methods=['GET', 'POST'])
def todo():
form = AddTodo()
if request.method == 'POST':
if form.validate_on_submit():
item = Todo(task=form.item.data, done=False)
db.session.add(item)
db.session.commit()
return redirect(url_for('todo'))
else:
items = Todo.query.filter_by(done=False).all()
return render_template('todo.html', form=form, items=items)
@app.route('/edit/<int:pk>', methods=['POST'])
def edit(pk):
if request.method == 'POST':
item = Todo.query.get(pk)
item.task = request.form['task']
item.description = request.form['description']
db.session.commit()
return redirect(url_for('todo'))
@app.route('/done/<int:pk>')
def done(pk):
item = Todo.query.get(pk)
item.done = True
item.completed_on = datetime.now()
db.session.commit()
return redirect(url_for('todo'))
@app.route('/delete/<int:pk>')
def delete(pk):
item = Todo.query.get(pk)
db.session.delete(item)
db.session.commit()
return redirect(url_for('todo'))
@app.route('/completed')
def completed():
items = Todo.query.filter_by(done=True).all()
return render_template('completed.html', items=items)
@app.route('/undone/<int:pk>')
def undone(pk):
item = Todo.query.get(pk)
item.done = False
item.completed_on = None
db.session.commit()
return redirect(url_for('completed'))
"""
-------------------------------- Run App -------------------------------
"""
if __name__ == '__main__':
app.run(debug=True)