Пример #1
0
def add_title():
    """
    Add a title to the database
    """
    check_admin()

    add_title = True

    form = TitleForm()
    if form.validate_on_submit():
        title = Title(name=form.name.data)
        try:
            # add title to the database
            db.session.add(title)
            db.session.commit()
            flash('You have successfully added a new title.')
        except:
            # in case title name already exists
            flash('Error: title name already exists.')

        # redirect to titles page
        return redirect(url_for('admin.list_titles'))

    # load title template
    return render_template('admin/titles/title.html',
                           action="Add",
                           add_title=add_title,
                           form=form,
                           title="Add Title")
Пример #2
0
def edit_title(id):
    """
    Edit a title
    """
    check_admin()

    add_title = False

    title = Title.query.get_or_404(id)
    form = TitleForm(obj=title)
    if form.validate_on_submit():
        title.name = form.name.data
        db.session.commit()
        flash('You have successfully edited the title.')

        # redirect to the titles page
        return redirect(url_for('admin.list_titles'))

    form.name.data = title.name
    return render_template('admin/titles/title.html',
                           action="Edit",
                           add_title=add_title,
                           form=form,
                           title_edit=title,
                           title="Edit Title")