Exemple #1
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        name = Role.query.filter_by(name=data['name']).count()
        auths = Role.query.filter_by(auths=data['auths']).count()
        if name != 0:
            flash('角色名称已经存在', 'err')
            return redirect(url_for('admin.role_add'))
        elif auths != 0:
            flash('权限列表已经存在', 'err')
            return redirect(url_for('admin.role_add'))
        else:
            role = Role(
                name=data['name'],
                auths=','.join(map(lambda v: str(v),
                                   data['auths'])),  #将数据变成字符串
            )
            db.session.add(role)
            db.session.commit()
            flash('添加角色成功', 'ok')
            oplog = Oplog(admin_id=session['admin_id'],
                          ip=request.remote_addr,
                          reason='添加标签:%s' % data['name'])
            db.session.add(oplog)
            db.session.commit()
            return redirect(url_for('admin.role_add'))

    return render_template('admin/role_add.html', form=form)
Exemple #2
0
def edit_role(id):
    """
    Edit a role
    """
    check_admin()

    add_role = False

    role = Role.query.get_or_404(id)
    form = RoleForm(obj=role)
    if form.validate_on_submit():
        role.name = form.name.data
        role.description = form.description.data
        db.session.add(role)
        db.session.commit()
        flash('You have successfully edited the role.')

        # redirect to the roles page
        return redirect(url_for('admin.list_roles'))

    form.description.data = role.description
    form.name.data = role.name
    return render_template('admin/roles/role.html',
                           add_role=add_role,
                           form=form,
                           title="Edit Role")
Exemple #3
0
def roleedit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == "GET":
        auths = role.auths
        form.auths.data = list(map(lambda v: int(v), auths.split(",")))

    if form.validate_on_submit():
        data = form.data

        role.name = data['name']
        role.auths = ",".join(map(lambda v: str(v), data['auths']))
        db.session.add(role)
        db.session.commit()
        flash("编辑角色成功!", "ok")
        oplog = Oplog(
            admin_id=session['admin_id'],
            ip=request.remote_addr,
            reason="编辑%s角色成功," % data['name'],
        )
        db.session.add(oplog)
        db.session.commit()

        return redirect(url_for('admin.authedit', id=id))
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #4
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    form.auths.choices = [(v.id, v.name) for v in Auth.query.all()]
    if request.method == "GET":
        form.name.flags.required = False
        form.auths.flags.required = False
        form.auths.data = list(map(lambda v: int(v), role.auths.split(",")))
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data["name"]).count()
        if role.name != data["name"] and role_count == 1:
            flash("角色已存在", category="error")
        else:
            role.name = data["name"]
            role.auths = ",".join(map(lambda v: str(v), data["auths"]))
            operatelog = OperateLog(admin_id=session["admin_id"],
                                    ip=request.remote_addr,
                                    reason="修改角色:{}".format(role.name))
            try:
                db.session.add(operatelog)
                db.session.add(role)
                db.session.commit()
                flash("角色修改成功", category="ok")
            except Exception as e:
                print(e)
                flash("角色修改失败", category="error")
                db.session.rollback()
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #5
0
def role_edit(id=None):

    form = RoleForm()
    role = Role.query.get_or_404(int(id))
    auth_list = Auth.query.all()
    auth_id = list(map(lambda v: int(v), role.auths.split(",")))

    if form.validate_on_submit():
        data = form.data
        name_count = Role.query.filter_by(name=data["name"]).count()
        if name_count == 1 and role.name != data["name"]:
            flash("角色名称已经存在!", "err")
            return redirect(url_for('admin.role_edit', id=id))

        # update role 表
        role.name = data["name"]
        role.auths = ",".join(
            map(lambda v: str(v), request.values.getlist("auths")))
        db.session.commit()

        flash("修改权限成功!", "ok")
        recod = "修改权限ID-%d" % (id)
        oplog_handle(recod)

    return render_template("admin/role_edit.html",
                           form=form,
                           role=role,
                           auth_list=auth_list,
                           auth_id=auth_id)
