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
|
from bson.objectid import ObjectId
from scripts.database.mongo import mongo_database
from scripts.scraping.scraper import scrape
mongo_collection = mongo_database.get_collection("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 delete_multiple_recipes(user):
try:
mongo_collection.delete_many({"user": user})
return {"success": True}
except:
return {"success": False}
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
|