def add_user(self, params: UserRequestParam): """ 添加用户 :param params: :return: """ params.validate() self.__validate_unique(params) now = timezone.now() create_dict: dict = params.obtain_dict() create_dict['create_time'] = now create_dict['update_time'] = now user = User.objects.create(**create_dict) user.save() # 添加角色关联关系 for role_id in params.roles: UserRole.objects.create(user=user, role_id=role_id, create_time=now) return utils.obj_to_dict(user, lambda obj: UserVO(user=obj))
def ajax_update_user(request: HttpRequest): """更新用户""" resp = Resp() try: result = BdUserService().update_user(params=UserRequestParam(request)) resp.success(result) except BaseError as e: logger.error('update user fail: %s', e) resp.fail_error(e) except Exception as e: logger.error('update user error: %s', e) resp.fail(CODE_99999) return JsonResponse(utils.obj_to_dict(resp), encoder=CustomerEncoder)
def obtain_user(self, params: UserRequestParam): """ 获取单个用户 :param params: :return: """ query_dict: dict = params.obtain_dict() if params.id: query_dict['id'] = params.id user = User.objects.filter(**query_dict).first() if not user: raise ParamError(CODE_10010) user_vo = UserVO(user=user) roles = Role.objects.filter(del_status=False, userrole__user=user) user_vo.roles = utils.objs_to_dicts(roles, lambda obj: RoleVO(role=obj)) return utils.obj_to_dict(user_vo)
def update_user(self, params: UserRequestParam): """ 更新用户 :param params: :return: """ update_dict: dict = params.obtain_dict() if not params.id or not update_dict: raise ParamError(CODE_10001) self.__validate_unique(params) now = timezone.now() if params.roles: UserRole.objects.filter(user_id=params.id).delete() for role_id in params.roles: UserRole.objects.create(user_id=params.id, role_id=role_id, create_time=now).save() update_dict['update_time'] = now return User.objects.filter(id=params.id).update(**update_dict)
def to_profile(request: HttpRequest): """个人信息页面""" user = BdUserService().obtain_user(params=UserRequestParam(request)) return render(request, 'backend/profile.html', context={'user': user})