Exemplo n.º 1
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)
Exemplo n.º 2
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())
Exemplo n.º 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())
Exemplo n.º 4
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, message_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:
        eid = token.get('eid')
        # проверка доступа к патчу отправленного сообщения через sender_id
        try:
            sender_id = session.get_message_by_id(message_id).sender_id
            if eid != sender_id:
                raise SanicMessageForbidden("Forbidden")
        except AttributeError:
            raise DBMessageNotExistException("Message not found")
        request_model = RequestPatchMsgDto(body)

        try:
            message = message_queries.patch_message(session, request_model,
                                                    message_id)
        except AttributeError:
            raise DBMessageNotExistException('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())
Exemplo n.º 5
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)
Exemplo n.º 6
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, message_id: int, *args,
                           **kwargs) -> BaseHTTPResponse:

        request_model = RequestPatchMessageDto(body)

        try:
            message = message_queries.patch_message(session, request_model,
                                                    message_id, body['uid'])
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')
        except DBResourceOwnerException:
            raise SanicUserConflictException("This is not your message")

        try:
            if hasattr(request_model, 'file_ids'):
                file_ids = file_queries.get_file_ids_by_msd_id(
                    session, message_id)
                file_ids_merged = list(set(file_ids + request_model.file_ids))
                for file_id in file_ids_merged:
                    if file_id not in file_ids:
                        file_queries.create_msg_file_relation(
                            session=session,
                            user_id=body['uid'],
                            msg_id=message_id,
                            file_id=file_id)
                    elif file_id not in request_model.file_ids:
                        file_queries.delete_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 DBMsgFileNotExistsException:
            raise SanicFileNotFound('MsgFile 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 = file_queries.get_file_ids_by_msd_id(
            session, message.id)
        response_model = ResponsePatchMessageDto(message)

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
Exemplo n.º 7
0
    async def method_patch(
            self, request: Request, body: dict, session, mid: int, token: dict, *args, **kwargs
    ) -> BaseHTTPResponse:

        request_model = RequestPatchMessageDto(body)

        try:
            db_message = message_queries.patch_message(session, request_model, mid=mid, uid=token.get("uid"))
        except (DBMessagesNoAccess, DBMessageNotExistsException):
            raise SanicResponseForbiddenException('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(status=200, body=response_model.dump())
Exemplo n.º 8
0
    async def method_patch(self, request: Request, body: dict, session: DBSession, msg_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:

        request_model = RequestPatchMessageDto(body)

        try:
            db_message = message_queries.get_db_message(session, msg_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        await self.check_sender_id_by_token(token, db_message,
                                            response_error_message='You can only change your own data')

        db_message = message_queries.patch_message(db_message, request_model.message)

        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=200)
Exemplo n.º 9
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, token: dict, mid: int, *args,
                           **kwargs) -> BaseHTTPResponse:
        try:
            db_message = message_queries.get_message(session=session, mid=mid)
        except DBMessageDoesntExistException:
            raise SanicMessageNotFound('message not found')

        if db_message.sender_id != token.get('id_auth'):
            return await self.make_response_json(status=403)

        request_model = RequestPatchMessageDto(body)

        message = message_queries.patch_message(db_message, request_model)

        try:
            session.commit_session()
        except (DBDataError, DBIntegrityError) as error:
            raise SanicDBException(str(error))

        response_model = ResponsePatchMessageDto(message)

        return await self.make_response_json(body=response_model.dump())