Exemple #6
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(int(id))
    if request.method == "GET":
        form.auths.data = list(map(int, role.auths.split(",")))
    if form.validate_on_submit():
        data = form.data
        if data["name"] == role.name and data["auths"] == list(
            map(int, role.auths.split(","))
        ):
            flash("老板,您未作修改哟~", "err")
            return redirect(url_for("admin.role_edit", id=id))
        role.name = data["name"]
        role.auths = ",".join(map(str, data["auths"]))
        db.session.add(role)
        oplog = OpLog(
            admin_id=session["admin_id"],
            ip=request.remote_addr,
            reason='修改角色"{0}"的信息'.format(data["name"]),
        )
        db.session.add(oplog)
        db.session.commit()
        flash("角色信息修改成功!", "ok")
        return redirect(url_for("admin.role_edit", id=id))
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #7
0
def role_add():
    form = RoleForm()
    if request.method == "GET":
        form.name.flags.required = False
        form.auths.flags.required = False
    form.auths.choices = [(v.id, v.name) for v in Auth.query.all()]
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data["name"]).count()
        if role_count == 1:
            flash("角色已存在", category="error")
        else:
            role = Role(
                name=data["name"],
                # 这种拼接只能是字符串
                auths=",".join(map(lambda v: str(v), data["auths"])))
            operatelog = OperateLog(admin_id=session["admin_id"],
                                    ip=request.remote_addr,
                                    reason="添加角色:{}".format(data["name"]))
            try:
                db.session.add(role)
                db.session.add(operatelog)
                db.session.commit()
                flash("角色添加成功", category="ok")
            except Exception as e:
                print(e)
                flash("角色添加失败", category="error")
                db.session.rollback()
    return render_template("admin/role_add.html", form=form)
Exemple #8
0
def role_edit(id):
    username = get_username()
    form=RoleForm()
    role=Role.query.filter_by(id=id).first()
    if request.method=='GET':
        form.name.data=role.name
        auths=role.auths
        form.auths.data=list(map(lambda v:int(v),auths.split(',')))
    if form.validate_on_submit():
        data=form.data
        role_count=Role.query.filter_by(name=data['name']).count()
        if role_count==1 and data['name']!=role.name:
            flash('已有该角色','err')
            return redirect(url_for('admin.role_add'))
        name=data['name']
        '''data['auths']:返回是一个数组,把他用逗号分隔成字符串'''
        auths=','.join(map(lambda v:str(v),data['auths']))
        role.name=name
        role.auths=auths

        db.session.add(role)
        oplog = Oplog(
            ip=request.remote_addr,
            admin_id=session["admin_id"],
            reson='编辑角色' + '<' + role.name + '>',

        )
        db.session.add(oplog)
        db.session.commit()
        flash('编辑角色成功','ok')
    context = {
        'username': username
    }
    return render_template("admin/role_edit.html",context=context,form=form)
Exemple #9
0
def role_edit(id=None):
    """
     编辑角色
    """
    form = RoleForm()
    role = Role.query.get_or_404(id)
    # print(role)
    if request.method == "GET":
        auths = role.auths
        # print(auths)
        # get时进行赋值。应对无法模板中赋初值
        if auths != '':
            form.auths.data = list(map(lambda v: int(v), auths.split(",")))
        else:
            form.auths.data = []
    if form.validate_on_submit():
        data = form.data
        role.name = data["name"]
        # print(data["name"])
        if data["name"] != "超级管理员":
            role.auths = ",".join(map(lambda v: str(v), data["auths"]))
            db.session.add(role)
            db.session.commit()
            flash("修改角色成功!", "ok")
        else:
            flash("超级管理员无法修改!", "err")
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #10
0
def add_role():
    """
    Add a role to the database
    """
    check_admin()

    add_role = True

    form = RoleForm()
    if form.validate_on_submit():
        role = Role(name=form.name.data,
                    description=form.description.data,
                    manage=form.manage.data,
                    only_manage_department=form.only_manage_department.data)

        try:
            # add role to the database
            db.session.add(role)
            db.session.commit()
            flash('Đã thêm chức vụ thành công.')
        except:
            # in case role name already exists
            flash('Lỗi: Tên chức vụ đã tồn tại.')

        # redirect to the roles page
        return redirect(url_for('admin.list_roles'))

    # load role template
    return render_template('admin/roles/role.html',
                           add_role=add_role,
                           form=form,
                           title='Thêm chức vụ')
