예제 #1
0
def login():
    json = request.json or {}
    data = get_filter({'username', 'password'}, json, False)
    user = User.query.filter_by(name=data['username']).first()
    if not user or not user.verify_password(data['password']):
        request_error('用户或密码不对', 404)
    return success_result(user=user.serializer())
예제 #2
0
def update(pk: int):
    action = request.args.get('action', None)
    if action and action in update_action:
        current_app.logger.info('%r 执行了 %s', g.user, action)
        # current_app.logger.info('<menu> %r 执行了 %s', g.user, action)
        return getattr(sys.modules[__name__], action)(pk)
    else:
        request_error('请选择有效的action')
예제 #3
0
def delete(pk: int):
    menu = Menu.query.get_or_404(pk)
    try:
        db.session.delete(menu)
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')

    current_app.logger.info('菜单 %r 被删除了', menu)
    return success_result()
예제 #4
0
def delete(pk: int):
    role = Role.query.get_or_404(pk)
    try:
        db.session.delete(role)
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')

    current_app.logger.info('角色 %r 被删除了', role)
    return success_result()
예제 #5
0
def create():
    data = get_filter({'name'}, request.json, False)
    role = Role(**data)
    try:
        db.session.add(role)
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')
    current_app.logger.info('角色创建 %r', role)
    return success_result()
예제 #6
0
def create():
    data = get_filter({'name', 'url'}, request.json, False)
    data.update(get_filter({'parent_id'}, request.json))
    menu = Menu(**data)
    try:
        db.session.add(menu)
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')
    current_app.logger.info('菜单创建 %r', menu)
    return success_result()
예제 #7
0
def update_basic(pk: int):
    data = get_filter({'name', 'url', 'parent_id'}, request.json)
    try:
        num = Menu.query.filter(Menu.id == pk).update(data)
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')

    current_app.logger.info('菜单[id]:%d 更新了 %d', pk, num)
    if num == 0:
        return success_result(msg='虽然成功了,但是什么事都没有发生')
    return success_result()
예제 #8
0
def bind_role(pk: int):
    menu = Menu.query.get_or_404(pk)
    roles = request.json.get('roles', [])
    if isinstance(roles, list):
        role = Role.query.filter(Role.id.in_(roles))
        menu.roles.extend(role)
    else:
        role = Role.query.get_or_404(roles)
        menu.roles.append(role)
    try:
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')
    return success_result()
예제 #9
0
def bind_user(pk: int):
    role = Role.query.get_or_404(pk)
    users = request.json.get('users', [])
    if isinstance(users, list):
        user = User.query.filter(User.id.in_(users))
        role.users.extend(user)
    else:
        user = User.query.get_or_404(users)
        role.users.append(user)
    try:
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')
    return success_result()
예제 #10
0
def bind_menu(pk: int):
    role = Role.query.get_or_404(pk)
    menus = request.json.get('menus', [])
    if isinstance(menus, list):
        menu = Menu.query.filter(Menu.id.in_(menus))
        role.menus.extend(menu)
    else:
        menu = Menu.query.get_or_404(menus)
        role.menus.append(menu)
    try:
        db.session.commit()
    except DatabaseError as e:
        current_app.logger.exception(e)
        request_error('database error')
    return success_result()
예제 #11
0
def register():
    needed_args = get_filter({'user_open_id', 'username', 'password', 'confirm_password'}, request.get_json(force=True), False)
    password = needed_args['password']
    if len(password) < 8:
        request_error(f'密码长度不够{8}位', 400)
    if password != needed_args.get('confirm_password'):
        request_error('两次密码输入不一样', 400)
    if User.query.filter_by(name=needed_args['username']).first():
        request_error('用户名已存在', 400)
    manager = User(user_open_id=needed_args['user_open_id'], name=needed_args['username'])
    manager.set_hash_password(password)
    try:
        db.session.add(manager)
        db.session.commit()
    except IntegrityError as e:
        current_app.logger.error("%s,%r", repr(manager), e.orig)
        request_error(str(e.orig), 400)
    except DatabaseError as e:
        current_app.logger.exception(manager)
        request_error(str(e.orig), 500)
    else:
        current_app.logger.info(repr(manager) + '注册')
        return success_result()
예제 #12
0
def change_password():
    needed_args = get_filter({'old_password', 'password', 'confirm_password', }, request.get_json(force=True), False)
    user = g.user
    if not user.verify_password(needed_args['old_password']):
        request_error(f'旧密码不正确', 400)
    if needed_args.get('password') != needed_args.get('confirm_password'):
        request_error('两次密码输入不一样', 400)
    user.set_hash_password(needed_args.get('password'))
    try:
        db.session.commit()
    except IntegrityError as e:
        current_app.logger.error("%s,%r", repr(user), e.orig)
        request_error(str(e.orig), 400)
    except DatabaseError as e:
        current_app.logger.exception(user)
        request_error(str(e.orig), 500)
    else:
        current_app.logger.info(repr(user) + '更改密码')
        return success_result(user=user.login())