generated from github/haikus-for-codespaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
221 lines (171 loc) · 6.87 KB
/
app.py
File metadata and controls
221 lines (171 loc) · 6.87 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Importing desired libraries"""
import sqlite3
from flask import Flask, redirect, render_template, request, session, jsonify
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology, login_required
from flask_session import Session
from chat import get_response
# Configure session
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Ensure that templates are auto-reloaded
app.config["TEMPLATES_AUTO-RELOAD"] = True
# Connect to the SQLite database
conn = sqlite3.connect("chattrbox.db", check_same_thread=False)
db = conn.cursor() # Creating a cursor object
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
@login_required
def index():
"""Entering the chat room"""
return render_template("chat.html")
@app.route("/welcome", methods=["GET", "POST"])
@login_required
def welcome():
"""Welcoming the user!"""
db.execute("SELECT * FROM users WHERE id = ?", (session["user_id"],))
rows = db.fetchall()
username = rows[0][1]
if request.method == "POST": # If method is "post"
return redirect("/")
# If method is "get"
return render_template("welcome.html", username=username)
@app.route("/predict", methods=["POST"])
@login_required
def predict():
text = request.get_json().get("message")
response = get_response(text)
message = {"answer": response}
return jsonify(message)
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)
# Ensure password was submitted
if not request.form.get("password"):
return apology("must provide password", 403)
username = request.form.get("username")
# Query database for username
db.execute("SELECT * FROM users WHERE username = ?", (username,))
rows = db.fetchall()
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0][2], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0][0]
# Redirect user to home page
return redirect("/welcome")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if request.method == "POST":
# Load up the users database to check for already existing users
db.execute("SELECT username FROM users")
usernames_tuples = db.fetchall()
usernames = []
for i in range(len(usernames_tuples)):
if usernames_tuples == []:
break
else:
usernames.append(usernames_tuples[i][0])
# Store user's username and password
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
hashed_password = generate_password_hash(password)
# Validate username and password
if username in usernames:
return apology("Username aleady exists! Try different usernames out")
elif not username or not password:
return apology("Enter both username and password!")
elif not confirmation:
return apology("Please confirm your password!")
elif password != confirmation:
return apology("Passwords do not match")
else:
db.execute("INSERT INTO users (username, hash) VALUES (?, ?)", (username, hashed_password))
conn.commit()
db.execute("SELECT id FROM users WHERE username = ?", (username,))
user_id = db.fetchall()
session["user_id"] = user_id[0][0]
return redirect("/welcome")
return render_template("register.html")
@app.route("/changepassword", methods=["GET", "POST"])
@login_required
def change_password():
"""Changing user's password"""
if request.method == "POST":
password = request.form.get("password")
newpassword = request.form.get("newpassword")
confirmation = request.form.get("confirmation")
if not password:
return apology("Give your old password")
elif not newpassword or not confirmation:
return apology("Enter your password and confirm it as well")
elif newpassword != confirmation:
return apology("Enter the same password again")
else:
# Query database for id
db.execute("SELECT * FROM users WHERE id = ?", (session["user_id"],))
rows = db.fetchall()
# Check if the old password entered is correct
if not check_password_hash(rows[0][2], password):
return apology("Enter your current password correctly")
db.execute("UPDATE users SET hash = (?) WHERE id = ?", (generate_password_hash(password), session["user_id"]))
conn.commit()
return redirect("/")
else:
return render_template("change-password.html")
@app.route("/deleteaccount", methods=["GET", "POST"])
@login_required
def delete_account():
"""Deleting user's account"""
if request.method == "POST":
password = request.form.get("password")
if not password:
return apology("Why is your password field empty, HUH?")
db.execute("SELECT * FROM users WHERE id = ?", (session["user_id"],))
rows = db.fetchall()
if not check_password_hash(rows[0][2], password):
return apology("Your password is incorrect!")
return redirect("/goodbye")
return render_template("delete.html")
@app.route("/goodbye", methods=["GET", "POST"])
@login_required
def goodbye():
"""Bidding user a farewell"""
if request.method == "POST":
db.execute("DELETE FROM users WHERE id = ?", (session["user_id"],))
conn.commit()
return redirect("/login")
return render_template("goodbye.html")
@app.route("/aboutus")
@login_required
def aboutus():
"""Redirect to About section"""
return render_template("aboutus.html")
if __name__=="__main__":
app.run(host="0.0.0.0", port=5000)