Exemple #11
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == 'GET':
        auths = role.auths
        form.auths.data = [int(v) for v in auths.split(',')]
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data['name']).count()
        if role_count >= 1 and role.name != data['name']:
            flash('角色已存在!', 'err')
            return redirect(url_for('admin.role_edit', id=id))

        role.name = data['name']
        role.auths = ','.join([str(v) for v in data['auths']])
        oplog = Oplog(
            admin_id=session['admin_id'],
            reason='修改一个角色:%s' % data['name'],
            ip=request.remote_addr
        )
        db.session.add(role)
        db.session.add(oplog)
        db.session.commit()
        flash('角色修改成功!', 'ok')
        return redirect(url_for('admin.role_edit', id=id))
    return render_template('admin/role_edit.html', form=form, role=role)
Exemple #12
0
def edit_role(id):
    """
    Edit a role
    """
    check_admin()

    add_role = False

    role = Role.query.get_or_404(id)
    form = RoleForm(obj=role)
    if form.validate_on_submit():
        role.name = form.name.data
        role.description = form.description.data
        role.manage = form.manage.data
        role.only_manage_department = form.only_manage_department.data
        db.session.add(role)
        db.session.commit()
        flash('Đã sửa chức vụ thành công.')

        # redirect to the roles page
        return redirect(url_for('admin.list_roles'))

    form.description.data = role.description
    form.name.data = role.name
    return render_template('admin/roles/role.html',
                           add_role=add_role,
                           form=form,
                           title="Sửa chức vụ")
Exemple #13
0
def role_edit(id=None):
    """修改角色"""
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == "GET":
        form.auths.data = list(map(lambda ah: int(ah),
                                   role.auths.split(",")))  #字符串转整形转列表
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data["name"]).count()
        if role_count == 1 and role.name != data["name"]:
            flash("角色名称已存在", "err")
            return redirect(url_for('admin.role_edit', id=id))
        role.name = data["name"]
        role.auths = ",".join(map(lambda ah: str(ah), data["auths"]))
        db.session.add(role)
        db.session.commit()
        flash("修改角色成功", "ok")
        adminoplog = AdminOpLog(
            admin_id=session["admin_id"],
            ip=request.remote_addr,  # 获取登陆ip,
            reason="修改了角色:%s→%s:%s" %
            (session["admin"], role.name, data["name"]))
        db.session.add(adminoplog)
        db.session.commit()
        return redirect(url_for("admin.role_edit", id=id))
    return render_template('admin/role_edit.html', form=form, role=role)
Exemple #14
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    old_role_name = role.name
    old_role_auths = role.auths
    if request.method == "GET":
        form.auths.data = list(map(lambda v: int(v),
                                   old_role_auths.split(",")))
    if form.validate_on_submit():
        data = form.data
        new_auth = ",".join(map(lambda v: str(v), data["auths"]))
        if old_role_name == data["name"] and old_role_auths == new_auth:
            flash("未做任何修改", "err")
            return redirect(url_for('admin.role_edit', id=id))
        role_count = Role.query.filter_by(name=data["name"]).count()
        if role.name != data["name"] and role_count == 1:
            flash("角色已经存在!", "err")
            return redirect(url_for('admin.role_edit', id=id))
        role.name = data["name"]
        role.auths = new_auth
        db.session.add(role)
        db.session.commit()
        reason = ""
        if old_role_name != role.name:
            reason = "将旧角色:%s 修改为 %s." % (old_role_name, role.name)
        if old_role_auths != role.auths:
            reason = reason + "将旧权限:%s 修改为:%s." % (old_role_auths, role.auths)
        add_oplog(reason)
        flash("修改角色成功!", "ok")
        redirect(url_for('admin.role_edit', id=id))
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #15
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == 'GET':
        auths = role.auths
        form.name.data = role.name
        form.auths.data = list(map(lambda v: int(v),
                                   auths.split(',')))  #数据库转化为字典
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data['name']).count()
        auths_str = ','.join(map(lambda v: str(v), data['auths']))  #输入项转化为字符串
        auths_count = Role.query.filter_by(auths=str(auths_str)).count()
        if role.name != data['name'] and role_count != 0:
            flash('角色名称已经存在', 'err')
            return redirect(url_for('admin.role_edit', id=id))
        else:
            role.name = data['name']
            role.auths = auths_str
            db.session.commit()
            flash('角色修改成功', 'ok')
            oplog = Oplog(admin_id=session['admin_id'],
                          ip=request.remote_addr,
                          reason='修改角色:%s' % data['name'])
            db.session.add(oplog)
            db.session.commit()
            return redirect(url_for('admin.role_edit', id=id))

    return render_template('admin/role_edit.html', form=form, role=role)
