generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
277 lines (231 loc) · 7.59 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os
from flask import (
Flask, flash, render_template, redirect,
request, session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
from forms import RegistrationForm, LoginForm, ReviewForm, EditForm, SearchForm
from extensions import csrf
if os.path.exists("env.py"):
import env
app = Flask(__name__)
csrf.init_app(app)
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
mongo = PyMongo(app)
@app.route("/")
@app.route("/list_materials")
def list_materials():
"""
Show list of materials on index/materials page
"""
materials = mongo.db.materials.find()
return render_template("materials.html", materials=materials)
@app.route("/register", methods=["GET", "POST"])
def register():
"""
User registration functionality
"""
form = RegistrationForm()
if form.validate_on_submit():
name = form.name.data.lower()
email = form.email.data
password = generate_password_hash(form.password.data)
existing_user = mongo.db.users.find_one(
{"username": name})
# Check if username is taken
if existing_user:
flash("Username already taken :(")
return redirect(url_for("register"))
# Add account to mongoDB
register = {
"username": name,
"email": email,
"password": password,
"admin": False
}
mongo.db.users.insert_one(register)
# Put the new user into session cookie
session["user"] = name
flash("Thank you for signing up!")
return redirect(url_for("profile", username=session["user"]))
return render_template(
"register.html",
form=form
)
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Log In functionality
"""
form = LoginForm()
if form.validate_on_submit():
name = form.name.data.lower()
password = form.password.data
# check if username exists in db
existing_user = mongo.db.users.find_one({"username": name})
if existing_user:
# check password
if check_password_hash(existing_user["password"], password):
session["user"] = name
flash("Sucessfully logged in!")
return redirect(url_for(
"profile", username=session["user"]))
else:
# invalid password
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
else:
# username doesn't exist
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
return render_template(
"login.html",
form=form
)
@app.route("/add_review", methods=["GET", "POST"])
def add_review():
"""
Add review functionality
"""
form = ReviewForm()
if form.validate_on_submit():
# Assign all parts of the form to variables and add review to mongoDB
review = {
"material_name": form.material_name.data,
"brand": form.brand.data,
"filament_name": form.filament_name.data,
"author": session["user"],
"rating": int(form.rating.data),
"temperature": form.temp.data,
"finish": form.finish.data,
"colour": form.colour.data,
"review_text": form.review.data,
"image_url": form.image.data,
"cost": int(form.cost.data),
"likes": 0
}
mongo.db.reviews.insert_one(review)
flash("Thanks for your review!")
return redirect(url_for("add_review"))
return render_template(
"add_review.html",
form=form
)
@app.route("/edit_review/<review_id>", methods=["GET", "POST"])
def edit_review(review_id):
"""
Edit review functionality
"""
review_to_edit = mongo.db.reviews.find_one({"_id": ObjectId(review_id)})
# Pass the existing review through to populate the form
form = EditForm(**review_to_edit)
if request.method == 'POST':
if form.validate_on_submit():
# Add updated review to mongoDB
updated_review = {
"material_name": form.material_name.data,
"brand": form.brand.data,
"filament_name": form.filament_name.data,
"author": session["user"],
"rating": int(form.rating.data),
"temperature": form.temperature.data,
"finish": form.finish.data,
"colour": form.colour.data,
"review_text": form.review_text.data,
"image_url": form.image_url.data,
"cost": int(form.cost.data),
"likes": 0
}
mongo.db.reviews.update(
{"_id": ObjectId(review_id)}, updated_review)
flash("Review successfully updated.")
return redirect(url_for(
"profile", username=session["user"]))
return render_template(
"edit_review.html",
form=form,
review_to_edit=review_to_edit
)
@app.route("/delete_review/<review_id>")
def delete_review(review_id):
"""
Delete reivew
"""
mongo.db.reviews.remove({"_id": ObjectId(review_id)})
flash("Review Sucessfully Deleted")
return redirect(url_for(
"profile", username=session["user"]))
@app.route("/profile/<username>", methods=["GET", "POST"])
def profile(username):
"""
Grab the session user's username from db
"""
reviews = mongo.db.reviews.find()
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
if session["user"]:
return render_template(
"profile.html", username=username, reviews=reviews)
return redirect(url_for("login"))
@app.route("/logout")
def logout():
"""
Remove user from session cookies
"""
flash("You have been logged out")
session.pop("user")
return redirect(url_for("login"))
@app.route("/admin_tools")
def admin_tools():
"""
Render Admin Tools Page (currently placeholder, no actual tools present)
"""
return render_template("admin_tools.html")
@app.route("/search", methods=["GET", "POST"])
def search():
"""
Search functionality
"""
reviews = list(mongo.db.reviews.find({"$text": {"$search": ""}}))
form = SearchForm()
if form.validate_on_submit():
query = form.query.data
reviews = list(mongo.db.reviews.find({"$text": {"$search": query}}))
return render_template(
"search.html",
form=form,
reviews=reviews
)
@app.route("/material/<material_name>", methods=["GET"])
def material(material_name):
"""
App route handling for material pages
"""
material_obj = mongo.db.materials.find({'path': material_name})
reviews = mongo.db.reviews.find({'material_name': material_name})
return render_template(
"material_page.html",
reviews=reviews,
material_obj=material_obj)
"""
Error Handling
"""
@app.errorhandler(404)
def not_found_error(error):
"""
404 Error Handler
"""
return render_template('404.html'), 404
@app.errorhandler(Exception)
def internal_error(error):
"""
General Error Handler
"""
return render_template('500.html'), 500
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)