Exemplo n.º 1
0
class IngredientCategorieView(HelperFlaskView, AdminViewMixin):
    decorators = [login_required, permissions_required("manage-application")]
    template_folder = "ingredient_categories"
    attribute_name = "ingredient-category"
    plural_attribute_name = "ingredient-categories"
    instance_name = "category"

    @login_required
    def before_request(self, name, id=None, *args, **kwargs):
        self.category = IngredientCategory.load(id)

    def before_index(self):
        self.ingredient_categories = IngredientCategory.load_all()

    def index(self, edit_id=None):
        self.edit_id = request.args.get("edit_id", None)

        return self.template()

    @route("show_edit/<id>", methods=["POST"])
    def show_edit(self, id):
        return super().show_edit()

    @route("hide_edit/<id>", methods=["POST"])
    def hide_edit(self, id):
        return super().hide_edit()

    @route("update/<id>", methods=["POST"])
    def update(self, id):
        self.category.name = request.form["name"]
        self.category.description = request.form["description"]
        self.category.save()

        return super().update()

    @route("edit/<id>", methods=["POST"])
    def edit(self, id):
        self.category.name = request.form["ingredient-category"]
        self.category.save()

        return super().edit()

    def post(self):
        self.category = IngredientCategory(
            name=request.form["ingredient-category"])
        self.category.save()

        return super().post()

    def delete(self, id):
        if self.category.is_used:
            flash("už je někde použité, nelze smazat!", category="error")
            return redirect(url_for("IngredientCategorieView:index"),
                            code="303")

        self.category.delete()
        return super().delete()
Exemplo n.º 2
0
class MeasurementView(HelperFlaskView, AdminViewMixin):
    decorators = [login_required, permissions_required("manage-application")]

    @login_required
    def before_request(self, name, id=None, *args, **kwargs):
        self.measurement = Measurement.load(id)

    def before_index(self):
        self.measurements = Measurement.load_all()

    def index(self):
        return self.template()

    @route("/show_edit/<id>", methods=["POST"])
    def show_edit(self, id):
        return super().show_edit()

    @route("measurements/hide_edit/<id>", methods=["POST"])
    def hide_edit(self, id):
        return super().hide_edit()

    @route("measurements/update/<id>", methods=["POST"])
    def update(self, id):
        self.measurement.name = request.form["name"]
        self.measurement.description = request.form["description"]
        self.measurement.save()

        return super().update()

    def post(self):
        self.measurement = Measurement(name=request.form["measurement"])
        self.measurement.save()

        return super().post()

    def delete(self, id):
        if self.measurement.is_used:
            flash("už je někde použité, nelze smazat!")
            return redirect(url_for("MeasurementView:index"), code=303)

        self.measurement.delete()

        return super().delete()
Exemplo n.º 3
0
class AdminView(HelperFlaskView):
    decorators = [login_required, permissions_required("manage-application")]

    def index(self):
        return self.template(template_name="admin/index.html.j2")

    def conversion_index(self):
        return redirect(url_for("ConversionView:index"))

    def measurements_index(self):
        return redirect(url_for("MeasurementView:index"))

    def recipe_categories_index(self):
        return redirect(url_for("RecipeCategorieView:index"))

    def ingredient_categories_index(self):
        return redirect(url_for("IngredientCategorieView:index"))

    def public_ingredients_index(self):
        return redirect(url_for("PublicIngredientView:index"))

    def tips_index(self):
        return redirect(url_for("TipView:manage"))

    def files(self):
        return redirect(url_for("FileView:index"))

    def unapproved_tips_count(self):
        self.notification_count = self._unapproved_tips_count()
        return self.template(
            template_name="admin/_admin_notification_count.html.j2")

    def admin_notification_count(self):
        self.notification_count = self._unapproved_tips_count()
        return self.template(
            template_name="admin/_admin_notification_count.html.j2")

    def _unapproved_tips_count(self) -> int:
        return len(Tip.unapproved_tips())
Exemplo n.º 4
0
class ConversionView(HelperFlaskView):
    decorators = [login_required, permissions_required("manage-application")]

    def index(self):
        self.conversions = Conversion.load_all()
        self.ingredients = Ingredient.load_all()
        return self.template()

    @route("/add/<ingredient_id>", methods=["POST"])
    def add(self, ingredient_id):
        new_measurement_id = request.form["new_measurement_id"]
        amount_from = request.form["amount_from"]
        amount_to = request.form["amount_to"]

        conversion = Conversion(
            ingredient_id=ingredient_id,
            to_measurement_id=new_measurement_id,
            amount_from=amount_from,
            amount_to=amount_to,
        )
        conversion.save()

        return redirect(url_for("IngredientView:show", id=ingredient_id))
Exemplo n.º 5
0
class PublicIngredientView(HelperFlaskView):
    decorators = [login_required, permissions_required("manage-application")]
    template_folder = "ingredients/public"

    def before_request(self, name, id=None, *args, **kwargs):
        self.ingredient = Ingredient.load(id)

        self.measurements = Measurement.load_all()
        self.categories = IngredientCategory.load_all()
        self.public_ingredients = Ingredient.load_all_public()

    def index(self, edit_id=None):
        self.edit_id = int(request.args.get("edit_id", -1))
        return self.template()

    @route("/ingredients/show_edit/<id>", methods=["POST"])
    def show_edit(self, id):
        if turbo.can_stream():
            return turbo.stream(
                [
                    turbo.after(
                        self.template(template_name="_edit"),
                        target=f"public-ingredient-{self.ingredient.id}",
                    ),
                    turbo.replace(
                        self.template(template_name="_ingredient", editing=True),
                        target=f"public-ingredient-{self.ingredient.id}",
                    ),
                ]
            )
        else:
            return redirect(
                url_for("PublicIngredientView:index", edit_id=self.ingredient.id)
            )

    @route("ingredients/hide_edit/<id>", methods=["POST"])
    def hide_edit(self, id):
        if turbo.can_stream():
            return turbo.stream(
                [
                    turbo.remove(
                        target=f"public-ingredient-edit-{self.ingredient.id}",
                    ),
                    turbo.replace(
                        self.template(template_name="_ingredient"),
                        target=f"public-ingredient-{self.ingredient.id}",
                    ),
                ]
            )
        else:
            return redirect(url_for("PublicIngredientView:index"))

    @route("update/<id>", methods=["POST"])
    def update(self, id):
        self.ingredient.category = IngredientCategory.load(request.form["category_id"])
        self.ingredient.measurement = Measurement.load(request.form["measurement_id"])

        self.ingredient.save()

        if turbo.can_stream():
            return turbo.stream(
                [
                    turbo.replace(
                        self.template(template_name="_ingredient"),
                        target=f"public-ingredient-{self.ingredient.id}",
                    ),
                    turbo.remove(target=f"public-ingredient-edit-{self.ingredient.id}"),
                ]
            )
        else:
            return redirect(url_for("PublicIngredientView:index"))