def get_values(self, request, pk): """ Calculates the nutritional values for current ingredient and the given amount and unit. This function basically just performs a multiplication (in the model), and is a candidate to be moved to pure AJAX calls, however doing it like this keeps the logic nicely hidden and respects the DRY principle. """ result = { 'energy': 0, 'protein': 0, 'carbohydrates': 0, 'carbohydrates_sugar': 0, 'fat': 0, 'fat_saturated': 0, 'fibres': 0, 'sodium': 0, 'vitamin_a': 0, 'vitamin_c': 0, 'calcium': 0, 'iron': 0, 'alcohol': 0, 'errors': [] } ingredient = self.get_object() form = UnitChooserForm(request.GET) if form.is_valid(): # Create a temporary MealItem object if form.cleaned_data['unit']: unit_id = form.cleaned_data['unit'].id else: unit_id = None item = MealItem() item.ingredient = ingredient item.weight_unit_id = unit_id item.amount = form.cleaned_data['amount'] result = item.get_nutritional_values() for i in result: result[i] = '{0:f}'.format(result[i]) else: result['errors'] = form.errors return Response(result)
def get_values(self, request, pk): ''' Calculates the nutritional values for current ingredient and the given amount and unit. This function basically just performs a multiplication (in the model), and is a candidate to be moved to pure AJAX calls, however doing it like this keeps the logic nicely hidden and respects the DRY principle. ''' result = { 'energy': 0, 'protein': 0, 'carbohydrates': 0, 'carbohydrates_sugar': 0, 'fat': 0, 'fat_saturated': 0, 'fibres': 0, 'sodium': 0, 'errors': [] } ingredient = self.get_object() form = UnitChooserForm(request.GET) if form.is_valid(): # Create a temporary MealItem object if form.cleaned_data['unit']: unit_id = form.cleaned_data['unit'].id else: unit_id = None item = MealItem() item.ingredient = ingredient item.weight_unit_id = unit_id item.amount = form.cleaned_data['amount'] result = item.get_nutritional_values() for i in result: result[i] = '{0:f}'.format(result[i]) else: result['errors'] = form.errors return Response(result)
def ajax_get_ingredient_values(request, pk): ''' Calculates the nutritional values for the given amount and exercise ''' result = { 'energy': 0, 'protein': 0, 'carbohydrates': 0, 'carbohydrates_sugar': 0, 'fat': 0, 'fat_saturated': 0, 'fibres': 0, 'sodium': 0, 'errors': [] } ingredient = get_object_or_404(Ingredient, pk=pk) if request.method == 'POST': form = UnitChooserForm(request.POST) if form.is_valid(): # Create a temporary MealItem object if form.cleaned_data['unit']: unit_id = form.cleaned_data['unit'].id else: unit_id = None item = MealItem() item.ingredient = ingredient item.weight_unit_id = unit_id item.amount = form.cleaned_data['amount'] result = item.get_nutritional_values() else: result['errors'] = form.errors return HttpResponse(json.dumps(result, cls=helpers.DecimalJsonEncoder), 'application/json')
def ajax_get_ingredient_values(request, pk): ''' Calculates the nutritional values for the given amount and exercise ''' result = {'energy': 0, 'protein': 0, 'carbohydrates': 0, 'carbohydrates_sugar': 0, 'fat': 0, 'fat_saturated': 0, 'fibres': 0, 'sodium': 0, 'errors': []} ingredient = get_object_or_404(Ingredient, pk=pk) if request.method == 'POST': form = UnitChooserForm(request.POST) if form.is_valid(): # Create a temporary MealItem object if form.cleaned_data['unit']: unit_id = form.cleaned_data['unit'].id else: unit_id = None item = MealItem() item.ingredient = ingredient item.weight_unit_id = unit_id item.amount = form.cleaned_data['amount'] result = item.get_nutritional_values() else: result['errors'] = form.errors return HttpResponse(json.dumps(result, cls=helpers.DecimalJsonEncoder), 'application/json')