Exemple #16
0
def role_add():
    username = get_username()
    form=RoleForm()
    if form.validate_on_submit():
        data=form.data
        role_count=Role.query.filter_by(name=data['name']).count()
        if role_count==1:
            flash('已有该角色','err')
            return redirect(url_for('admin.role_add'))
        name=data['name']
        '''data['auths']:返回是一个数组,把他用逗号分隔成字符串'''
        auths=','.join(map(lambda v:str(v),data['auths']))
        role=Role(
            name=name,
            auths=auths,

        )
        db.session.add(role)
        oplog = Oplog(
            ip=request.remote_addr,
            admin_id=session["admin_id"],
            reson='添加角色' + '<' + role.name + '>',

        )
        db.session.add(oplog)
        db.session.commit()
        flash('角色添加成功','ok')
    context = {
        'username': username
    }
    return render_template("admin/role_add.html",context=context,form=form)
Exemple #17
0
def add_role():
    """
    Add a role to the database
    """
    check_admin()

    add_role = True

    form = RoleForm()
    if form.validate_on_submit():
        role = Role(name=form.name.data, description=form.description.data)

        try:
            # add role to the database
            db.session.add(role)
            db.session.commit()
            flash('You have successfully added a new role.')
        except:
            # in case role name already exists
            flash('Error: role name already exists.')

        # redirect to the roles page
        return redirect(url_for('admin.list_roles'))

    # load role template
    return render_template('admin/roles/role.html',
                           add_role=add_role,
                           form=form,
                           title='Add Role')
Exemple #18
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    old_name = role.name
    old_auths = role.auths
    if request.method == 'GET':
        auths = role.auths
        form.auths.data = list(map(lambda v: int(v), auths.split(',')))
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data['name']).count()
        if role.name != data['name'] and role_count == 1:
            flash("名称 [ {} ] 已存在".format(data['name']), "err")
            return redirect(url_for('admin.role_edit', id=id))
        role.name = data['name']
        role.auths = ','.join(map(lambda v: str(v), data['auths']))
        db.session.add(role)
        db.session.commit()
        flash("修改标签成功", "ok")
        oplog = Oplog(admin_id=session['admin_id'],
                      ip=request.remote_addr,
                      reason="编辑角色:{}=>{};{}=>{}".format(
                          old_name, data['name'], old_auths, data['auths']))
        db.session.add(oplog)
        db.session.commit()
        return redirect(url_for('admin.role_list', page=1))
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #19
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == "GET":
        auths = role.auths
        form.auths.data = list(map(lambda v: int(v), auths.split(",")))
    if form.validate_on_submit():
        form = RoleForm()
        role = Role.query.get_or_404(id)
        if form.validate_on_submit():
            data = form.data
            role.name = data['name']
            role.auths = ",".join(map(lambda v: str(v), data["auths"]))
            db.session.add(role)
            db.session.commit()
            flash("修改角色成功", "ok")
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #20
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(name=data["name"],
                    auths=",".join(map(lambda v: str(v), data["auths"])))
        db.session.add(role)
        db.session.commit()
        flash("添加角色成功", "ok")
    return render_template("admin/role_add.html", form=form)
Exemple #21
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(name=data['name'],
                    auths=','.join(map(lambda v: str(v), data['auths'])))
        db.session.add(role)
        db.session.commit()
        flash('添加成功', 'ok')
    return render_template("admin/role_add.html", form=form)
Exemple #22
0
def addRole():
    roleForm = RoleForm()
    if roleForm.validate_on_submit():
        data = roleForm.data
        role = Role(name=data["name"],
                    auth=",".join(map(lambda v: str(v), data["auth"])))
        db.session.add(role)
        db.session.commit()

        flash("添加角色成功!", "okey")
    return render_template("admin/addRole.html", form=roleForm)
Exemple #23
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(name=data["name"],
                    auths=",".join(map(lambda v: str(v), data["auths"])))
        db.session.add(role)
        db.session.commit()
        flash(u"角色添加成功!", "OK")
        return redirect(url_for("admin.role_add"))
    return render_template("admin/role_add.html", form=form)
Exemple #24
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(name=data['name'],
                    auths=",".join(map(lambda v: str(v), data['auths'])))
        db.session.add(role)
        db.session.commit()
        flash('添加角色成功')
        return redirect(url_for('admin.role_add'))
    return render_template('admin/role_add.html', form=form)
