-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (87 loc) · 3.3 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
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
from flask import Flask, request, redirect, render_template, session, flash
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://get-it-done:beproductive@localhost:8889/get-it-done'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
app.secret_key = 'y337kGcys&zP3B'
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120))
completed = db.Column(db.Boolean)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __init__(self, name, owner):
self.name = name
self.completed = False
self.owner = owner
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120))
tasks = db.relationship('Task', backref= 'owner')
def __init__(self, email, password):
self.email = email
self.password = password
@app.before_request
def require_login():
allowed_routes = ['login', 'register']
if request.endpoint not in allowed_routes and 'email' not in session:
return redirect('/login')
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email).first()
if user and user.password == password:
session['email'] = email
flash("Logged In")
return redirect('/')
else:
flash('User password incorrect, or user does not exist', 'error')
return render_template('login.html')
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
verify = request.form['verify']
# TODO - validate user's data
existing_user = User.query.filter_by(email=email).first()
if not existing_user:
new_user = User(email, password)
db.session.add(new_user)
db.session.commit()
session['email'] = email
return redirect('/')
else:
# TODO - user better response messaging
return "<h1>Duplicate user</h1>"
return render_template('register.html')
@app.route('/logout')
def logout():
del session['email']
return redirect('/')
@app.route('/', methods=['POST', 'GET'])
def index():
owner = User.query.filter_by(email=session['email']).first()
if request.method == 'POST':
task_name = request.form['task']
new_task = Task(task_name, owner)
db.session.add(new_task)
db.session.commit()
tasks = Task.query.filter_by(completed=False,owner=owner).all()
completed_tasks = Task.query.filter_by(completed=True,owner=owner).all()
return render_template('todos.html',title="Get It Done!",
tasks=tasks, completed_tasks=completed_tasks)
@app.route('/delete-task', methods=['POST'])
def delete_task():
task_id = int(request.form['task-id'])
task = Task.query.get(task_id)
task.completed = True
db.session.add(task)
db.session.commit()
return redirect('/')
if __name__ == '__main__':
app.run()