示例#1
0
def update_recipe(recipe_id: int):
    """Router for update recipe."""
    recipe = get_recipe(recipe_id)

    if recipe.author == current_user or current_user.role == 1:
        form = RecipeForm()
        if form.validate_on_submit():
            picture_file = save_picture(form.picture.data)
            recipe.image_file = picture_file
            recipe.name = form.name.data
            recipe.ingredients = form.ingredients.data
            recipe.description = form.description.data
            recipe.category_id = form.category.data
            recipe.date_updated = datetime.utcnow()
            db.session.commit()
            flash(f'Изменения успешно сохранены!', 'success')
            return redirect(url_for('recipes'))
        elif request.method == 'GET':
            form.name.data = recipe.name
            form.ingredients.data = recipe.ingredients
            form.description.data = recipe.description
            form.picture.data = recipe.image_file
            form.category.data = recipe.category_id
        image_file = url_for('static',
                             filename='recipe_photos/' + recipe.image_file)
        categories = RecipeForm.categories_list

        return render_template('create_recipe.html',
                               title='Редактирование рецепта',
                               form=form,
                               categories=categories,
                               image_file=image_file,
                               legend='Редактирование рецепта')
    else:
        abort(403)
示例#2
0
def create_recipe():
    """Router for create recipe page."""
    form = RecipeForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            picture_file = save_picture(form.picture.data)
            user_id = current_user.id
            recipe = Recipes(name=form.name.data,
                             ingredients=form.ingredients.data,
                             description=form.description.data,
                             image_file=picture_file,
                             date_posted=datetime.utcnow(),
                             date_updated=datetime.utcnow(),
                             category_id=form.category.data,
                             user_id=user_id)
            db.session.add(recipe)
            db.session.commit()
            flash(f'Рецепт {form.name.data} сохранен!', 'success')
            return redirect(url_for('recipes'))
    elif request.method == 'GET':
        categories = RecipeForm.categories_list

        return render_template('create_recipe.html',
                               title='Добавление рецепта',
                               form=form,
                               categories=categories,
                               legend='Добавление рецепта')
示例#3
0
def register():
    """Router for register page."""
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        picture_file = save_picture(form.picture.data)
        current_user.image_file = picture_file
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = Users(username=form.username.data,
                     email=form.email.data,
                     image_file=picture_file,
                     password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(
            f'Аккаунт {form.username.data} успешно зарегистрирован! Войдите в систему.',
            'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Регистрация', form=form)
示例#4
0
def update_user(user_id: int):
    """Router for update users."""
    user = get_user(user_id)
    form = UpdateAccountForm()
    if form.validate_on_submit():
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            user.image_file = picture_file
        user.username = form.username.data
        user.email = form.email.data
        db.session.commit()
        flash(f'Изменения успешно сохранены!', 'success')
        return redirect(url_for('users'))
    elif request.method == 'GET':
        form.username.data = user.username
        form.email.data = user.email
    image_file = url_for('static',
                         filename='photos/' + user.image_file)
    return render_template('account.html',
                           title='Профиль',
                           image_file=image_file,
                           legend='Изменение профиля',
                           form=form,
                           user=user)
示例#5
0
def account():
    """Router for account page."""
    form = UpdateAccountForm()
    if form.validate_on_submit():
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            current_user.image_file = picture_file
        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash(f'Изменения успешно сохранены!', 'success')
        return redirect(url_for('account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
    recipes = current_user.recipes
    image_file = url_for('static',
                         filename='photos/' + current_user.image_file)
    return render_template('account.html',
                           title='Профиль',
                           image_file=image_file,
                           recipes=recipes,
                           legend='Редактирование профиля',
                           form=form)