Beispiel #1
0
def recipe_edit(request, id):
    purchases_counter = ShoppingList.objects.filter(user=request.user).count()
    recipe_base = get_object_or_404(Recipe, pk=id)
    form = RecipeForm(request.POST or None,
                      files=request.FILES or None,
                      instance=recipe_base)
    ingredients = get_ingredients(request)
    if not form.is_valid():
        return render(
            request,
            'EditRecipe.html',
            {
                'form': form,
                'is_new': True,
                'recipe': recipe_base,
                'page_title': 'Редактирование рецепта',
                'purchases_counter': purchases_counter
            },
        )
    recipe = form.save(commit=False)
    recipe.user = request.user
    recipe.save()
    RecipeIngredient.objects.filter(recipe=recipe).delete()
    objs = []
    for title, count in ingredients.items():
        ingredient = get_object_or_404(Ingredient, title=title)
        objs.append(
            RecipeIngredient(recipe=recipe, ingredient=ingredient,
                             count=count))
    RecipeIngredient.objects.bulk_create(objs)
    form.save_m2m()
    return redirect('index')
Beispiel #2
0
def add_or_update_recipe(request):

    current_recipe = None

    if request.method == 'POST' and not 'recipe_id' in request.POST:

        recipe_form = RecipeForm(request.POST)

        if recipe_form.is_valid():
            # getting recipe object from form
            recipe_obj = recipe_form.save(commit=False)
            recipe_obj.user = request.user
            recipe_obj.save()

            # save many to many fields
            recipe_form.save_m2m()

            return redirect('recipes:recipe-manage',
                            recipe_slug=recipe_obj.slug)
    else:

        recipe_id = request.POST.get('recipe_id', '')

        # check if user wants to update or create
        if recipe_id == "":
            recipe_form = RecipeForm()
        else:
            current_recipe = Recipe.objects.get(id=recipe_id)
            recipe_form = RecipeForm(instance=current_recipe)

    return render(request, 'recipes/user/add_recipe.html', {
        'recipe_form': recipe_form,
        'recipe': current_recipe
    })
Beispiel #3
0
def new_recipe(request):
    form = RecipeForm(request.POST or None, files=request.FILES or None)
    ingredients = get_ingredients(request)
    if not form.is_valid():
        return render(
            request,
            'formRecipe.html',
            {
                'form': form,
                'is_new': True,
                'page_title': 'Создание рецепта'
            },
        )
    recipe = form.save(commit=False)
    recipe.user = request.user
    recipe.save()
    RecipeIngredient.objects.filter(recipe=recipe).delete()
    objs = []
    for title, count in ingredients.items():
        ingredient = get_object_or_404(Ingredient, title=title)
        objs.append(
            RecipeIngredient(recipe=recipe, ingredient=ingredient,
                             count=count))
    RecipeIngredient.objects.bulk_create(objs)
    form.save_m2m()
    return redirect('index')
Beispiel #4
0
def new_recipe(request):
    form = RecipeForm(request.POST or None, files=request.FILES or None)
    ingredients = get_form_ingredients(request)
    if form.is_valid() and ingredients:
        recipe = form.save(commit=False)
        recipe.author = request.user
        form.save()
        passed_tags = request.POST.getlist('tags')
        for tag_id in passed_tags:
            tag = get_object_or_404(Tag, id=tag_id)
            recipe.tags.add(tag)

        for ing_id, values in ingredients.items():
            title, amount, units = values
            ingredient = Ingredient.objects.get_or_create(
                title=title, dimension=units)[0]
            new_ingredient = IngredientAmount.objects.get_or_create(
                ingredient=ingredient,
                amount=amount)[0]
            recipe.ingredients.add(new_ingredient)
        form.save_m2m()
        return redirect('index')
    if request.method == 'POST' and not ingredients:
        form.add_error(None, 'Обязательное поле.')
    return render(
        request,
        template_name='formRecipe.html',
        context={
            'form': form,
        }
    )
Beispiel #5
0
def recipe_create(request):
    form = RecipeForm(request.POST or None, files=request.FILES or None)
    if not form.is_valid():
        context = {
            'form': form,
            'is_new': True,
        }
        return render(request, 'recipes/recipe_create.html', context)
    recipe = form.save(commit=False)
    recipe.author = request.user
    RecipeIngredient.objects.filter(recipe=recipe).delete()
    objs = []
    ingredients = get_ingredients(request)
    if ingredients:
        for title, count in ingredients.items():
            ingredient = get_object_or_404(Ingredient, title=title)
            objs.append(
                RecipeIngredient(recipe=recipe,
                                 ingredient=ingredient,
                                 count=count))
    else:
        context = {
            'form': form,
            'error': 'error',
        }
        return render(request, 'recipes/recipe_create.html', context)
    recipe.save()
    RecipeIngredient.objects.bulk_create(objs)
    form.save_m2m()
    return redirect('index')
