Example #1
0
    def execute(
        self,
        nickname: str,
        email: str,
        password1: str,
        password2: str,
    ) -> Union[User, NoReturn]:
        if password1 != password2:
            raise CustomException('password1, 2 does not match', code=400)

        if self._is_exist(nickname=nickname, email=email):
            raise CustomException('already exists', 400)

        password = bcrypt.hashpw(
            password=password1.encode('utf-8'),
            salt=bcrypt.gensalt(),
        )

        user = User(nickname=nickname, email=email, password=password)

        try:
            session.add(user)
            session.commit()
        except IntegrityError as e:
            session.rollback()
            raise CustomException(str(e), code=500)

        return user
Example #2
0
 async def decorator(*args, **kwargs):
     try:
         session.begin(subtransactions=True)
         result = await function(*args, **kwargs)
         session.commit()
     except Exception as e:
         session.rollback()
         raise e
     return result
    def execute(self, user_id: int, caption: str) -> Union[Post, NoReturn]:
        post = Post(user_id=user_id, caption=caption)

        try:
            session.add(post)
            session.commit()
        except IntegrityError as e:
            session.rollback()
            raise CustomException(str(e), code=500)

        return post
Example #4
0
    async def dispatch(
        self, request: Request, call_next: RequestResponseEndpoint,
    ):
        request_id = str(uuid4())
        token = session_context.set(request_id)

        try:
            response = await call_next(request)
        except Exception as e:
            session.rollback()
            raise e
        finally:
            session.remove()
            session_context.reset(token)

        return response
Example #5
0
 async def decorator(*args, **kwargs):
     try:
         if self.propagation == Propagation.REQUIRED:
             result = await self.run_required(
                 function=function, args=args, kwargs=kwargs,
             )
         elif self.propagation == Propagation.REQUIRES_NEW:
             result = await self.run_requires_new(
                 function=function, args=args, kwargs=kwargs,
             )
         else:
             result = await self.run_requires_new(
                 function=function, args=args, kwargs=kwargs,
             )
     except Exception as e:
         session.rollback()
         raise e
     return result
Example #6
0
 def create(self, **args):
     args['created'] = datetime.datetime.utcnow()
     error_message = None
     try:
         new_model = self.model(**args)
         session.add(new_model)
         session.commit()
         self.model = new_model
     except IntegrityError as e:
         print(str(e))
         error_message = 'Faulty or a duplicate record'
     except Exception as e:
         print(str(e))
         error_message = str(e)
     finally:
         if error_message:
             session.rollback()
             raise Exception(error_message)
     return self.model
Example #7
0
 def update(self, **args):
     error_message = None
     try:
         model = self.model
         for key, value in args.items():
             setattr(property, key, value)
         session.merge(model)
         session.commit()
         self.model = model
     except IntegrityError as e:
         print(str(e))
         error_message = 'Faulty or a duplicate record'
     except Exception as e:
         print(str(e))
         error_message = str(e)
     finally:
         if error_message:
             session.rollback()
             raise Exception(error_message)
     return self.model
Example #8
0
 def create(self, **args):
     args['created'] = datetime.datetime.utcnow()
     error_message = None
     try:
         # Creating user object
         property = PropertyModel(**args)
         # Saving to DB
         session.add(property)
         session.commit()
         self.property = property
     except IntegrityError as e:
         print(str(e))
         error_message = 'Faulty or a duplicate record'
     except Exception as e:
         print(str(e))
         error_message = str(e)
     finally:
         if error_message:
             session.rollback()
             raise Exception(error_message)
     return self.property
Example #9
0
 def update(self, **args):
     if hasattr(self, 'property') is False:
         raise Exception("Property was not loaded")
     error_message = None
     try:
         property = self.property
         for key, value in args.items():
             setattr(property, key, value)
         # Saving to DB
         session.merge(property)
         session.commit()
         self.property = property
     except IntegrityError as e:
         print(str(e))
         error_message = 'Faulty or a duplicate record'
     except Exception as e:
         print(str(e))
         error_message = str(e)
     finally:
         if error_message:
             session.rollback()
             raise Exception(error_message)
     return self.property
Example #10
0
 def create(self, **args):
     if 'type' not in args:
         args['type'] = 'default'
     args['created'] = datetime.datetime.utcnow()
     args['confirmation_token'] = random_alnum_string(32)
     error_message = None
     try:
         # Creating user object
         user = UserModel(**args)
         # Saving to DB
         session.add(user)
         session.commit()
         self.user = user
     except IntegrityError as e:
         print(str(e))
         error_message = 'Faulty or a duplicate record'
     except Exception as e:
         print(str(e))
         error_message = str(e)
     finally:
         if error_message:
             session.rollback()
             raise Exception(error_message)
     return self.user
    def execute(
        self,
        user_id: int,
        post_id: int,
        caption: str,
    ) -> Union[Post, NoReturn]:
        post = session.query(Post).filter(
            Post.id == post_id,
            Post.user_id == user_id,
        ).first()

        if not post:
            raise CustomException('post not found', code=404)

        post.caption = caption

        try:
            session.add(post)
            session.commit()
        except IntegrityError as e:
            session.rollback()
            raise CustomException(str(e), code=500)

        return post
Example #12
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     try:
         session.commit()
     except Exception:
         session.rollback()
         raise CustomException(error='transaction error', code=500)
Example #13
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     try:
         session.commit()
     except Exception as e:
         session.rollback()
         raise e