async def method_patch(self, request: Request, body: dict, session: DBSession, uid: int = None, ulogin: str = None, *args, **kwargs) -> BaseHTTPResponse: try: user = user_queries.get_user(session, user_id=uid, login=ulogin) except DBUserNotExistsException: raise SanicUserNotFound('User not found') if body['uid'] != user.id: raise SanicUserConflictException("This is not your user") request_model = RequestPatchUserDto(body) patched_user = user_queries.patch_user(session, request_model, user.id) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponsePatchUserDto(patched_user) return await self.make_response_json(body=response_model.dump(), status=200)
async def method_patch(self, request: Request, body: dict, session: DBSession, uid: int, token: dict, *args, **kwargs) -> BaseHTTPResponse: await self.check_uid_in_token( token, uid, response_error_message='You can only change your own data') request_model = RequestPatchUserDto(body) try: db_user = user_queries.patch_user(session, request_model, uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseUserDto(db_user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_patch(self, request: Request, body: dict, session: DBSession, user_id: int, token: dict, *args, **kwargs) -> BaseHTTPResponse: # проверяем, что пользователь посылает запрос от своего имени if token.get('id') != user_id: return await self.make_response_json(status=403) request_model = RequestPatchUserDto(body) # проверяем, что пользователь есть в базе и не удален try: user = user_queries.patch_user(session, request_model, user_id) except DBUserNotFoundException: raise SanicUserNotFoundException('User not found') except DBUserDeletedException: raise SanicUserDeletedException('User deleted') try: session.commit_session() except (DBIntegrityException, DBDataException) as error: raise SanicDBException(str(error)) response_model = ResponseGetUserDto(user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_patch(self, request: Request, body: dict, session: DBSession, user_id: int, token: dict, *args, **kwargs) -> BaseHTTPResponse: if user_id != token["sub"]: raise Forbidden("user can update only himself") request_model = RequestPatchUserDto(body) try: db_user = update_user(session, request_model, user_id) except DBUserNotExists: raise SanicUserNotFound("User not found") try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseUserDto(db_user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_patch( self, request: Request, body: dict, session: DBSession, uid: int, token: dict, *args, **kwargs ) -> BaseHTTPResponse: if token.get('uid') != uid: return await self.make_response_json(status=403) request_model = RequestPatchUserDto(body) try: user_queries.get_user(session, user_id=uid) user = user_queries.patch_user(session, request_model, uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseUserDto(user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_patch(self, request: Request, body: dict, session: DBSession, uid: int, *args, **kwargs) -> BaseHTTPResponse: if uid != get_id_from_token(request): raise SanicForbidden('You have no rights to edit this user') request_model = RequestPatchUserDto(body) try: user = user_queries.patch_user(session, request_model, uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseUserDto(user) return await self.make_response_json(status=200, body=response_model.dump())