aboutsummaryrefslogtreecommitdiff
path: root/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'server.py')
-rw-r--r--server.py61
1 files changed, 45 insertions, 16 deletions
diff --git a/server.py b/server.py
index 013089a..7840ea3 100644
--- a/server.py
+++ b/server.py
@@ -1,17 +1,16 @@
from flask import Flask, Response, render_template, request, session, redirect, url_for
+from flask_bcrypt import Bcrypt
import functools
import re
-from scripts.database.mongo import mongo_database
+import Constants
from scripts.scraping.scraper import scrape
-from scripts.recipes.handle_recipes import recipe_exists, add_single_recipe, get_all_recipes, get_single_recipe, get_facets, delete_single_recipe, search_recipes
-from scripts.users.handle_users import user_exists, authenticate_user, add_user
-from flask_bcrypt import Bcrypt
+from scripts.recipes.handle_recipes import recipe_exists, add_single_recipe, get_all_recipes, get_single_recipe, get_facets, delete_single_recipe, search_recipes, delete_multiple_recipes
+from scripts.users.handle_users import user_exists, add_user, delete_single_user
-# Bcrypt.check_password_hash(hashed_password, "eligible password")
app = Flask(__name__)
bcrypt = Bcrypt(app)
app.config["TEMPLATES_AUTO_RELOAD"] = True
-app.secret_key = 'wdkbahjfbqb'
+app.secret_key = Constants.SECRET_KEY
# Decorator function to check for username in session to protect routes
def login_required(func):
@@ -26,6 +25,7 @@ def login_required(func):
@app.route("/home")
@login_required
def home_page():
+ print(session["username"])
tags = get_facets(session["username"])
all_recipes = get_all_recipes(session["username"])
return render_template("/pages/home.html",recipes=all_recipes, facets=tags)
@@ -38,6 +38,13 @@ def landing_page():
@login_required
def single_recipe_page(recipe_id):
recipe_details = get_single_recipe(recipe_id)
+
+ # If there are instruction sections, be sure to render correctly
+ if "subInstructions" in recipe_details["instructions"][0]:
+ new_instructions = [{"i": x["subInstructions"], "n": x["name"]} for x in recipe_details["instructions"]]
+ recipe_details["instructions"] = new_instructions
+ recipe_details["has_subsections"] = True
+
return render_template("/pages/single-recipe.html", single_recipe=recipe_details)
@app.route("/login")
@@ -65,18 +72,23 @@ def add_recipe():
req_data = request.form.to_dict()
submitted_url = req_data["url"]
if submitted_url == '':
- return "<span class='text-red-500'>No recipe supplied!</span>"
+ return "<span class='text-red-500'>No recipe supplied!</span>", 422
does_recipe_exist = recipe_exists(session["username"], submitted_url)
if does_recipe_exist:
- return "<h1>Recipe already exists</h1>"
+ return "<span class='error'>Recipe already exists!</span>", 422
else:
- recipe_json = scrape(submitted_url, session["username"])
- add_single_recipe(recipe_json)
- all_recipes = get_all_recipes(session["username"])
- all_facets = get_facets(session["username"])
- return render_template("/components/app.html", recipes=all_recipes, facets=all_facets)
+ response = scrape(submitted_url, session["username"])
+
+ if response["success"]:
+ add_single_recipe(response["data"])
+ all_recipes = get_all_recipes(session["username"])
+ all_facets = get_facets(session["username"])
+ return render_template("/components/app.html", recipes=all_recipes, facets=all_facets)
+ else:
+ return f"<span class='error'>Error processing recipe metadata, {response['error']} </span>", 422
+
@app.post("/recipes/search")
def search():
@@ -115,12 +127,16 @@ def login_user():
@app.post("/signup")
def signup_user():
req_data = request.form.to_dict()
- user = req_data["username"]
- pw = req_data["password"]
+ user = req_data.get("username")
+ pw = req_data.get("password")
+ pw_confirm = req_data.get("password-confirm")
if user == "" or pw == "":
return "<p class='error'>You must fill in both username and password</p>"
+ if pw != pw_confirm:
+ return "<p class='error'>Passwords do not match</p>"
+
res = user_exists(user)
if res["success"] and res["user"] is not None:
return "<p class='error'>User already exists, <a href='/login'>Login instead</a></p>"
@@ -141,4 +157,17 @@ def signup_user():
@app.post("/logout")
def logout_user():
session.pop('username', None)
- return Response(headers={"HX-Redirect": "/"}) \ No newline at end of file
+ return Response(headers={"HX-Redirect": "/"})
+
+@app.delete("/delete-account")
+def delete_user():
+ delete_user_res = delete_single_user(session["username"])
+ delete_recipes_res = delete_multiple_recipes(session["username"])
+ if delete_user_res["success"] and delete_recipes_res["success"]:
+ session.pop("username", None)
+ return Response(headers={"HX-Redirect": "/"})
+ else:
+ return "Something went wrong deleting the user and recipes"
+
+if __name__ == "__main__":
+ app.run(host="0.0.0.0")