def get_initial(self): initial = super(LogUpdateView, self).get_initial() ml = MealLog.objects.get(id=self.meallog_id) dl = ml.daily_log self.dailylog_id = dl.id initial['date'] = dl.date initial['time'] = ml.log_time mi_list = MealItem.objects.filter(meal_log=ml) food_count = 1 recipe_count = 1 for item in mi_list: if item.food: initial['food'+str(food_count)] = item.food initial['food_portions'+str(food_count)] = chop_zeros(item.portions) food_count += 1 else: initial['recipe'+str(recipe_count)] = item.recipe initial['recipe_portions'+str(recipe_count)] = chop_zeros(item.portions) recipe_count += 1 return initial
def get_metric_profile(self): return { 'user': self.user, 'gender': self.gender, 'birthdate': self.birthdate, 'height': chop_zeros(self.height), 'weight': chop_zeros(self.weight), 'caloriegoal': self.caloriegoal, 'showmetric': self.showmetric }
def get_total(self): total = { 'servingSize': 0, 'calories': 0, 'totalFat': 0, 'cholesterol': 0, 'sodium': 0, 'totalCarb': 0, 'protein': 0 } # gets the nutrients for single serving of the food/recipe nutrients = {} if self.food: nutrients = self.food.get_nutrients() else: nutrients = self.recipe.get_total() # for each nutrient for key in nutrients: # adds the food's nutrients times the portion size total[key] += nutrients[key] * self.portions # chops all unnecessary zeros for key in total: total[key] = chop_zeros(total[key]) return total
def get_total(self): total = { 'servingSize': 0, 'calories': 0, 'totalFat': 0, 'cholesterol': 0, 'sodium': 0, 'totalCarb': 0, 'protein': 0 } # gets list of MealItems in current MealLog meal_item_list = MealItem.objects.filter(meal_log=self) # for each MealItem for m_item in meal_item_list: # gets total nutrients for each MealItem nutrients = m_item.get_total() # total the nutrients for key in nutrients: total[key] += nutrients[key] # chops all unnecessary zeros for key in total: total[key] = chop_zeros(total[key]) return total
def get_total(self): total = { 'servingSize': 0, 'calories': 0, 'totalFat': 0, 'cholesterol': 0, 'sodium': 0, 'totalCarb': 0, 'protein': 0 } # gets list of meal logs for this daily log meal_log_list = MealLog.objects.filter(daily_log=self) # for each meal log for m_log in meal_log_list: # gets total nutrients of current meal nutrients = m_log.get_total() # for each nutrient for key in nutrients: # adds the food's nutrients times the portion size total[key] += nutrients[key] # chops all unnecessary zeros for key in total: total[key] = chop_zeros(total[key]) return total
def get_total(self): total = { 'servingSize': 0, 'calories': 0, 'totalFat': 0, 'cholesterol': 0, 'sodium': 0, 'totalCarb': 0, 'protein': 0 } # gets list of recipe foods in current recipe recipe_food_list = RecipeFood.objects.filter(recipe=self) # for each meal food for r_food in recipe_food_list: # gets total nutrients for each food nutrients = r_food.get_total() # total the nutrients for key in nutrients: total[key] += nutrients[key] / self.servingsProduced # chops all unnecessary zeros for key in total: total[key] = chop_zeros(total[key]) return total
def get_context_data(self, **kwargs): # get context context = super(FactsView, self).get_context_data(**kwargs) # if there is a GET request if self.request.method == 'GET': # get the query value query = self.request.GET.get('portions') # if query empty if query == None: # set to 1 query = Decimal(1) else: # convert query to python decimal query = Decimal(query) # no GET request else: # set query to 1 query = Decimal(1) # pass query as 'portions' context['portions'] = query # multiply nutrition data fields by query and chop trailing zeros context['food'].servingSize = chop_zeros(query * context['food'].servingSize) context['food'].calories = chop_zeros(query * context['food'].calories) context['food'].totalFat = chop_zeros(query * context['food'].totalFat) context['food'].cholesterol = chop_zeros(query * context['food'].cholesterol) context['food'].sodium = chop_zeros(query * context['food'].sodium) context['food'].totalCarb = chop_zeros(query * context['food'].totalCarb) context['food'].protein = chop_zeros(query * context['food'].protein) if self.request.user.is_authenticated: user = self.request.user profile = Profile.objects.get(user=user) else: user = None profile = None object_list = Recipe.objects.filter(Q(user=user) | Q(is_public=True)) # stuff for the recipe list recipe_food_list = RecipeFood.objects.filter(food=self.kwargs['pk']) recipe_ids = recipe_food_list.distinct().values_list('recipe') object_list = object_list.filter(pk__in=recipe_ids) portions_list = RecipeFood.objects.none() for recipe in object_list: portions_list = portions_list | RecipeFood.objects.filter( food=self.kwargs['pk'], recipe=recipe) context['portions_list'] = portions_list context['object_list'] = object_list return context
def get_context_data(self, **kwargs): context = super(LogDetailView, self).get_context_data(**kwargs) # make sure user has permission by matching the user to the DailyLog try: dl = DailyLog.objects.get(user=self.request.user, id=self.kwargs['pk']) except DailyLog.DoesNotExist: raise PermissionDenied # get list of MealLogs for this DailyLog ml_list = MealLog.objects.filter(daily_log=dl).order_by('log_time') info_list = [] # build a dictionary for each meal log for ml in ml_list: info = {} info['id'] = ml.id info['time'] = ml.log_time.strftime('%I:%M %p') info['item_list'] = [] info['total'] = ml.get_total() # get list of meal foods for each meal log mi_list = MealItem.objects.filter(meal_log=ml) # build a dictionary for each food in each meal log for mi in mi_list: item = {} if mi.food: item['type'] = 'food' item['id'] = mi.food.id item['name'] = mi.food.name else: item['type'] = 'recipe' item['id'] = mi.recipe.id item['name'] = mi.recipe.name item['portions'] = chop_zeros(mi.portions) item = {**item, **mi.get_total()} info['item_list'].append(item) info_list.append(info) # add to context context['daytotal'] = dl.get_total() context['info_list'] = info_list return context
def get_initial(self): initial = super(CopyRecipe, self).get_initial() ml = Recipe.objects.get(id=self.kwargs['pk']) self.recipe_id = ml.id initial['name'] = ml.name initial['servingsProduced'] = ml.servingsProduced initial['allergies'] = ml.allergies.all() initial['diets'] = ml.diets.all() initial['instruction'] = ml.instruction initial['is_public'] = ml.is_public mf_list = RecipeFood.objects.filter(recipe=ml) for i in range(mf_list.count()): initial['food' + str(i + 1)] = mf_list[i].food initial['portions' + str(i + 1)] = chop_zeros(mf_list[i].portions) return initial
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(DetailRecipe, self).get_context_data(**kwargs) context['ingredients'] = RecipeFood.objects.filter( recipe=self.kwargs['pk']) instance = Recipe.objects.get(id=self.kwargs['pk']) nutrition = instance.get_total() context['nutrition'] = nutrition # if there is a GET request if self.request.method == 'GET': # get the query value query = self.request.GET.get('portions') # if query empty if query == None: # set to 1 query = Decimal(1) else: # convert query to python decimal query = Decimal(query) # no GET request else: # set query to 1 query = Decimal(1) # pass query as 'portions' context['portions'] = query # multiply nutrition data fields by query and chop trailing zeros context['nutrition']['calories'] = chop_zeros( query * context['nutrition']['calories']) context['nutrition']['totalFat'] = chop_zeros( query * context['nutrition']['totalFat']) context['nutrition']['cholesterol'] = chop_zeros( query * context['nutrition']['cholesterol']) context['nutrition']['sodium'] = chop_zeros( query * context['nutrition']['sodium']) context['nutrition']['totalCarb'] = chop_zeros( query * context['nutrition']['totalCarb']) context['nutrition']['protein'] = chop_zeros( query * context['nutrition']['protein']) return context