예제 #1
0
def add_menu_item(menu_id):
    form = EditMenuItemForm()
    the_menu = Menu.query.filter_by(id=menu_id).first()

    if form.validate_on_submit():
        the_menu_item = MenuItem()

        the_menu_item.name = form.name.data
        the_menu_item.slug = form.slug.data
        the_menu_item.weight = form.weight.data
        the_menu_item.menu_id = form.menu.data
        the_menu_item.created_on = datetime.utcnow()

        items = MenuItem.query.filter_by(menu_id=menu_id, name=the_menu_item.name).all()
        if len(items) > 0:
            flash("Menu Item can't use the same name as another item in the same menu")
            return render_template("admin/menus/menu-item/menu-item.html", form=form, menu_item=the_menu_item)

        db.session.add(the_menu_item)
        flash("{0} has been created".format(the_menu_item.name))

        return redirect(url_for(".menu", menu_id=the_menu_item.menu_id))

    form.menu.data = menu_id

    return render_template("admin/menus/menu-item/new.html", js='menus/menu-item/new', form=form, menu=the_menu)