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

        request_model = RequestAuthEmployeeDto(body)

        try:
            db_employee = employee_queries.get_employee(
                session, login=request_model.login)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

        try:
            check_hash(request_model.password, db_employee.password)
        except CheckPasswordHashException:
            raise SanicPasswordHashException('Wrong password')

        payload = {
            'eid': db_employee.id,
        }

        token = create_token(payload)
        response = AuthResponseObject(token)

        response_model = ResponseAuthEmployeeDto(response)

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

        if 'id' in request.args:
            eid = int(request.args['id'][0])
        elif 'login' in request.args:
            eid = employee_queries.get_employee_id_by_login(
                session, request.args['login'][0])
        else:
            raise SanicBadRequest('Bad request')

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

        try:
            db_employee = employee_queries.get_employee(session,
                                                        employee_id=eid)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('User not found')

        response_model = ResponseEmployeeDto(db_employee)

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

        try:
            employee = employee_queries.delete_employee(session, eid)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

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

        return await self.make_response_json(status=204)
예제 #4
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())
예제 #5
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, eid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        try:
            employee = employee_queries.get_employee(session,
                                                     employee_id=int(
                                                         token.get('eid')))
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

        response_employee = ResponseEmployeeDto(employee)

        return await self.make_response_json(status=200,
                                             body=response_employee.dump())
예제 #6
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())
예제 #7
0
def create_message(session: DBSession, message: RequestCreateMessageDto, sid: int) -> DBMessage:

    rid = get_employee_id_by_login(session, message.recipient)

    try:
        session.get_employee_by_id(rid)
    except DBEmployeeNotExistsException:
        raise SanicEmployeeNotFound('Employee not found')

    new_message = DBMessage(
        sender_id=sid,
        recipient_id=rid,
        message=message.message,
    )

    session.add_model(new_message)

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

        request_model = RequestPatchEmployeeDto(body)

        try:
            employee = employee_queries.patch_employee(session, request_model, eid)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

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

        response_model = ResponseEmployeeDto(employee)

        return await self.make_response_json(status=200, body=response_model.dump())
예제 #9
0
    async def _method(self, request: Request, body: dict, *args,
                      **kwargs) -> BaseHTTPResponse:

        database = self.context.database
        session = database.make_session()

        if self.auth_required:
            try:
                DBSession(session).get_employee_by_id(
                    kwargs.get('token').get('eid'))
            except DBEmployeeNotExistsException:
                raise SanicEmployeeNotFound(
                    'Sorry, we could not find your employee.'
                    'Obviously, your account has been removed')

        try:
            return await super()._method(request, body, session, *args,
                                         **kwargs)

        except SanicException as e:
            return await self.make_response_json(status=e.status_code,
                                                 message=str(e))