forked from Tieshia/hb-project1
/
mealplan_db.py
266 lines (187 loc) · 8.89 KB
/
mealplan_db.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
""" Meal-of-Fortune meal planning application.
Copyright (C) 2018 Tieshia Francis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For further information, please contact: francistie@gmail.com
License available at LICENSE.txt.
"""
"""Functions accessing database for Meal Planning project."""
from datetime import datetime
from model import (connect_to_db, db, User, FoodType, Recipe, Ingredient,
RecipeIngredient, UserRecipe, Score)
from random import choice
from nltk.stem import PorterStemmer
# Create class for stemming words
ps = PorterStemmer()
def get_user(email): # -- TESTED
"""Provides user based off login email."""
return User.query.filter_by(email=email).first()
def create_new_user(name, email, password): # -- TESTED
"""Create new user in db."""
new_user = User(name=name, email=email, password=password)
db.session.add(new_user)
db.session.commit()
return new_user
def get_active_user_recipes(user_id): # -- TESTED
"""Get list of active user recipes based on user_id."""
return UserRecipe.query.filter((UserRecipe.active == True) &
(UserRecipe.user_id == user_id)).all()
def create_user_recipe(recipe_id, user_id): # -- TESTED
"""Create/update user recipe in db."""
user_recipe = UserRecipe.query.filter((UserRecipe.recipe_id == recipe_id) &
(UserRecipe.user_id == user_id)).first()
print "User recipe:", user_recipe
if user_recipe:
user_recipe.active = True
print "Changed recipe to active"
else:
new_user_recipe = UserRecipe(recipe_id=recipe_id, times_cooked=0,
active=True, user_id=user_id)
db.session.add(new_user_recipe)
print "Creted new recipe"
db.session.commit()
print "Saved changes"
def get_ingredient(ingredient_name): # -- TESTED
"""Returns ingredient based on ingredient_name."""
return Ingredient.query.filter_by(ingredient_name=ingredient_name).first()
def get_ingredient_type(ingredient_type): # -- TESTED
"""Returns food type based on ingredient type."""
return FoodType.query.filter_by(food_type=ingredient_type).first()
def create_ingredient(ingredient_name, type_id): # -- TESTED
"""Create new ingredient in db."""
new_ingredient = Ingredient(
ingredient_name=ingredient_name, type_id=type_id)
db.session.add(new_ingredient)
db.session.commit()
# -- ** Technically not a db fn?
def standardize_ingredient_name(ingredient_name):
"""Use nltk.stem to standardize user input."""
tokenized_ingredients = ingredient_name.split()
if len(tokenized_ingredients) > 1:
for i in range(len(tokenized_ingredients)):
tokenized_ingredients[i] = ps.stem(tokenized_ingredients[i])
ingredient_name = ' '.join(tokenized_ingredients)
else:
ingredient_name = ps.stem(ingredient_name)
return ingredient_name
def add_ingredient(ingredient_name, ingredient_type): # -- TESTED
"""Adds ingredient if not already in db."""
ingredient = get_ingredient(ingredient_name)
if ingredient is None:
# Get food type
ing_type = get_ingredient_type(ingredient_type)
# Add new ingredient to db
create_ingredient(ingredient_name, ing_type.type_id)
# Get ingredient and return
return get_ingredient(ingredient_name)
def get_recipe_by_id(recipe_id): # -- TESTED
"""Returns recipe by recipe_id."""
return Recipe.query.filter_by(recipe_id=recipe_id).first()
def get_recipe_by_url(url): # -- TESTED
"""Returns recipe by url."""
return Recipe.query.filter_by(url=url).first()
def create_recipe(recipe_name, url, image_url): # -- TESTED
"""Create new recipe in db."""
recipe = get_recipe_by_url(url)
if recipe is None:
response_recipe = Recipe(recipe_name=recipe_name,
url=url,
image_url=image_url)
db.session.add(response_recipe)
db.session.commit()
def get_recipe_ingredient(recipe_id): # -- TESTED
"""Returns recipe_ingredient based on recipe_id."""
return RecipeIngredient.query.filter_by(recipe_id=recipe_id).first()
def create_recipe_ingredient(recipe_id, ingredient_id): # -- TESTED
"""Create new recipe_ingredient in db."""
new_recipe_ingredient = RecipeIngredient(recipe_id=recipe_id,
ingredient_id=ingredient_id)
db.session.add(new_recipe_ingredient)
db.session.commit()
def mark_meal_made(recipe_id, user_id): # -- TESTED
"""Update status of user_recipe."""
user_recipe = UserRecipe.query.filter((UserRecipe.recipe_id == recipe_id) &
(UserRecipe.user_id == user_id)).first()
user_recipe.active = False
user_recipe.times_cooked = user_recipe.times_cooked + 1
db.session.commit()
def get_score(recipe_id, user_id): # -- TESTED
"""Get score based on recipe and user id."""
return Score.query.filter((Score.recipe_id == recipe_id) &
(Score.user_id == user_id)).first()
def upsert_score(recipe_id, user_id, score): # -- TESTED
"""Add/update score based on recipe and user_id."""
user_score = get_score(recipe_id, user_id)
if user_score:
# update in database
user_score.score = score
user_score.rated_at = datetime.now()
# Else
else:
# Add new score row to database
new_score = Score(rated_at=datetime.now(), score=int(score),
user_id=user_id, recipe_id=recipe_id)
db.session.add(new_score)
db.session.commit()
def get_weighted_highest_average_rated_recipes():
"""Return list of highest rated recipes."""
scores = Score.query.all()
average_ratings = {}
recipes = []
for score in scores:
recipe = get_recipe_by_id(score.recipe_id)
if average_ratings.get(recipe):
average_ratings[recipe]['ratings_sum'] += score.score
average_ratings[recipe]['count'] += 1
else:
average_ratings[recipe] = {'ratings_sum': score.score,
'count': 1}
for key in average_ratings:
average_ratings[key]['average'] = (
(average_ratings[key]['ratings_sum']/average_ratings[key]['count']) + 1)
recipes.append((average_ratings[key]['average'], key))
highest_rated = sorted(recipes)[:10]
weighted_highest_ratings = []
for avg_score, recipe in highest_rated:
for i in range(average_ratings[recipe]['count']):
weighted_highest_ratings.append((avg_score, recipe))
return weighted_highest_ratings
def get_random_highest_rated_recipes():
"""Return selection of 3 highest rated recipes."""
recipes = get_weighted_highest_average_rated_recipes()
random_highest = set()
while len(random_highest) < 3:
random_highest.add(choice(recipes))
return random_highest
def get_all_recipes():
"""Return a list of all recipes."""
return Recipe.query.all()
def get_user_scores(user_id):
"""Return a list of all recipes for user."""
return Score.query.filter_by(user_id=user_id).all()
def clear_recipes(user_id):
"""Mark all active recipes for user to inactive."""
active_recipes = get_active_user_recipes(user_id)
for recipe in active_recipes:
print "Recipe:", recipe
print "Status:", recipe.active
user_recipe = UserRecipe.query.filter((UserRecipe.recipe_id == recipe.recipe_id == True) &
(UserRecipe.user_id == user_id)).first()
print "User recipe:", user_recipe
user_recipe.active = False
print "Updated status:", recipe.active
db.session.commit()
def delete_user_recipe(user_id, recipe_id):
"""Delete user_recipe for specified user and recipe."""
user_recipe = UserRecipe.query.filter((UserRecipe.recipe_id == recipe_id) &
(UserRecipe.user_id == user_id)).first()
db.session.delete(user_recipe)
db.session.commit()