def menu_edit(id): '''/admin/Menu/edit''' # 生成菜单数据 data = Menu.mlist_to_tree() list = Menu.form_at_tree(data) list.insert(0, {"id": "0", "showName": u"顶级菜单"}) menu = Menu.query.get_or_404(int(id)) #form form = AddmenuForm() menus = [(int(n['id']), n['showName']) for n in list] form.fid.choices = menus if form.validate_on_submit(): menu.name = form.name.data menu.fid = form.fid.data menu.hide = form.hide.data menu.url = form.url.data menu.sort = form.sort.data db.session.add(menu) db.session.commit() return jsonify({'code': 1, 'msg': '菜单修改成功!'}) if form.errors: return jsonify({'code': 0, 'msg': '菜单修改失败!'}) form.name.data = menu.name form.fid.data = menu.fid form.hide.data = menu.hide form.url.data = menu.url form.sort.data = menu.sort return render_template('admin/menu/edit.html', form=form, menu=menu)
def index(): '''/admin/index''' if not int(current_user.id) == 1: menu = Menu.glist_to_tree(session.get('gid')) else: menu = Menu.mlist_to_tree() mlist = Menu.form_at_tree(menu) session['enable_url'] = filter(None, list(set(auth['url'] for auth in mlist))) #处理隐藏菜单 data = [m for m in mlist if int(m['hide']) == 0] return render_template('admin/index.html', list=data, username=session.get('username'))
def menu_add(): '''/admin/Menu/add''' #生成菜单数据 data = Menu.mlist_to_tree() list = Menu.form_at_tree(data) list.insert(0, {"id": "0", "showName": u"顶级菜单"}) #form form = AddmenuForm() form.fid.choices = [(int(n['id']), n['showName']) for n in list] if form.validate_on_submit(): Menu.insert_menu(form.name.data, form.fid.data, form.hide.data, form.url.data, form.sort.data) return jsonify({'code': 1, 'msg': '菜单添加成功!'}) if form.errors: return jsonify({'code': 0, 'msg': '菜单添加失败!'}) return render_template('admin/menu/add.html', form=form)