async def method_post(self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: recipient = message_queries.get_user(session=session, login=request_model.recipient) except DBUserNotExistsException: raise SanicUserNotFound('Recipient not found') uid = token.get('uid') try: sender = message_queries.get_user(session=session, user_id=uid) except DBUserNotExistsException: raise SanicUserNotFound('Sender not found') converse_id = {'sender_id': sender.id, 'recipient_id': recipient.id} db_message = message_queries.create_message(session, request_model, converse_id) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: return await self.make_response_json(status=500, message=str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
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, 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_post(self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs) -> BaseHTTPResponse: """ Создание сообщения """ user_id = token.get('user_id') request_model = RequestCreateMessageDto(body) try: db_message = message_queries.create_message(session, request_model, user_id=user_id) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestAuthUserDto(body) try: db_user = get_user_by_login(session, login=request_model.login) except DBUserNotExists: raise SanicUserNotFound("User not found") try: check_hash(request_model.password, db_user.password) except CheckPasswordHashException: raise SanicPasswordHashException("Wrong password") payload = { "sub": db_user.id, } token = create_token(payload) response = AuthResponseObject(token) response_model = ResponseAuthUserDto(response) return await self.make_response_json( body=response_model.dump(), status=200, )
async def method_get(self, request: Request, body: dict, session: DBSession, uid: int, *args, **kwargs) -> BaseHTTPResponse: try: message = user_queries.get_user(session, uid=uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') # change response_model = ResponseUserDto(message) return await self.make_response_json(status=200, body=response_model.dump())
async def method_post( self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs ) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: message_queries.create_message(session=session, message=request_model, sender=token.get('id_auth')) session.commit_session() except DBUserNotExistsException: raise SanicUserNotFound('User not found') except(DBDataError, DBIntegrityError) as error: raise SanicDBException(str(error)) return await self.make_response_json(body={}, status=201)
async def method_get( 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) try: user = user_queries.get_user(session, user_id=uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') response_model = ResponseUserDto(user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_delete(self, request: Request, body: dict, session: DBSession, uid: int, *args, **kwargs) -> BaseHTTPResponse: try: user = user_queries.delete_user(session, uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) return await self.make_response_json(status=204)
async def method_get(self, request: Request, body: dict, session: DBSession, user_id: int, token: dict, *args, **kwargs) -> BaseHTTPResponse: if user_id != token["sub"]: raise Forbidden("user can get only himself") try: db_user = get_user_by_id(session, user_id=user_id) except DBUserNotExists: raise SanicUserNotFound("User not found") response_model = ResponseUserDto(db_user) return await self.make_response_json(body=response_model.dump(), status=200)
async def method_delete(self, request: Request, body: dict, session: DBSession, user_id: int, token: dict, *args, **kwargs) -> BaseHTTPResponse: if user_id != token["sub"]: raise Forbidden("user can delete only himself") try: _ = delete_user(session, user_id) except DBUserNotExists: raise SanicUserNotFound("User not found") try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) return await self.make_response_json(status=204)
async def method_get(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 get your own data') try: db_user = user_queries.get_user(session, user_id=uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') response_model = ResponseUserDto(db_user) return await self.make_response_json(status=200, body=response_model.dump())
async def method_post( self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs, ) -> BaseHTTPResponse: """ Send message to recipient by login :param request: :param body: :param session: :param token: :param args: :param kwargs: :return: """ request_model = RequestCreateMessageDto(body) try: recipient = get_user_by_login(session, login=request_model.recipient) except DBUserNotExists as e: raise SanicUserNotFound( f"Recipient {request_model.recipient} not found") from e db_message = create_message( session, message=request_model.message, recipient_id=recipient.id, sender_id=token["sub"], ) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(status=200, body=response_model.dump())
async def method_post(self, request: Request, body: dict, session, token: dict, *args, **kwargs) -> BaseHTTPResponse: request = RequestCreateMessageDto(body) try: db_message = message.create_message(session, request, token.get("uid"), request.recipient) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response = ResponseMessageDto(db_message) return await self.make_response_json(response.dump(), status=201)
async def method_get(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") response_model = ResponseGetUserDto(user) return await self.make_response_json(body=response_model.dump(), status=200)
async def method_delete(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 delete your own data') try: user = user_queries.delete_user(session, user_id=uid) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) return await self.make_response_json(status=204)
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())
async def method_post(self, request: Request, body: dict, session: DBSession, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: message = message_queries.create_message(session, request_model, body['uid']) except DBUserNotExistsException: raise SanicUserNotFound('Recipient not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) try: if hasattr(request_model, 'file_ids'): for file_id in request_model.file_ids: file_queries.create_msg_file_relation( session=session, user_id=body['uid'], msg_id=message.id, file_id=file_id ) except DBFileNotExistsException: raise SanicFileNotFound('File not found') except DBResourceOwnerException: raise SanicUserConflictException("This is not your file") try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) message.file_ids = [] if hasattr(request_model, 'file_ids'): message.file_ids = request_model.file_ids response_model = ResponseCreateMessageDto(message) return await self.make_response_json(body=response_model.dump(), status=201)