Beispiel #6
0
def recipe_update(request, recipe_id):
    try:
        recipe = Recipe.objects.get(id=recipe_id)
    except Recipe.DoesNotExist:
        raise Http404("Recipe does not exist")
    form = RecipeForm(request.POST, instance=recipe)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.save()
        form.save_m2m()
    return redirect('recipes:recipe-manage', recipe_slug=recipe.slug)
Beispiel #7
0
def recipe_edit(request, slug):
    recipe = get_object_or_404(Recipe, slug=slug)
    if request.user.username != recipe.author.username:
        return redirect('recipe_detail', slug=slug)
    form = RecipeForm(
        request.POST or None,
        files=request.FILES or None,
        instance=recipe,
    )
    if not form.is_valid():
        context = {
            'form': form,
            'recipe': recipe,
            'recipe_edit': 'recipe_edit'
        }
        return render(
            request,
            'recipes/recipe_create.html',
            context,
        )
    recipe = form.save(commit=False)
    RecipeIngredient.objects.filter(recipe=recipe).delete()
    objs = []
    ingredients = get_ingredients(request)
    if ingredients:
        for title, count in ingredients.items():
            ingredient = get_object_or_404(Ingredient, title=title)
            objs.append(
                RecipeIngredient(recipe=recipe,
                                 ingredient=ingredient,
                                 count=count))
    else:
        context = {
            'form': form,
            'error': 'error',
        }
        return render(request, 'recipes/recipe_create.html', context)
    recipe.save()
    RecipeIngredient.objects.bulk_create(objs)
    form.save_m2m()
    return redirect('recipe_detail', slug=recipe.slug)
Beispiel #8
0
def recipe_edit(request, recipe_id):
    recipe = get_object_or_404(Recipe, id=recipe_id)
    if request.user != recipe.author:
        return redirect('recipe_view', recipe_id=recipe_id)
    form = RecipeForm(request.POST or None,
                      files=request.FILES or None,
                      instance=recipe)
    ingredients = get_dict_ingredients(request)

    if form.is_valid():
        IngredientAmount.objects.filter(recipe=recipe).delete()
        Tag.objects.filter(recipe=recipe).delete()
        recipe = form.save(commit=False)
        recipe.author = request.user
        recipe.save()

        tags = form.cleaned_data['tag']
        for tag in tags:
            recipe_tag = Tag(recipe=recipe, title=tag)
            recipe_tag.save()

        for key, value in ingredients.items():
            ingredient = get_object_or_404(Ingredient, title=key)
            recipe_ing = IngredientAmount(recipe=recipe,
                                          ingredient=ingredient,
                                          amount=value)
            recipe_ing.save()
        form.save_m2m()
        return redirect('recipe_view', recipe_id=recipe_id)
    tags = list(recipe.tags.values_list('title', flat=True))
    return render(
        request,
        'formChangeRecipe.html',
        {
            'form': form,
            'recipe': recipe,
            'tags': tags
        },
    )
Beispiel #9
0
def create_recipe(request):
    form = RecipeForm(request.POST or None, files=request.FILES or None)
    user = get_object_or_404(User, username=request.user)
    ingredients = get_dict_ingredients(request)
    if form.is_valid():
        recipe = form.save(commit=False)
        recipe.author = user
        recipe.save()

        tags = form.cleaned_data['tag']
        for tag in tags:
            recipe_tag = Tag(recipe=recipe, title=tag)
            recipe_tag.save()

        for key, value in ingredients.items():
            ingredient = get_object_or_404(Ingredient, title=key)
            recipe_ing = IngredientAmount(recipe=recipe,
                                          ingredient=ingredient,
                                          amount=value)
            recipe_ing.save()
        form.save_m2m()
        return redirect('recipes')
    return render(request, 'formRecipe.html', {'form': form})
Beispiel #10
0
def recipe_create(request):
    purchases_counter = ShoppingList.objects.filter(user=request.user).count()
    form = RecipeForm(request.POST or None, files=request.FILES or None)
    if not form.is_valid():
        context = {
            'form': form,
            'is_new': True,
            'purchases_counter': purchases_counter,
            'page_title': 'Создание'
        }
        return render(request, 'formRecipe.html', context)
    recipe = form.save(commit=False)
    recipe.user = request.user
    recipe.save()
    RecipeIngredient.objects.filter(recipe=recipe).delete()
    objs = []
    ingredients = get_ingredients(request)
    if ingredients:
        for title, count in ingredients.items():
            ingredient = get_object_or_404(Ingredient, title=title)
            objs.append(
                RecipeIngredient(recipe=recipe,
                                 ingredient=ingredient,
                                 count=count))
    else:
        last_recipe = Recipe.objects.latest('pub_date')
        last_recipe.delete()
        context = {
            'form': form,
            'error': 'error',
            'purchases_counter': purchases_counter,
            'page_title': 'Создание'
        }
        return render(request, 'formRecipe.html', context)
    RecipeIngredient.objects.bulk_create(objs)
    form.save_m2m()
    return redirect('index')