-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
199 lines (165 loc) · 5.9 KB
/
app.py
File metadata and controls
199 lines (165 loc) · 5.9 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
# tells the application where our database will be stored
db = SQLAlchemy(app)
# initialise connection to the database
class User(db.Model): # create a new class which inherits from a basic database model, provided by SQLAlchemy
# SQLAlchemy also creates a table called user, which it will use to store our User objects.
id = db.Column(
db.Integer,
primary_key=True,
)
name = db.Column(
db.String(50),
unique=True,
nullable=False,
)
email = db.Column(
db.String(50),
nullable=False,
)
username = db.Column(
db.String(30),
primary_key=True,
)
password = db.Column(
db.String(30),
nullable=False,
)
# defines how to represent our User object as a string. This allows us to do things like print(User)
def __repr__(self):
return '<User %r>' % self.id
class ToDo(db.Model):
# SQLAlchemy create a table called "toDo", which it will use to store our "ToDo" objects.
id = db.Column(
db.Integer,
primary_key=True
)
content = db.Column(
db.String(200),
nullable=False
)
date_created = db.Column(
db.DateTime,
default=datetime.utcnow
)
user_id = db.Column(
db.Integer,
db.ForeignKey('user.id'),
nullable=False,
)
user = db.relationship('User',
backref=db.backref('to_dos', lazy=True))
def __repr__(self):
return '<Task %r>' % self.id
class RegisterForm(Form):
name = StringField('Name', validators=[validators.Length(min=1,max=50)])
email = StringField('Email', validators=[validators.Length(min=1,max=50), validators.Email()])
username = StringField('Username', validators=[validators.Length(min=4,max=30)])
password = PasswordField('Password',
validators=[validators.InputRequired(),
validators.EqualTo('confirm', message="Passwords do not match!")])
confirm = PasswordField('Confirm Password')
class LoginForm(Form):
username = StringField('Username', validators=[validators.Length(min=4,max=30)])
password = PasswordField('Password',validators=[validators.InputRequired()])
@app.route("/")
def home():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/register", methods=['GET','POST'])
def register():
if request.method == 'GET':
form = RegisterForm()
return render_template("register.html",form=form)
else:
form = RegisterForm(request.form)
if form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = form.password.data
new_user = User(name=name, email=email, username=username, password=password)
print("User created!")
try:
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
except:
return "There was an issue in adding this user in our database!"
else:
return "Form not valid!"
@app.route("/allusers")
def allusers():
all_users = User.query.order_by(User.id).all()
return render_template("all_users.html", all_users = all_users)
@app.route("/login", methods=['GET','POST'])
def login():
if request.method == 'GET':
form = LoginForm()
return render_template("login.html", form=form)
else:
form = LoginForm(request.form)
if form.validate():
username = form.username.data
passw = form.password.data
try:
user = User.query.filter_by(username=username)
if user is not None and passw == user.password:
session.logged_in = True
return redirect(url_for('home'),current_user=user)
else:
return redirect(url_for('register'))
except:
return redirect(url_for('register'))
else:
return "Form not validated!"
@app.route("/logout")
def logout():
session.logged_in = False
return render_template("home.html")
@app.route("/tasks/<int:id>", methods = ['GET','POST'])
def tasks(id):
if request.method == 'GET':
current_tasks = ToDo.query.filter_by(user_id=id).order_by(ToDo.date_created)
return render_template("tasks.html", tasks = current_tasks)
else:
task_content = request.form['content']
new_task = ToDo(content=task_content, user_id=id)
try:
db.session.add(new_task)
db.session.commit()
return redirect("/tasks")
except:
return "There was an issue adding the new task to our database!"
@app.route("/deleteTask/<int:id>")
def delete(id):
task_to_delete = ToDo.query.get_or_404(id)
try:
db.session.delete(task_to_delete)
db.session.commit()
return redirect("/tasks")
except:
return "There was an issue deleting that task!"
@app.route("/updateTask/<int:id>", methods=['GET','POST'])
def update(id):
task_to_update = ToDo.query.get_or_404(id)
if request.method == 'GET':
return render_template("updateTask.html", task = task_to_update)
else:
new_content = request.form['content']
task_to_update.content = new_content
try:
db.session.commit()
return redirect("/tasks")
except:
return "There was an issue updating that task!"
if __name__ == "__main__":
app.run(debug=True)