aboutsummaryrefslogtreecommitdiff
path: root/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'server.py')
-rw-r--r--server.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/server.py b/server.py
index 7840ea3..66c2d43 100644
--- a/server.py
+++ b/server.py
@@ -3,9 +3,11 @@ from flask_bcrypt import Bcrypt
import functools
import re
import Constants
+from urllib.parse import urlparse
from scripts.scraping.scraper import scrape
+from scripts.scraping.ai_scraping import run_ai_query
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
+from scripts.users.handle_users import user_exists, add_user, delete_single_user, update_user
app = Flask(__name__)
bcrypt = Bcrypt(app)
@@ -25,7 +27,6 @@ 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)
@@ -79,15 +80,22 @@ def add_recipe():
if does_recipe_exist:
return "<span class='error'>Recipe already exists!</span>", 422
else:
- response = scrape(submitted_url, session["username"])
+ response = scrape(submitted_url)
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)
+
+ elif not response["success"]:
+ ai_res = run_ai_query(submitted_url)
+ add_single_recipe(ai_res["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
+ return "something went wrong", 400
@app.post("/recipes/search")
@@ -168,6 +176,18 @@ def delete_user():
return Response(headers={"HX-Redirect": "/"})
else:
return "Something went wrong deleting the user and recipes"
+
+@app.post("/update-account")
+def update_account():
+ user_prefs = request.form.to_dict()
+ response = update_user(session["username"], user_prefs)
+
+ if response["success"]:
+ session["isAiSubscriber"] = True
+ return Response(headers={"HX-Redirect": "/account"})
+ else:
+ return f"<span class='error'>{response["message"]}</span>", 422
+
if __name__ == "__main__":
app.run(host="0.0.0.0")