Example #1
0
    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)
Example #2
0
    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)
Example #3
0
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')
Example #4
0
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')
Example #5
0
def view(request, id, slug=None):
    template_data = {}

    ingredient = cache.get(cache_mapper.get_ingredient_key(int(id)))
    if not ingredient:
        ingredient = get_object_or_404(Ingredient, pk=id)
        cache.set(cache_mapper.get_ingredient_key(ingredient), ingredient)
    template_data['ingredient'] = ingredient
    template_data['form'] = UnitChooserForm(data={'ingredient_id': ingredient.id,
                                                  'amount': 100,
                                                  'unit': None})
    template_data['show_shariff'] = True

    return render(request, 'ingredient/view.html', template_data)
Example #6
0
def view(request, id, slug=None):
    template_data = {}

    ingredient = get_object_or_404(Ingredient, pk=id)
    template_data['ingredient'] = ingredient
    template_data['form'] = UnitChooserForm(data={
        'ingredient_id': ingredient.id,
        'amount': 100,
        'unit': None
    })

    return render_to_response('ingredient/view.html',
                              template_data,
                              context_instance=RequestContext(request))