From 2e0b9c97af457da5c6afda611d48e59047d4cdb8 Mon Sep 17 00:00:00 2001 From: JJ Date: Mon, 31 Mar 2025 21:08:41 +0100 Subject: Basic AI functionality --- server.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'server.py') 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 "Recipe already exists!", 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"Error processing recipe metadata, {response['error']} ", 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"{response["message"]}", 422 + if __name__ == "__main__": app.run(host="0.0.0.0") -- cgit v1.2.3