Ejemplo n.º 1
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))
Ejemplo n.º 2
0
def addToIngredients():

    form = IngredientForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        ingredient = Ingredient(
            name=form.data['name'],
            type=form.data['type'],
            editable=True,
        )
        db.session.add(ingredient)
        db.session.commit()
        # print(ingredient)
        return ingredient.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}, 401
Ejemplo n.º 3
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"))
Ejemplo n.º 4
0
    def update(self, id):
        form = IngredientForm(request.form)

        if not self.ingredient.can_edit_measurement:
            del form.measurement

        if not form.validate_on_submit():
            save_form_to_session(request.form)
            return redirect(
                url_for("IngredientView:edit", id=self.ingredient.id))

        form.populate_obj(self.ingredient)
        self.ingredient.edit()

        return redirect(url_for("IngredientView:show", id=self.ingredient.id))
Ejemplo 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"))
Ejemplo n.º 6
0
def ingredients():
    form = IngredientForm()
    if form.validate_on_submit():
        ingredient = Ingredient(name=form.name.data)
        db.session.add(ingredient)
        db.session.commit()
        flash(_('Your ingredient added!'))
        return redirect(url_for('ingredients'))
    page = request.args.get('page', 1, type=int)
    ingredients = Ingredient.query.order_by(Ingredient.name).paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('ingredients', page=ingredients.next_num) \
        if ingredients.has_next else None
    prev_url = url_for('ingredients', page=ingredients.prev_num) \
        if ingredients.has_prev else None
    return render_template('ingredients.html',
                           title=_('Ingredients'),
                           form=form,
                           ingredients=ingredients.items,
                           next_url=next_url,
                           prev_url=prev_url)
Ejemplo n.º 7
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))
Ejemplo n.º 8
0
    def update(self, id):
        form = IngredientForm(request.form)

        if self.ingredient.is_used:
            del form.calorie
            del form.protein
            del form.fat
            del form.sugar

        if not form.validate_on_submit():
            save_form_to_session(request.form)
            return redirect(url_for("IngredientView:edit", id=self.ingredient.id))

        form.populate_obj(self.ingredient)

        if not self.ingredient.is_shared or current_user.is_admin:
            self.ingredient.edit()
            flash("Surovina byla upravena.", "success")
        else:
            self.ingredient.refresh()
            flash("Sdílenou surovinu nelze upravit", "error")

        return redirect(url_for("IngredientView:show", id=self.ingredient.id))
Ejemplo n.º 9
0
    def edit(self, id, **kwargs):
        self.editing_id = int(request.args.get("editing_id", 0))
        self.show_fast_add = request.args.get("show_fast_add", False)
        self.ingredient_form = IngredientForm()

        self.form = create_form(RecipeForm, obj=self.recipe)

        self.personal_ingredients = sorted(
            self.recipe.unused_personal_ingredients,
            key=lambda x: unidecode(x.name.lower()),
        )

        self.public_ingredients = sorted(
            self.recipe.unused_public_ingredients,
            key=lambda x: unidecode(x.name.lower()),
        )

        return self.template()