Exemple #25
0
def roleAdd():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        # 使用map(lambda v: str(v), data.get('auths') 把数组的数据转换为字符串
        role = Role(name=data.get('name'),
                    auths=",".join(map(lambda v: str(v), data.get('auths'))))
        db.session.add(role)
        db.session.commit()
        flash("角色添加成功!")
    return render_template("admin/role_add.html", form=form)
Exemple #26
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(name=data['name'],
                    auths=",".join(map(lambda auth: str(auth), data['auths'])))
        db.session.add(role)
        db.session.commit()
        flash("角色添加成功", "success")
        return redirect(url_for("admin.role_add"))
    return render_template("admin/role_add.html", form=form)
Exemple #27
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(
            name=data["name"],
            auths=",".join(map(lambda v: str(v), data["auths"]))
        )
        db.session.add(role)
        db.session.commit()
        flash("添加角色成功!", "ok")
    return render_template("admin/role_add.html", form=form)
Exemple #28
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role(
            name=data['name'],
            auths=','.join(map(lambda item: str(item), data['auths']))  # 数字转换为字符串形式
        )
        db.session.add(role)
        db.session.commit()
        flash('角色添加成功', category='ok')
    return render_template('admin/role_add.html', form=form)
Exemple #29
0
def role_list(page=None):
    form = RoleForm()
    if page is None:
        page = 1
    page_data = Role.query.order_by(Role.addtime.desc()).paginate(page=page,
                                                                  per_page=10)
    if form.validate_on_submit():
        data = form.data
        role_add(data)
    return render_template("admin/role_list.html",
                           page_data=page_data,
                           form=form)
Exemple #30
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        form.auths.choices = [(v.id, v.name) for v in Auth.query.all()]
        data = form.data
        role = Role(name=data["name"],
                    auths=",".join(map(lambda v: str(v), data["auths"])))
        db.session.add(role)
        db.session.commit()
        flash("添加角色成功", "ok")
        redirect(url_for("admin.role_add"))
    return render_template("admin/role_add.html", form=form)
Exemple #31
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        role = Role(
            name=form.name.data,
            auths=','.join(map(lambda v: str(v), form.auths.data))
        )
        db.session.add(role)
        db.session.commit()
        flash("角色添加成功", 'ok')
        return redirect(url_for('admin.role_add'))
    return render_template('admin/role_add.html', form=form)
Exemple #32
0
def role_edit(id=None):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == "GET":
        auths = role.auths
        form.auths.data = list(map(lambda v: int(v), auths.split(",")))
    if form.validate_on_submit():
        data = form.data
        role.name = data["name"]
        role.auths = ",".join(map(lambda v: str(v), data["auths"]))
        db.session.add(role)
        db.session.commit()
        flash("修改角色成功!", "ok")
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #33
0
def role_edit(id):
    form = RoleForm()
    role = Role.query.get_or_404(id)
    if request.method == "GET":
        auths = role.auths
        form.auths.data = list(map(lambda v: int(v), auths.split(",")))
    if form.validate_on_submit():
        data = form.data
        role.name = data["name"]
        role.auths = ",".join(map(lambda v: str(v), data["auths"]))
        db.session.add(role)
        db.session.commit()
        oplog = Oplog(
            admin_id=session["admin_id"],
            ip=request.remote_addr,
            reason="角色[%s]修改为[%s]" % (role.name, data["name"])
        )
        db.session.add(oplog)
        db.session.commit()
        flash("已成功修改角色", "ok")
        return redirect(url_for("admin.role_list", page=1))
    return render_template("admin/role_edit.html", form=form, role=role)
Exemple #34
0
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        role = Role.query.filter_by(name=data["name"]).count()
        if role == 1:
            flash("该角色已存在", "error")
            return redirect(url_for("admin.role_add"))
        role = Role(
            name=data["name"],
            auths=",".join(map(lambda v: str(v), data["auths"]))
        )
        db.session.add(role)
        db.session.commit()
        oplog = Oplog(
            admin_id=session["admin_id"],
            ip=request.remote_addr,
            reason="添加角色 %s" % data["name"]
        )
        db.session.add(oplog)
        db.session.commit()
        flash("已成功添加角色", "ok")
        return redirect(url_for("admin.role_add"))
    return render_template("admin/role_add.html", form=form)