Exemple #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
    def add_server(self, server: Server) -> None:
        if not self.has_free_slots():
            raise ValidationError('The rack has not free slots')

        session.add(server)
        session.commit()

        current_app.logger.info(f'В стойку с идентификатором {self.obj.id} добавлен сервер')
    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
Exemple #4
0
    async def create_user(self, email: str, password1: str, password2: str,
                          nickname: str) -> Union[User, NoReturn]:
        if password1 != password2:
            raise PasswordDoesNotMatchException

        if (session.query(User).filter(
                or_(User.email == email, User.nickname == nickname)).first()):
            raise DuplicateEmailOrNicknameException

        user = User(email=email, password=password1, nickname=nickname)
        session.add(user)

        return user
Exemple #5
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
    async def execute(
        self,
        email: str,
        password1: str,
        password2: str,
        nickname: str,
    ) -> Union[User, NoReturn]:
        if password1 != password2:
            raise CustomException(error='password does not match', code=400)

        if session.query(User).filter(User.email == email).first():
            raise CustomException(error='duplicated email', code=400)

        if session.query(User).filter(User.nickname == nickname).first():
            raise CustomException(error='duplicated nickname', code=400)

        user = User(email=email, password=password1, nickname=nickname)
        session.add(user)
        session.commit()

        return user
Exemple #7
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
Exemple #8
0
    def update_user(
        self,
        user_id: int = None,
        password: str = None,
        email: str = None,
        nickname: str = None,
    ) -> Union[User, NoReturn]:
        user = session.query(User).get(user_id)

        if not user:
            raise CustomException(error='user does not exist', code=500)

        if password:
            user.password = password

        if email:
            user.email = email

        if nickname:
            user.nickname = nickname

        session.add(user)
        return user
Exemple #9
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
Exemple #11
0
 def create_user(self, email: str, password: str, nickname: str) -> User:
     user = User(email=email, password=password, nickname=nickname)
     session.add(user)
     return user
 def create(self, model: t.Type[BaseModel]) -> t.Type[BaseModel]:
     session.add(model)
     session.commit()
     return model