Esempio n. 1
0
def create_ingredients(request):
    add_ingredient_form = AddIngredientForm(request.POST)

    if add_ingredient_form.is_valid():
        ingredient_name = add_ingredient_form.cleaned_data['name']
        add_ingredient_form.fields.pop('name')
        nutrient = add_ingredient_form.save()
        ingredient = Ingredient(name=ingredient_name, nutrient=nutrient)
        try:
            ingredient.save()

            edit_icon_html = _get_edit_icon_html()
            remove_icon_html = _get_remove_icon_html()

            return JsonResponse(
                {
                    'ingredient': ingredient.to_dict(),
                    'ingredient_actions': edit_icon_html + remove_icon_html
                },
                status=200)
        except IntegrityError:
            return JsonResponse(
                {'errors': {
                    'name': [u'Ingredient already exists.']
                }},
                status=400)
    return JsonResponse({'errors': add_ingredient_form.errors}, status=400)
Esempio n. 2
0
    def post(self):
        form = IngredientForm(request.form)

        if not form.validate_on_submit():
            save_form_to_session(request.form)
            return redirect(url_for("IngredientView:new"))

        ingredient = Ingredient(author=current_user)
        form.populate_obj(ingredient)
        ingredient.save()

        return redirect(
            url_for("IngredientView:show", id=ingredient.id, from_new=True))
Esempio n. 3
0
    def post(self, recipe_id):
        from app.controllers import EditRecipeIngredientView

        recipe = Recipe.load(recipe_id)

        form = IngredientForm(request.form)

        ingredient = Ingredient()
        form.populate_obj(ingredient)
        ingredient.save()

        recipe.add_ingredient(ingredient)

        ingredient.set_additional_info(recipe)

        if turbo.can_stream():
            return turbo.stream([
                turbo.remove(target="add-ingredient-simple"),
                turbo.prepend(
                    self.template(
                        template_name="recipes/edit/ingredient/_row.html.j2",
                        ingredient=ingredient,
                        recipe=recipe,
                        editing=True,
                    ),
                    target="ingredients",
                ),
                turbo.after(
                    self.template(
                        template_name="recipes/edit/ingredient/_edit.html.j2",
                        ingredient=ingredient,
                        recipe=recipe,
                    ),
                    target=f"ingredient-{ingredient.id}",
                ),
            ] + EditRecipeIngredientView().update_usable_ingredients(recipe))
        else:
            return redirect(url_for("RecipeView:edit", id=recipe_id))
Esempio n. 4
0
    def post(self):
        form = IngredientForm(request.form)

        if not form.validate_on_submit():
            save_form_to_session(request.form)
            return redirect(url_for("IngredientView:new"))

        ingredient = Ingredient(author=current_user.username)
        form.populate_obj(ingredient)

        if ingredient.save():
            return redirect(url_for("IngredientView:show", id=ingredient.id))
        else:
            flash("Nepodařilo se vytvořit surovinu", "error")
            return redirect(url_for("IngredientView:new"))
Esempio n. 5
0
    def post_shared(self):
        form = IngredientForm(request.form)

        if not form.validate_on_submit():
            save_form_to_session(request.form)
            return redirect(url_for("IngredientView:new_shared"))

        ingredient = Ingredient(is_shared=True, source=current_user.username)
        form.populate_obj(ingredient)

        if ingredient.save():
            flash(
                "Děkujeme za vytvoření sdílené suroviny. Až ji zkontrolujeme, bude zobrazena všem uživatelům.",
                "success",
            )
            return redirect(url_for("IngredientView:index"))
        else:
            flash("Nepodařilo se vytvořit surovinu", "error")
            return redirect(url_for("IngredientView:new_shared"))