Ejemplo n.º 1
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateUserDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
        except GeneratePasswordHashException as e:
            raise SanicPasswordHashException(str(e))

        try:
            db_user = user_queries.create_user(session, request_model,
                                               hashed_password)
        except DBUserExistsException:
            raise SanicUserConflictException('Login is busy')

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

        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
Ejemplo n.º 2
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, uid: int, token: dict, *args,
                           **kwargs) -> BaseHTTPResponse:

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

        request_model = RequestPatchUserDto(body)

        try:
            db_user = user_queries.patch_user(session, request_model, uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')

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

        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
Ejemplo n.º 3
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, *args,
                         **kwargs) -> BaseHTTPResponse:

        db_user = user_queries.get_users(session)
        response_model = ResponseUserDto(db_user, many=True)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
Ejemplo n.º 4
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, uid: int, *args,
                         **kwargs) -> BaseHTTPResponse:

        try:
            message = user_queries.get_user(session, uid=uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')  # change

        response_model = ResponseUserDto(message)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
Ejemplo n.º 5
0
    async def method_get(
            self, request: Request, body: dict, session: DBSession, uid: int, token: dict, *args, **kwargs
    ) -> BaseHTTPResponse:

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

        try:
            user = user_queries.get_user(session, user_id=uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')

        response_model = ResponseUserDto(user)

        return await self.make_response_json(status=200, body=response_model.dump())
Ejemplo n.º 6
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateUserDto(body)

        db_user = user_queries.create_user(session, request_model)
        try:
            session.commit_session()
        except DBIntegrityException:
            return await self.make_response_json(
                body={"Error message": "such a login exists"}, status=409)
        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
Ejemplo n.º 7
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, user_id: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:
        if user_id != token["sub"]:
            raise Forbidden("user can get only himself")

        try:
            db_user = get_user_by_id(session, user_id=user_id)
        except DBUserNotExists:
            raise SanicUserNotFound("User not found")

        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
Ejemplo n.º 8
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, uid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        await self.check_uid_in_token(
            token,
            uid,
            response_error_message='You can only get your own data')

        try:
            db_user = user_queries.get_user(session, user_id=uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')

        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())
Ejemplo n.º 9
0
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, user_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:
        if user_id != token["sub"]:
            raise Forbidden("user can update only himself")

        request_model = RequestPatchUserDto(body)

        try:
            db_user = update_user(session, request_model, user_id)
        except DBUserNotExists:
            raise SanicUserNotFound("User not found")

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

        response_model = ResponseUserDto(db_user)

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

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

        request_model = RequestPatchUserDto(body)

        try:
            user_queries.get_user(session, user_id=uid)
            user = user_queries.patch_user(session, request_model, uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')

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

        response_model = ResponseUserDto(user)

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

        if uid != get_id_from_token(request):
            raise SanicForbidden('You have no rights to edit this user')

        request_model = RequestPatchUserDto(body)

        try:
            user = user_queries.patch_user(session, request_model, uid)
        except DBUserNotExistsException:
            raise SanicUserNotFound('User not found')

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

        response_model = ResponseUserDto(user)

        return await self.make_response_json(status=200,
                                             body=response_model.dump())