Example #1
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())
Example #2
0
def get_recipients_by_file_id(session: DBSession, file_id: int) -> List[int]:
    msgs_files = session.get_msgfiles_by_file_id(file_id)
    recipient_ids = []
    for msg_file in msgs_files:
        message = session.get_message_by_id(msg_file.msg_id)
        recipient_ids.append(message.recipient_id)
    return recipient_ids
Example #3
0
def recovery_message(
        session: DBSession, message_id: int, message: RequestRecoveryMessageDto, attribute='is_delete_sender'
) \
        -> DBMessage:
    db_message = session.get_message_by_id(message_id)
    value = getattr(message, 'is_deleted')
    setattr(db_message, attribute, value)
    return db_message
Example #4
0
def patch_message(session: DBSession, message: RequestPatchMessageDto, message_id: int) -> DBMessage:
    db_message = session.get_message_by_id(message_id)

    if message.message:
        value = getattr(message, 'message')
        setattr(db_message, 'message', value)

    return db_message
Example #5
0
def get_message(session: DBSession, message_id: int = None) -> DBMessage:
    db_message = None

    if message_id is not None:
        db_message = session.get_message_by_id(message_id)
    if db_message is None:
        raise DBMessageNotExistsException
    return db_message
Example #6
0
def patch_message(session: DBSession, message: RequestPatchMsgDto, message_id: int) -> DBMessage:
    db_message = session.get_message_by_id(message_id)
    for attr in message.fields:
        if hasattr(message, attr):
            value = getattr(message, attr)
            setattr(db_message, attr, value)

    return db_message
Example #7
0
def patch_message(session: DBSession, message: RequestPatchMessageDto,
                  message_id: int) -> DBMessage:
    db_message = session.get_message_by_id(message_id)

    attr = 'message'
    if hasattr(message, attr):
        setattr(db_message, attr, getattr(message, attr))

    return db_message
def delete_message(session: DBSession, message_id: int, user_id: int) -> None:

    db_message = session.get_message_by_id(message_id)
    if db_message is None:
        raise DBMessageNotExistsException
    if db_message.sender_id != user_id:
        raise DBResourceOwnerException

    db_message.is_delete = True
Example #9
0
def delete_message(session: DBSession, message_id: int) -> DBMessage:
    pass
    db_message = session.get_message_by_id(message_id=message_id)

    if db_message is None:
        raise DBMessageNotExistsException()

    session.delete_model(db_message)

    return db_message
Example #10
0
def get_message(session: DBSession, *, user_id: id,
                message_id: int) -> DBMessage:

    db_message = session.get_message_by_id(user_id=user_id,
                                           message_id=message_id)

    if db_message is None:
        raise DBMessageNotExistsException

    return db_message
Example #11
0
def patch_message(session: DBSession, message: RequestPatchMessageDto, message_id: int) -> DBMessage:

    db_message = session.get_message_by_id(message_id)

    # just 1 field ? subject to refactor
    for attr in message.fields:
        if hasattr(message, attr):
            value = getattr(message, attr)
            setattr(db_message, attr, value)

    return db_message
Example #12
0
def delete_message(session: DBSession, user_id: int,
                   message_id: int) -> DBMessage:

    db_message = session.get_message_by_id(user_id=user_id,
                                           message_id=message_id)

    if db_message is None:
        raise DBMessageNotExistsException

    db_message.is_deleted = True

    return db_message
Example #13
0
def patch_message(session: DBSession, message, message_id: int) -> DBMessage:

    db_message = session.get_message_by_id(message_id)

    if db_message is None:
        raise DBMessageNotExistsException()

    for attr in message.fields:
        if hasattr(message, attr):
            value = getattr(message, attr)
            setattr(db_message, attr, value)

    return db_message
Example #14
0
def patch_message(session: DBSession, message: RequestPatchMessageDto,
                  user_id: int, message_id: int) -> DBMessage:

    db_message = session.get_message_by_id(user_id=user_id,
                                           message_id=message_id)

    if db_message is None:
        raise DBMessageNotExistsException

    for attr in message.fields:
        setattr(db_message, attr, getattr(message, attr))

    return db_message
Example #15
0
def get_message(session: DBSession,
                *,
                user_id: int = None,
                mid: int = None) -> DBMessage:
    db_message = None

    if user_id is not None:
        db_message = session.get_messages_by_user_id(user_id)
    else:
        db_message = session.get_message_by_id(mid)

    if db_message is None:
        raise DBMessageDoesntExistException

    return db_message
def patch_message(session: DBSession,
                  request_dto_message: RequestPatchMessageDto, message_id: int,
                  user_id: int) -> DBMessage:

    db_message = session.get_message_by_id(message_id)
    if db_message is None:
        raise DBMessageNotExistsException
    if db_message.sender_id != user_id:
        raise DBResourceOwnerException

    for attr in request_dto_message.fields:
        value = getattr(request_dto_message, attr)
        setattr(db_message, attr, value)

    return db_message
Example #17
0
def get_message(session: DBSession,
                msg_id: int = None,
                is_read: bool = None) -> DBMessage:
    db_message = None

    if msg_id is not None:
        db_message = session.get_message_by_id(msg_id)

    if db_message is None:
        raise DBMessageNotFoundException

    if db_message.is_deleted is True:
        raise DBMessageDeletedException

    if is_read:
        db_message.is_read = True

    return db_message
Example #18
0
    async def method_delete(self, request: Request, body: dict,
                            session: DBSession, message_id: int, token: dict,
                            *args, **kwargs) -> BaseHTTPResponse:
        eid = token.get('eid')
        try:
            recipient_id = session.get_recipient_id_by_message(message_id)
            sender_id = session.get_message_by_id(message_id).sender_id
            # проверка доступа к удалению отправленного или полученного сообщения через sender_id или recipient_id
            if eid != sender_id and eid != recipient_id:
                raise SanicMessageForbidden("Forbidden")
        except AttributeError:
            raise DBMessageNotExistException("Message not found")
        try:
            message = message_queries.delete_massage(session, message_id)
        except AttributeError:
            raise DBMessageNotExistException('message not found')

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

        return await self.make_response_json(status=204)
Example #19
0
def get_message(session: DBSession, message_id: int) -> DBMessage:
    db_message = session.get_message_by_id(message_id)
    return db_message
Example #20
0
def delete_message(session: DBSession, message_id: int):
    db_message = session.get_message_by_id(message_id)
    db_message.is_delete = True
    return db_message
Example #21
0
def delete_message(session: DBSession, message_id: int, attribute='is_delete_sender') -> DBMessage:
    db_message = session.get_message_by_id(message_id)
    value = True
    setattr(db_message, attribute, value)
    return db_message
Example #22
0
def get_messages(session: DBSession, message_id: int) -> DBMessage:
    return session.get_message_by_id(message_id)
Example #23
0
def delete_message(session: DBSession, msg_id: int) -> DBMessage:
    db_message = session.get_message_by_id(msg_id)

    db_message.is_deleted = True

    return db_message
Example #24
0
def get_sender_deleted_message(session: DBSession, mid: int):
    message = session.get_message_by_id(mid)
    id = None
    if message and message.is_delete_sender == True:
        id = message.sender_id
    return id
Example #25
0
def get_recipient_deleted_message(session: DBSession, mid: int):
    message = session.get_message_by_id(mid)
    id = None
    if message and message.is_delete_recipient == True:
        id = message.recipient_id
    return id