generated from Code-Institute-Org/gitpod-full-template
/
app.py
279 lines (206 loc) · 9.39 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
278
279
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
if os.path.exists("env.py"):
import env
# Creating an instance of flask
app = Flask(__name__)
# Config to grab database
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")
# Pymongo instance to add app using constructure method
mongo = PyMongo(app)
@app.route("/")
@app.route("/index")
def index():
local_business = list(mongo.db.business.find())
return render_template("index.html", results=local_business)
@app.route("/search", methods=["GET", "POST"])
def search():
query_one = request.form.get("query-1")
query_two = request.form.get("query-2")
query = str(query_one + " " + query_two)
business = list(mongo.db.business.find({"$text": {"$search": query}}))
return render_template("results.html", results=business, query=query)
@app.route("/register", methods=["GET", "POST"])
def register():
# Retrieves categories of business from db
categories = mongo.db.category.find().sort("category_name", 1)
if request.method == "POST":
# Checks if username already exists in the db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
# Checks if email already exists in the db
existing_email = mongo.db.business.find_one(
{"email": request.form.get("email").lower()})
# Verify email matches
email_one = request.form.get("email")
email_two = request.form.get("email2")
# Verify password matches
password_one = request.form.get("password")
password_two = request.form.get("password2")
if existing_user or existing_email:
flash("Username/Email already exists")
return redirect(url_for("register"))
elif email_one != email_two:
flash("Emails must match to verify")
return redirect(url_for("register"))
elif password_one != password_two:
flash("Passwords must match to verify")
return redirect(url_for("register"))
category_id = mongo.db.category.find_one(
{"category_name": request.form.get("category_name")})
register_business = {
"name": request.form.get("business_name"),
"website": request.form.get("website").lower(),
"email": request.form.get("email").lower(),
"address": request.form.get("address"),
"category_name": category_id["category_name"],
"category_id": category_id["_id"]
}
mongo.db.business.insert_one(register_business)
register_user = {
"username": request.form.get("username").lower(),
"password": generate_password_hash(request.form.get("password")),
"business_id": register_business["_id"]
}
mongo.db.users.insert_one(register_user)
# Puts the new user into 'session' cookie
session["user"] = request.form.get("username").lower()
flash("Registration Successful!")
return redirect(url_for("profile", username=session["user"]))
return render_template("register.html", categories=categories)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
# Checks if username exists in the db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# Checks is password matches db
if check_password_hash(
existing_user["password"], request.form.get("password")):
session["user"] = request.form.get("username").lower()
flash("Welcome, {}".format(request.form.get("username")))
if str(session["user"]) == "admin":
return redirect(url_for("admin_page",
admin_user=session["user"]))
else:
return redirect(url_for("profile",
username=session["user"]))
else:
# If password does not match
flash("Incorrect Username/Password")
return redirect(url_for("login"))
else:
# If username does not match
flash("Incorrect Username/Password")
return redirect(url_for("login"))
return render_template("login.html")
@app.route("/admin_page/<admin_user>", methods=["GET", "POST"])
def admin_page(admin_user):
if str(session["user"]) == "admin":
admin_name = mongo.db.users.find_one(
{"username": session["user"]})["username"]
if request.method == "POST":
exisitng_category = mongo.db.category.find_one(
{"category_name": request.form.get("business-type").lower()})
if exisitng_category:
flash("Category already exists")
return redirect(url_for("admin_page",
admin_user=session["user"]))
add_category = {
"category_name": request.form.get("business-type")
}
mongo.db.category.insert_one(add_category)
flash("Category added")
return redirect(url_for("admin_page", admin_user=session["user"]))
return render_template("admin_page.html", username=admin_name)
return redirect(url_for("index"))
@app.route("/profile/<username>", methods=["GET", "POST"])
def profile(username):
# Retrieves session user's username from the db
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
business_id = list(mongo.db.business.find({"_id": mongo.db.users.find_one(
{"username": session["user"]})["business_id"]}))
if session["user"]:
return render_template(
"profile.html", username=username, business_id=business_id)
return redirect(url_for("index"))
@app.route("/edit_info/<edit_business>", methods=["GET", "POST"])
def edit_info(edit_business):
# Retrieves categories of business from db
categories = mongo.db.category.find().sort("category_name", 1)
if request.method == "POST":
# Checks if email already exists in the db
existing_email = mongo.db.business.find_one(
{"email": request.form.get("email").lower()})
current_email = mongo.db.business.find_one(
{"_id": ObjectId(edit_business)})["email"]
email_input = request.form.get("email").lower()
# Verify email matches
email_one = request.form.get("email")
email_two = request.form.get("email2")
if email_one != email_two:
flash("Emails must match to verify")
return redirect(url_for("edit_info", edit_business=edit_business))
elif current_email != email_input:
if existing_email:
flash("Email is already exists")
return redirect(url_for("edit_info",
edit_business=edit_business))
# Retrieves current category id from business to remove it
current_category_id = mongo.db.business.find_one(
{"_id": ObjectId(edit_business)})
current_category_id.pop("category_id")
category_id = mongo.db.category.find_one(
{"category_name": request.form.get("category_name")})
update_business = {
"name": request.form.get("business_name"),
"website": request.form.get("website").lower(),
"email": request.form.get("email").lower(),
"address": request.form.get("address"),
"category_name": category_id["category_name"],
"category_id": category_id["_id"]
}
mongo.db.business.update(
{"_id": ObjectId(edit_business)}, update_business)
flash("Business Information was updated!")
return redirect(url_for("profile", username=session["user"]))
business_id = mongo.db.business.find_one(
{"_id": ObjectId(edit_business)})
user_business = mongo.db.users.find_one(
{"username": session["user"]})["business_id"]
if session["user"]:
if str(edit_business) == str(user_business):
return render_template(
"edit_info.html",
categories=categories, business_id=business_id)
return redirect(url_for("index"))
@app.route("/delete_probus/<delete_business>")
def delete_probus(delete_business):
# Retrieves users ObjectID
user_id = mongo.db.users.find_one(
{"username": session["user"]})["_id"]
if session["user"]:
flash("Your Profile and Business have been removed from our database")
session.pop("user")
mongo.db.business.remove({"_id": ObjectId(delete_business)})
mongo.db.users.remove({"_id": ObjectId(user_id)})
return redirect(url_for("index"))
@app.route("/logout")
def logout():
# Remove user from session cookies
flash("You have been logged out")
session.pop("user")
return redirect(url_for("index"))
# Change debug to TRUE for debugging
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")), debug=False)