Ejemplo n.º 1
0
def update_recipe(recipe_id):
    if current_user.is_admin:
        recipe = Recipe.query.get_or_404(recipe_id)
        form = RecipeForm()
        form.poll.choices = [(poll.id, poll.name)
                             for poll in Poll.query.order_by('date_created')]

        if form.validate_on_submit():
            recipe.name = form.name.data
            recipe.description = form.description.data
            recipe.poll = Poll.query.filter_by(id=form.poll.data).first()
            db.session.commit()
            flash('The recipe has been updated!', 'success')
            return redirect(url_for('recipe', recipe_id=recipe.id))

        elif request.method == 'GET':
            form.name.data = recipe.name
            form.description.data = recipe.description
            form.poll.data = recipe.poll.id

        return render_template('recipe_form.html',
                               title='Update - ' + recipe.name + ' - Recipe',
                               description='Fill in the details to be changed',
                               legend='Update - ' + recipe.name + ' - Recipe',
                               form=form)
    else:
        abort(403)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
def add_recipe():
    if current_user.is_admin:
        form = RecipeForm()

        form.poll.choices = [(poll.id, poll.name)
                             for poll in Poll.query.order_by('date_created')]

        if form.validate_on_submit():
            recipe = Recipe(name=form.name.data,
                            description=form.description.data,
                            contributor_id=current_user.id,
                            poll_id=form.poll.data)

            db.session.add(recipe)  # adds to the database
            db.session.commit()  # commits all the changes in the database
            flash('The recipe has been added', 'success')
            return redirect(url_for('add_recipe'))

        return render_template('recipe_form.html',
                               title='Add Recipe',
                               description='Fill in the details of the recipe',
                               legend='Add a Recipe',
                               form=form)
    else:
        abort(403)
Ejemplo n.º 4
0
def new_recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe()
        save_recipe(recipe, form)
        flash("Recipe {} added successfully".format(form.name.data))
        return redirect("/index")

    return render_template("edit.html", title="Create recipe", form=form)
Ejemplo n.º 5
0
def edit_recipe(id):
    recipe = Recipe.query.filter_by(id=id).first_or_404()
    form = RecipeForm(obj=recipe)
    if form.validate_on_submit():
        save_recipe(recipe, form)
        flash("Recipe {} saved succesfully".format(form.name.data))
        return redirect("/recipe/{}".format(recipe.id))

    return render_template("edit.html", title="Edit recipe", form=form)
Ejemplo n.º 6
0
def new_recipe():
    recipe_form = RecipeForm()
    if recipe_form.validate_on_submit():
        user = current_user
        recipe = Recipe(body=recipe_form.name.data, user_id=user.id)
        db.session.add(recipe)
        db.session.commit()
        flash('New recipe added!')
        return redirect(url_for('index'))
    return render_template('new_recipe.html', recipe_form=recipe_form)
Ejemplo n.º 7
0
    def post(self):
        form = RecipeForm(request.form)

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

        recipe = Recipe(author=current_user)
        recipe.fill(form)
        recipe.save()

        return redirect(url_for("RecipeView:edit", id=recipe.id))
Ejemplo n.º 8
0
def submit():
    """submit a recipe"""
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(title=form.title.data,
                        ingredients=form.ingredients.data,
                        instructions=form.instructions.data,
                        author=current_user)
        db.session.add(recipe)
        db.session.commit()
        return redirect(f'/recipe/{recipe.id}')
    return render_template('submit.html', title="submit", form=form)
Ejemplo n.º 9
0
def new_recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(name=form.name.data,
                        author=current_user,
                        notes=form.notes.data)
        db.session.add(recipe)
        db.session.commit()
        flash('Receptet har sparats.')
        return redirect(url_for('recipe', recipe_id=recipe.id))
    return render_template('new_recipe.html',
                           form=form,
                           headline='Nytt recept')
Ejemplo n.º 10
0
def new_recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(title=form.title.data,
                        content=form.content.data,
                        ingredients=form.ingredients.data,
                        image_file_recipe=form.picture.data,
                        author=current_user)
        db.session.add(recipe)
        db.session.commit()
        flash('Twój przepis został dodany!', 'success')
        return redirect(url_for('home'))
    return render_template('new_recipe.html',
                           title='New Title',
                           form=form,
                           legend='Stwórz nowy przepis')
