示例#1
0
 def obtain_roles(self, params: RoleRequestParam):
     """
     获取所有角色
     :return: 角色列表
     """
     roles = Role.objects.all(**params.obtain_dict())
     return utils.objs_to_dicts(roles,
                                convert_func=lambda obj: RoleVO(role=obj))
示例#2
0
    def add_role(self, params: RoleRequestParam):
        """
        添加角色
        :param params:
        :return:
        """
        params.validate()
        if Role.objects.filter(name=params.name).exists():
            raise ParamError(CODE_10017)

        now = timezone.now()
        create_dict: dict = params.obtain_dict()
        create_dict['create_time'] = now
        create_dict['update_time'] = now
        role = Role.objects.create(**create_dict)
        role.save()
        # 添加菜单关联关系
        for menu_id in params.menus:
            RoleMenu.objects.create(role=role,
                                    menu_id=menu_id,
                                    create_time=now)
示例#3
0
def ajax_update_role(request: HttpRequest):
    """更新角色"""
    resp = Resp()
    try:
        result = BdRoleService().update_role(params=RoleRequestParam(request))
        resp.success(result)
    except BaseError as e:
        logger.error('update role fail: %s', e)
        resp.fail_error(e)
    except Exception as e:
        logger.error('update role error: %s', e)
        resp.fail(CODE_99999)
    return JsonResponse(utils.obj_to_dict(resp), encoder=CustomerEncoder)
示例#4
0
 def obtain_role(self, params: RoleRequestParam):
     """
     获取单个角色
     :param params:
     :return:
     """
     query_dict: dict = params.obtain_dict()
     if params.id:
         query_dict['id'] = params.id
     role = Role.objects.filter(**query_dict).first()
     if not role:
         raise ParamError(CODE_10011)
     role_vo = RoleVO(role=role)
     menus = Menu.objects.filter(del_status=False, rolemenu__role=role)
     role_vo.menus = utils.objs_to_dicts(menus,
                                         lambda obj: MenuVO(menu=obj))
     return utils.obj_to_dict(role_vo)
示例#5
0
    def update_role(self, params: RoleRequestParam):
        """
        更新角色
        :param params:
        :return:
        """
        update_dict: dict = params.obtain_dict()

        if not params.id or not update_dict:
            raise ParamError(CODE_10001)

        if Role.objects.filter(name=params.name).exclude(
                id=params.id).exists():
            raise ParamError(CODE_10017)

        now = timezone.now()
        if params.menus:
            RoleMenu.objects.filter(role_id=params.id).delete()
            for menu_id in params.menus:
                RoleMenu.objects.create(role_id=params.id,
                                        menu_id=menu_id,
                                        create_time=now)
        update_dict['update_time'] = now
        return Role.objects.filter(id=params.id).update(**update_dict)