def post(self, recipe_id): form = RecipeForm(request.form) if not form.validate_on_submit(): save_form_to_session(request.form) return redirect(url_for("RecipeView:edit", id=self.recipe.id)) form.populate_obj(self.recipe) self.recipe.edit() self.recipe.reload() if turbo.can_stream(): return turbo.stream([ turbo.replace( self.template("_info", message="Upraveno", form=form), target="recipe-info", ), turbo.replace( self.template("_ingredient_table"), target="recipe-ingredient-table", ), ]) else: return redirect(url_for("RecipeView:edit", id=self.recipe.id))
def update(self): if turbo.can_stream(): return turbo.stream([ turbo.replace( self.template(template_name="_row"), target=f"{self._attribute_name}-{self._instance.id}", ), turbo.remove( target=f"{self._attribute_name}-edit-{self._instance.id}"), ]) else: return redirect(url_for(f"{self.name}:index"))
def post_description(self, recipe_id): self.recipe.description = request.form["description"] self.recipe.edit() if turbo.can_stream(): return turbo.stream( turbo.replace( self.template("_description", message="Upraveno"), target="recipe-description", )) else: return redirect(url_for("RecipeView:edit", id=self.recipe.id))
def post(self): if turbo.can_stream(): return turbo.stream([ turbo.append( self.template(template_name="_row"), target=f"{self._plural_attribute_name}", ), turbo.replace( self.template(template_name="_add"), target=f"{self._attribute_name}-create-form", ), ]) else: return redirect(url_for(f"{self.name}:index"))
def toggle_reaction(self, recipe_id, refresh=False): recipe = Recipe.load(recipe_id) recipe.toggle_reaction() # TODO: This should be made without turbo, but problem with Bootstrap table if turbo.can_stream() and not refresh: return turbo.stream( turbo.replace( template("public_recipes/_recipe_row.html.j2", recipe=recipe), target=f"recipe-{recipe_id}", )) return redirect(request.referrer)
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"))
def show_edit(self): if turbo.can_stream(): return turbo.stream([ turbo.after( self.template(template_name="_edit"), target=f"{self._attribute_name}-{self._instance.id}", ), turbo.replace( self.template(template_name="_row", editing=True), target=f"{self._attribute_name}-{self._instance.id}", ), ]) else: return redirect( url_for(f"{self.name}:index", edit_id=self._instance.id))
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"))
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) )
if dietary_labels := self.form.dietary_labels.data: self.recipes = [ r for r in self.recipes if r.has_labels(dietary_labels) ] if difficulty_labels := self.form.difficulty_labels.data: self.recipes = [ r for r in self.recipes if r.has_any_of_labels(difficulty_labels) ] # TODO: This should be made without turbo, but problem with Bootstrap table if turbo.can_stream(): return turbo.stream( turbo.replace( self.template(template_name="_recipes_table"), target="recipes-table", )) else: return self.template() @route("public-index/") def public_index(self): if current_user.is_authenticated: return redirect(url_for("PublicRecipeView:index")) return self.template(template_name="public_index") @route("gallery/") def gallery(self): self.recipes = Recipe.load_all_public_with_image()