Ejemplo n.º 11
0
def add_recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(recipe_name=form.recipe_name.data,
                        description=form.description.data,
                        cook_time=form.cook_time.data,
                        start_day_before=form.start_day_before.data,
                        lunchbox=form.lunchbox.data,
                        author=current_user)
        db.session.add(recipe)
        db.session.commit()
        flash('Your recipe has been saved')
        return redirect(url_for('home'))
    return render_template('add_recipe.html',
                           title='Add your awesome recipe here',
                           form=form)
Ejemplo n.º 12
0
def update_recipe(recipe_id):
    recipe = Recipe.query.get_or_404(recipe_id)
    if recipe.author != current_user:
        abort(403)
    form = RecipeForm()
    if form.validate_on_submit():
        recipe.title = form.title.data
        recipe.content = form.content.data
        recipe.ingredients = form.ingredients.data
        db.session.commit()
        flash('Twój przepis jest zaktualizowany', 'success')
    elif request.method == 'GET':
        form.title.data = recipe.title
        form.content.data = recipe.content
        form.ingredients.data = recipe.ingredients
    return render_template('new_recipe.html',
                           title='Update Recipe',
                           form=form,
                           legend='Edytuj przepis')
Ejemplo n.º 13
0
def edit_recipe(recipe_id=None):
    recipe = Recipe.query.get(int(recipe_id))
    if not recipe.author == current_user:
        flash('Du kan bara redigera dina egna recept')
        return redirect(url_for('recipe', recipe_id=recipe_id))
    form = RecipeForm()
    form.name.data = recipe.name
    form.notes.data = recipe.notes
    if form.validate_on_submit():
        recipe.name = request.form.get('name', None)
        recipe.notes = request.form.get('notes', None)
        db.session.commit()
        flash('Receptet har redigerats.')
        return redirect(url_for('recipe', recipe_id=recipe_id))
    return render_template('edit_recipe.html',
                           title='Redigera',
                           recipe=recipe,
                           headline='Redigera recept',
                           form=form)
Ejemplo n.º 14
0
def add_recipe():
    form = RecipeForm()
    quantities = MeasurementQty.query.all()
    units = MeasurementUnit.query.order_by(MeasurementUnit.factor).all()
    ingredients = Ingredient.query.order_by(Ingredient.name).all()
    if form.validate_on_submit():
        # Could choose to set default author to logged in user if none is supplied
        recipe = Recipe(name=form.recipe.data,
                        description=form.description.data,
                        author=current_user,
                        servings=form.servings.data,
                        comments=form.comments.data,
                        source=form.source.data)
        # Tags
        # No implemented yet
        # Ingredients
        db.session.add(recipe)
        if form.ingredient.validate(form):
            for ingredient in form.ingredient.data:
                query = IngredientSet.add_set(
                    quantity=ingredient['quantity'],
                    unit=ingredient['unit'],
                    ingredient=ingredient['ingredient'])
                if query is not None:
                    recipe.add_ingredient(query)
                else:
                    print('Set does not exist placeholder')
        # Steps
        if form.ingredient.validate(form):
            for step in form.step.data:
                step = RecipeStep(step_number=step['step_number'],
                                  step_text=step['step'])
                recipe.steps.append(step)
        db.session.commit()
        flash('Recipe added')
        return redirect(url_for('index'))
    return render_template('add_recipe.html',
                           title='Add recipe',
                           form=form,
                           quantities=quantities,
                           units=units,
                           ingredients=ingredients)
def postrecipe():
    form = RecipeForm()
    search_form = SearchForm()
    if form.validate_on_submit():
        mongo.db.recipes.insert_one({
            'name': form.recipe_name.data,
            'desc': form.recipe_desc.data,
            'ingredients': form.ingredients.data,
            'method': form.method.data,
            'owner': current_user.username,
            'tags': form.tags.data,
            'image': form.image.data,
            'likes': []
        })
        flash('Recipe added!', 'success')
        return redirect(url_for('index'))
    return render_template('add_recipe.html',
                           form=form,
                           search_form=search_form,
                           title='Post Recipe')
Ejemplo n.º 16
0
def copy_recipe(recipe_id):
    original = Recipe.query.get(int(recipe_id))
    form = RecipeForm()
    form.name.data = original.name
    form.notes.data = original.notes
    if form.validate_on_submit():
        copy = Recipe(name=request.form.get('name', None),
                      notes=request.form.get('notes', None),
                      user_id=current_user.id,
                      original_author_id=original.author.id,
                      original_recipe_id=original.id)
        db.session.add(copy)
        db.session.commit()
        flash('Receptet har sparats')
        return redirect(url_for('recipe', recipe_id=copy.id))
    return render_template('edit_recipe.html',
                           title='Spara en kopia',
                           headline='Spara en kopia',
                           form=form,
                           recipe=original)
