-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
165 lines (133 loc) · 4.93 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from flask import Flask, render_template, redirect, url_for, flash, request
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired, URL, Email
from flask_ckeditor import CKEditor
import os
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, current_user, logout_user
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
ckeditor = CKEditor(app)
Bootstrap(app)
##CONNECT TO DB
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///users.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
class RegisterForm(FlaskForm):
email = StringField("Email", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired()])
name = StringField("Name", validators=[DataRequired()])
submit = SubmitField("Sign Me Up!")
class LoginForm(FlaskForm):
email = StringField("Email", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
submit = SubmitField("Let Me In!")
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(100))
to_do_items = db.Column(db.String(5000))
db.create_all()
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route("/todo")
@app.route('/')
def home():
if current_user.is_authenticated:
user = User.query.get(current_user.id)
to_do = user.to_do_items
try:
if len(to_do) > 0:
task_list = to_do.strip("["", ]""'"",").split("', '")
except TypeError:
task_list = []
else:
task_list = []
return render_template('index.html', items=task_list)
@app.route('/<items>')
def home_items(items):
if current_user.is_authenticated:
user = User.query.get(current_user.id)
to_do = user.to_do_items
try:
if len(to_do) > 0:
task_list = to_do.strip("["", ]""'"",").split("', '")
except TypeError:
task_list = []
else:
task_list = items.strip("["", ]""'"",").split("', '")
return render_template('index.html', items=task_list)
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password, password):
login_user(user)
return redirect(url_for('home'))
return render_template('login.html', form=form)
@app.route('/register', methods=["GET", "POST"])
def register():
form = RegisterForm()
if form.validate_on_submit():
hash_and_salted_password = generate_password_hash(
form.password.data,
method='pbkdf2:sha256',
salt_length=8
)
new_user = User(
email=form.email.data,
name=form.name.data,
password=hash_and_salted_password
)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for('home'))
return render_template('register.html', form=form)
@app.route('/add/<task_list>', methods=["GET", "POST"])
def add_item(task_list):
item = request.form["new_list_item"]
new_task_list = task_list.strip("[""]""'"",").split("', '")
new_task_list.append(item)
if current_user.is_authenticated:
string = "["
for item in new_task_list:
string += "'" + item + "', "
string += "]"
print(string)
user = User.query.get(current_user.id)
user.to_do_items = string
db.session.commit()
return redirect(url_for('home_items', items=new_task_list))
@app.route('/done/<item>-<task_list>', methods=["GET","POST"])
def done(item, task_list):
new_task_list = task_list.strip("[""]""'"",").split("', '")
item_to_remove = new_task_list.index(item)
new_task_list.remove(new_task_list[item_to_remove])
if current_user.is_authenticated:
string = "["
for item in new_task_list:
string += "'" + item + "', "
string += "]"
print(string)
user = User.query.get(current_user.id)
user.to_do_items = string
db.session.commit()
return redirect(url_for('home_items', items=new_task_list))
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('home'))
if __name__ == "__main__":
app.run(debug=True)