aboutsummaryrefslogtreecommitdiff
path: root/scripts/recipes/handle_recipes.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/recipes/handle_recipes.py')
-rw-r--r--scripts/recipes/handle_recipes.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/scripts/recipes/handle_recipes.py b/scripts/recipes/handle_recipes.py
new file mode 100644
index 0000000..76faf86
--- /dev/null
+++ b/scripts/recipes/handle_recipes.py
@@ -0,0 +1,79 @@
+from bson.objectid import ObjectId
+from scripts.database.mongo import mongo_database
+from scripts.scraping.scraper import scrape
+
+mongo_collection = mongo_database["recipes"]
+
+def add_single_recipe(recipe):
+ added_recipe = mongo_collection.insert_one(recipe)
+ new_recipe = mongo_collection.find_one()
+ return added_recipe
+
+def recipe_exists(user, url):
+ recipe_exists = mongo_collection.find_one({"url": url, "user": user})
+ if recipe_exists is not None:
+ return True
+ else:
+ return False
+
+def get_all_recipes(user):
+ all_recipes = mongo_collection.find({"user": user}).to_list()
+ return all_recipes
+
+def get_single_recipe(objectId):
+ single_recipe = mongo_collection.find_one({"_id": ObjectId(objectId) })
+ return single_recipe
+
+def delete_single_recipe(objectId):
+ try:
+ mongo_collection.delete_one({"_id": ObjectId(objectId)})
+ return {"success": True, "id": objectId}
+ except:
+ return {"success": False, "error": "Could not delete recipe"}
+
+def search_recipes(query_data, user):
+ query = query_data["q"]
+ facets_list = []
+
+ for item in query_data:
+ if "choice" in item:
+ facets_list.append(query_data[item])
+
+ if len(facets_list) > 0:
+ mongo_query = {"tags":{ "$all": facets_list}, "user": user, "title": { "$regex": query, "$options": "xi" }}
+ else:
+ mongo_query = {"title": { "$regex": query, "$options": "xi" }, "user": user}
+
+ all_recipes = mongo_collection.find(mongo_query).to_list()
+ return all_recipes
+
+def get_facets(user, query_data=None):
+ if query_data is not None:
+ facets_list = []
+ new_list = []
+ query = query_data["q"]
+ for item in query_data:
+ if "choice" in item:
+ facets_list.append(query_data[item])
+
+ if len(facets_list) > 0:
+ mongo_query = {"tags":{ "$all": facets_list}, "user": user, "title": { "$regex": query, "$options": "xi" }}
+ else:
+ mongo_query = {"title": { "$regex": query, "$options": "xi" }, "user": user}
+
+ facets = mongo_collection.distinct("tags", mongo_query)
+
+ for facet in facets:
+ if facet in facets_list:
+ new_list.append({"name": facet, "checked": True})
+ else:
+ new_list.append({"name": facet, "checked": False})
+
+ return new_list
+
+ else:
+ new_list = []
+ facets = mongo_collection.distinct("tags", {"user": user})
+ for facet in facets:
+ new_list.append({"name": facet, "checked": False})
+ return new_list