Ejemplo n.º 17
0
def recipes():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(name=form.name.data)
        db.session.add(recipe)
        db.session.commit()
        flash(_('Your recipe added!'))
        return redirect(url_for('recipe', name=recipe.name))
    page = request.args.get('page', 1, type=int)
    recipes = Recipe.query.order_by(Recipe.name).paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('recipes', page=recipes.next_num) \
        if recipes.has_next else None
    prev_url = url_for('recipes', page=recipes.prev_num) \
        if recipes.has_prev else None
    return render_template('recipes.html',
                           title=_('Recipes'),
                           form=form,
                           recipes=recipes.items,
                           next_url=next_url,
                           prev_url=prev_url)
def editrecipe(id):
    form = RecipeForm()
    search_form = SearchForm()
    recipe = mongo.db.recipes.find_one({"_id": ObjectId(id)})
    # stops user editing recipes not owned by finding recipe id in source code
    if current_user.username == recipe['owner'] or current_user.is_admin:
        if request.method == "POST":
            if form.validate_on_submit():
                mongo.db.recipes.update_one({"_id": ObjectId(id)}, {
                    "$set": {
                        'name': form.recipe_name.data,
                        'desc': form.recipe_desc.data,
                        'ingredients': form.ingredients.data,
                        'method': form.method.data,
                        'tags': form.tags.data,
                        'image': form.image.data,
                        'owner': current_user.username
                    }
                })
                flash('Recipe Updated', 'info')
                return redirect(url_for('index'))
            return render_template('add_recipe.html',
                                   form=form,
                                   search_form=search_form,
                                   title='Edit Recipe')
        elif request.method == "GET":
            # Populates recipe form with data from db
            form.recipe_name.data = recipe['name']
            form.recipe_desc.data = recipe['desc']
            for ingredient in recipe['ingredients']:
                form.ingredients.append_entry(data=ingredient)
            form.method.data = recipe['method']
            form.image.data = recipe['image']
            form.tags.data = ', '.join(map(str, recipe['tags']))
            return render_template('add_recipe.html',
                                   form=form,
                                   search_form=search_form,
                                   title='Edit Recipe')
        flash('Action not allowed', 'warning')
        return redirect(url_for('index'))
Ejemplo n.º 19
0
def addRecipe():
    form = RecipeForm()
    if request.method == 'POST' and form.validate_on_submit():
        title = form.title.data
        desc = form.desc.data
        prep = form.prep.data
        cal = form.cal.data
        date = datetime.today().strftime("%y-%m-%d")

        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('Select RecID from recipes order by length(RecID) DESC,RecID Desc Limit 1')
        lastrid = cursor.fetchone()['RecID'].split('-')[1]
        cursor.execute('Select DescID from recipe_description order by length(DescID) DESC,DescID Desc Limit 1')
        lastdid = cursor.fetchone()['DescID'].split('-')[1]


        cursor.execute('Insert into recipe_description values (%s,%s)', ('DESC-' + str(int(lastdid)+1),desc))
        cursor.execute('Insert into recipes values (%s,%s,%s,%s,%s,%s)', ('RE-' + str(int(lastrid)+1),title,cal,'DESC-' + str(int(lastdid)+1),date,prep))
        mysql.connection.commit()
        flash('Recipe Added')
        print(cal,date)
        return redirect(url_for('recipe',id='RE-' + str(int(lastrid)+1)))
    return render_template('addRecipe.html',form=form)
Ejemplo n.º 20
0
def edit_recipe(rid):
    target = Recipe.query.filter_by(id=rid).one_or_none()

    if (current_user.admin or target.author == current_user) or target is None:
        form = RecipeForm()
        form.submit_button.label = f"Edit {target.title}"
        if form.validate_on_submit():
            target.title = form.title.data
            target.instructions = form.instructions.data
            target.ingredients = form.ingredients.data
            db.session.add(target)
            db.session.commit()
            flash(f'successfully edited {target.title}')
            return redirect(f'/recipe/{rid}')

    else:
        flash('you do not have permission to edit this recipe')
        redirect('/')
    form = RecipeForm(title=target.title,
                      ingredients=target.ingredients,
                      instructions=target.instructions)
    return render_template('submit.html',
                           title=f'Editing {target.title}',
                           form=form)