예제 #1
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, message_id: int, *args,
                           **kwargs) -> BaseHTTPResponse:

        if get_id_from_token(request) != message_queries.get_message_author(
                session, message_id):
            raise SanicForbidden('You have no rights to edit this message')

        request_model = RequestPatchMessageDto(body)

        try:
            message = message_queries.patch_message(session, request_model,
                                                    message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        response_model = ResponseMessageDto(message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
예제 #2
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, msg_id: int, token: dict, *args,
                           **kwargs) -> BaseHTTPResponse:

        # проверяем, что сообщение есть в БД и не удалено
        try:
            db_message = message_queries.get_message(session, msg_id)
        except DBMessageNotFoundException:
            raise SanicMessageNotFoundException('Message not found')
        except DBMessageDeletedException:
            raise SanicMessageDeletedException('Message deleted')

        # проверка на то, что пользователь редактирует сообщение, отправленное от его имени
        if token['id'] != db_message.sender_id:
            return await self.make_response_json(status=403)

        # проверяем, что пользователь не удален
        try:
            user_queries.get_user(session=session, user_id=token['id'])
        except DBUserDeletedException:
            raise SanicUserDeletedException('User deleted')

        request_model = RequestPatchMessageDto(body)

        message = message_queries.patch_message(session, request_model, msg_id)

        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as error:
            raise SanicDBException(str(error))

        response_model = ResponseGetMessageDto(message)

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
예제 #3
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, message_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:

        request_model = RequestPatchMessageDto(body)

        uid = token.get('uid')

        try:
            message = message_queries.patch_message(session,
                                                    request_model,
                                                    user_id=uid,
                                                    message_id=message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        response_model = ResponseMessageDto(message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
예제 #4
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, message_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:
        """
        Редактирование сообщения по его id
        """

        if token.get('user_id') != message_queries.check_user_by_message_id(
                session, message_id=message_id).sender_id:
            return await self.make_response_json(status=403)

        request_model = RequestPatchMessageDto(body)

        try:
            db_message = message_queries.patch_message(session,
                                                       request_model,
                                                       message_id=message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message 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)