Exemplo n.º 1
0
def create_test_recipe():
    """
    Initialise a new recipe and save it in the DB
    :return:
    """
    now = timezone.now();
    recipe = Recipe(name='Tarte Tatin', global_time=55, preparation_time=15, difficulty=1, costs=1, num_people=6,
                    created=now, modified=now, user_mod='user_test', description='Tarte aux pommes caramélisées',
                    process='*Peler les pommes et les couper en quartier.\n*Etaler les quartier en cercle dans un moule à tarte....')
    recipe.save()
    return recipe
Exemplo n.º 2
0
def add_modify(request):
    """
    Perform the update/creation of a recipe with informations in POST
    :param request: the http request
    :return:the recipe view filled with the created/updated recipe
    """
    recipe = Recipe()
    if ('add' == request.POST['action'] and request.user.has_perm('cooking.add_recipe')) or (
                    'modify' == request.POST['action'] and request.user.has_perm('cooking.change_recipe')):
        if 'add' == request.POST['action']:
            recipe.created = timezone.now()
        else:
            recipe = get_object_or_404(Recipe, pk=request.POST['id'])
        recipe.name = request.POST['name']
        recipe.costs = int(request.POST['cost'])
        recipe.difficulty = int(request.POST['difficulty'])
        recipe.num_people = int(request.POST['num_people'])
        recipe.description = request.POST['description']
        if request.POST['global_time']:
            recipe.global_time = int(request.POST['global_time'])
        recipe.modified = timezone.now()
        if request.POST['preparation_time']:
            recipe.preparation_time = int(request.POST['preparation_time'])
        if request.POST['waiting_time']:
            recipe.waiting_time = int(request.POST['waiting_time'])
        recipe.process = request.POST['process']
        recipe.user_mod = request.user.username
        recipe.save()
        ingredients_recipes = IngredientRecipe.objects.filter(recipe=recipe)
        context = {
            'recipe': recipe,
            'ingredients_recipes': ingredients_recipes,
        }
        return render(request, 'recipe.html', context)
    else:
        context = {
            'latest_recipes': Recipe.objects.order_by('-created')[:10],
            'error_messages': (_('Insufisant privileges'),),
        }
        return render(request, 'recipes.html', context)