示例#1
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        if 'recipient_login' in request.args:
            login = request.args['recipient_login'][0]
            db_message = message_queries.get_messages_by_recipient_login(
                session, token.get('eid'), login)
        elif 'recipient_id' in request.args:
            rid = request.args['recipient_id'][0]
            db_message = message_queries.get_messages_by_recipient_id(
                session, token.get('eid'), rid)
        elif 'sender_login' in request.args:
            login = request.args['sender_login'][0]
            db_message = message_queries.get_messages_by_sender_login(
                session, token.get('eid'), login)
        elif 'sender_id' in request.args:
            sid = request.args['sender_id'][0]
            db_message = message_queries.get_messages_by_sender_id(
                session, token.get('eid'), sid)
        else:
            raise SanicBadRequest('Bad request')

        try:
            response_model = ResponseMessageDto(db_message, many=True)
        except ApiResponseValidationException:
            raise SanicMessagesNotFound('Messages not found')

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
示例#2
0
    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)
示例#3
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)
示例#4
0
    async def method_get(
        self,
        request: Request,
        body: dict,
        session: DBSession,
        message_id: int,
        token: dict,
        *args,
        **kwargs,
    ) -> BaseHTTPResponse:
        """
        Get message by id
        :param request:
        :param body:
        :param session:
        :param message_id:
        :param token:
        :param args:
        :param kwargs:
        :return:
        """
        try:
            db_message = get_message_by_id(session, message_id=message_id)
        except DBMessageNotExists as e:
            raise NotFound(f"Message {message_id} not found") from e

        if token["sub"] not in (db_message.sender_id, db_message.recipient_id):
            raise Forbidden("user is not recipient or sender")

        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#5
0
    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)
示例#6
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())
示例#7
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())
示例#8
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())
示例#9
0
    async def method_get(
            self, request: Request, body: dict, session: DBSession, token, *args, **kwargs
    ) -> BaseHTTPResponse:
        uid = token.get('uid')
        db_messages = message_queries.get_inbox_messages(session, uid)
        response_model = ResponseMessageDto(db_messages, many=True)

        return await self.make_response_json(status=200, body=response_model.dump())
示例#10
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        db_message = message_queries.get_messages(session, token.get('eid'))
        response_model = ResponseMessageDto(db_message, many=True)

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
示例#11
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, *args,
                         **kwargs) -> BaseHTTPResponse:

        uid = read_token(request.headers['authorization'])['uid']
        db_message = message_queries.get_messages(session, uid)
        response_model = ResponseMessageDto(db_message, many=True)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#12
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        """#выводит все отправленные сообщения"""
        eid = token.get('eid')

        db_message = message_queries.get_messages_all(session, eid)

        response_model = ResponseMessageDto(db_message, many=True)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#13
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        """
        Просмотр всех сообщений
        """

        user_id = token.get('user_id')
        db_messages = message_queries.get_messages(session, user_id=user_id)
        response_model = ResponseMessageDto(db_messages, many=True)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
示例#14
0
    async def method_get(self, request: Request, body: dict, session: DBSession, msg_id: int, token: dict,
                         *args, **kwargs) -> BaseHTTPResponse:

        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 get your own data',
                                            include_recipient_id=True)

        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(body=response_model.dump(), status=200)
示例#15
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        print(token.get("eid"))
        try:
            db_message = message_queries.get_messages(session,
                                                      recipient_id=int(
                                                          token.get("eid")))
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound("Employee not found")

        response_model = ResponseMessageDto(db_message, many=True)

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

        uid = token.get('uid')

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

        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#17
0
    async def method_post(self, request: Request, body: dict, session, token: dict, *args, **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateMessageDto(body)

        try:
            message = message_queries.create_message(session, request_model, token.get("eid"))
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound("Employee 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())
示例#18
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, message_id: int, *args,
                         **kwargs) -> BaseHTTPResponse:

        try:
            message = message_queries.get_message(session, message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')  # change

        if not (message.recipient_id == get_id_from_token(request)) and \
           not (message.sender_id == get_id_from_token(request)):
            raise SanicForbidden('You have no rights to read this message')

        response_model = ResponseMessageDto(message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#19
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, message_id, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        eid = token.get('eid')

        try:
            recipient_id = session.get_recipient_id_by_message(message_id)
        except AttributeError:
            raise DBMessageNotExistException("Message not found")

        if eid != recipient_id:
            raise SanicMessageForbidden("Not allowed")
        try:
            db_message = message_queries.get_message(session, message_id)
        except SanicMessageReadNotAllowedException as e:
            raise e
        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(body=response_model.dump())
示例#20
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, message_id: int, token: dict,
                         *args, **kwargs) -> BaseHTTPResponse:

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

        employee_id = token["eid"]
        if message.sender_id != employee_id and message.recipient_id != employee_id:
            return await self.make_response_json(status=403,
                                                 message="Forbidden")

        response_message = ResponseMessageDto(message)

        return await self.make_response_json(status=200,
                                             body=response_message.dump())
示例#21
0
    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())
示例#22
0
    async def method_patch(
        self,
        request: Request,
        body: dict,
        session: DBSession,
        message_id: int,
        token: dict,
        *args,
        **kwargs,
    ) -> BaseHTTPResponse:
        """
        Update message by id
        :param request:
        :param body:
        :param session:
        :param message_id:
        :param token:
        :param args:
        :param kwargs:
        :return:
        """
        request_model = RequestUpdateMessageDto(body)

        try:
            db_message = update_message(session,
                                        message=request_model,
                                        message_id=message_id)
        except DBMessageNotExists as e:
            raise NotFound(f"Message {message_id} not found") from e

        if token["sub"] != db_message.sender_id:
            raise Forbidden("user is not sender")

        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())
示例#23
0
    async def method_get(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)

        try:
            db_message = message_queries.get_message(session,
                                                     message_id=message_id)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')
        response_model = ResponseMessageDto(db_message)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
示例#24
0
    async def method_post(self, request: Request, body: dict, session, token,
                          *args, **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateMessageDto(body)
        sender_id = token.get('uid')
        try:
            db_message = message_queries.create_message(
                session, request_model, sender_id)
        except DBUserNotExistsException:
            raise SanicUserConflictException('Recipient 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)
示例#25
0
    async def method_get(self,
                         request: Request,
                         body: dict,
                         session: DBSession,
                         uid: int = None,
                         *args,
                         **kwargs) -> BaseHTTPResponse:

        rid = kwargs.get('rid')

        try:
            db_message = message_queries.get_dialog(session=session,
                                                    sender_id=uid,
                                                    recipient_id=rid)
        except DBMessageNotExistsException:
            raise SanicDialogNotFound('Dialog with current user not found')

        response_model = ResponseMessageDto(db_message, many=True)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#26
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, mid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        rights_holders = [
            messages_queries.get_sender(session, mid),
            messages_queries.get_recipient(session, mid)
        ]

        if rights_holders is None or token.get('uid') not in rights_holders:
            return await self.make_response_json(status=403)

        try:
            message = messages_queries.get_message(session, mid)
        except DBMessageNotExistsException:
            raise SanicMessageNotFound('Message not found')

        response_model = ResponseMessageDto(message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
示例#27
0
    async def method_post(self, request: Request, body: dict, session, *args, **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateMessageDto(body)

        uid = read_token(request.headers['authorization'])['uid']

        try:
            db_message = message_queries.create_message(session, request_model, uid)
        except DBUserNotExistsException as e:
            return await self.make_response_json(status=400, message="No such recipient")
        except DBMessageCreateException as e:
            raise SanicDBException(str(e))

        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)
示例#28
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)