Example #1
0
File: log.py Project: uditp26/wger
def log_meal(request, meal_pk):
    """
    Copy the requested meal item and logs its nutritional values
    """

    # Check read permission
    meal = get_object_or_404(Meal, pk=meal_pk)
    user = meal.plan.user
    is_owner = request.user == user
    date = datetime.date.today()

    if not is_owner and not user.userprofile.ro_access:
        return HttpResponseForbidden()

    for item in meal.mealitem_set.select_related():
        log_item = LogItem(plan=item.meal.plan,
                           ingredient=item.ingredient,
                           weight_unit=item.weight_unit,
                           amount=item.amount)
        log_item.save()

    return HttpResponseRedirect(reverse('nutrition:log:detail',
                                        kwargs={'pk': meal.plan.pk,
                                                'year': date.year,
                                                'month': date.month,
                                                'day': date.day
                                                }))
Example #2
0
def _logMealPlan(meals):
    """
    Helper method to log a collection of meals
    """

    for meal in meals:
        for item in meal.mealitem_set.select_related():
            log_item = LogItem(plan=item.meal.plan,
                               ingredient=item.ingredient,
                               weight_unit=item.weight_unit,
                               amount=item.amount)
            log_item.save()
Example #3
0
    print("** Generating {0} nutrition plan(s) per user".format(
        args.number_nutrition_logs))

    if args.add_to_user:
        userlist = [User.objects.get(pk=args.add_to_user)]
    else:
        userlist = [i for i in User.objects.all()]

    # Load all ingredients to a list
    ingredient_list = [i for i in Ingredient.objects.order_by('?').all()[:100]]

    for user in userlist:
        plan_list = NutritionPlan.objects.order_by('?').filter(user=user)
        print('   - generating for {0}'.format(user.username))

        # Add diary entries
        for plan in NutritionPlan.objects.filter(user=user):
            for i in range(0, args.number_diary_dates):
                date = timezone.now() - datetime.timedelta(
                    days=random.randint(0, 100),
                    hours=random.randint(0, 12),
                    minutes=random.randint(0, 59))
                for j in range(0, args.number_nutrition_logs):
                    ingredient = random.choice(ingredient_list)
                    log = LogItem(plan=plan,
                                  datetime=date,
                                  ingredient=ingredient,
                                  weight_unit=None,
                                  amount=random.randint(10, 300))
                    log.save()