Exemple #1
0
    def save(self, user, password):
        roles = models.UserRole.query.filter(
            models.UserRole.id.in_([role.id for role in user.roles])).all()

        if not roles:
            raise NoResultFound([role.id for role in user.roles], 'UserRole')

        try:
            new_user = self.user_model(name=user.name, roles=roles, password=password)
            self.session.add(new_user)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('User already exist')

            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return self._to_domain_model(new_user)
Exemple #2
0
    def update(self, user):
        user_to_update = self.user_model.query.get(user.id)

        if not user_to_update:
            raise NoResultFound(user.id, User.__name__)

        roles = models.UserRole.query.filter(
            models.UserRole.id.in_([role.id for role in user.roles])).all()

        if not roles:
            raise NoResultFound([role.id for role in user.roles], 'UserRole')

        try:
            user_to_update.name = user.name
            user_to_update.roles = roles

            self.session.add(user_to_update)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('User already exist')
            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return self._to_domain_model(user_to_update)
    def update(self, task):
        task_to_update = self.task_model.query.get(task.id)

        if not task_to_update:
            raise NoResultFound(task.id, Task.__name__)

        doer_id = None

        if task.doer:
            doer_id = task.doer.id
            doer = models.User.query.get(doer_id)
            if not doer:
                raise NoResultFound(doer_id, 'User')

        creator = models.User.query.get(task.creator.id)

        if not creator:
            raise NoResultFound(task.creator.id, 'User')

        if not models.TaskStatus.query.get(task.status.id):
            raise NoResultFound(task.status.id, 'TaskStatus')

        try:
            task_to_update.status_id = task.status.id
            task_to_update.creator_id = task.creator.id

            task_to_update.doer_id = doer_id

            task_to_update.content = task.content
            task_to_update.name = task.name

            self.session.add(task_to_update)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('Task already exist')
            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return self._to_domain_model(task_to_update)
    def save(self, role):
        try:
            new_role = self.role_model(name=role.name)
            self.session.add(new_role)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('Role already exist')

            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return UserRole(id=new_role.id, name=new_role.name)
    def save(self, status):
        try:
            new_status = self.status_model(name=status.name)
            self.session.add(new_status)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('Status already exist')

            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return TaskStatus(id=new_status.id, name=new_status.name)
    def update(self, role):
        role_to_update = self.role_model.query.get(role.id)

        if not role_to_update:
            raise NoResultFound(role.id, UserRole.__name__)

        try:
            role_to_update.name = role.name
            self.session.add(role_to_update)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('Role already exist')
            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return UserRole(id=role_to_update.id, name=role_to_update.name)
    def update(self, status):
        status_to_update = self.status_model.query.get(status.id)

        if not status_to_update:
            raise NoResultFound(status.id, TaskStatus.__name__)

        try:
            status_to_update.name = status.name

            self.session.add(status_to_update)
            self.session.commit()

        except exc.IntegrityError as e:
            self.session.rollback()

            if 'unique' in str(e).lower():
                raise NotUnique('Status already exist')
            raise

        except exc.SQLAlchemyError:
            self.session.rollback()
            raise DbError()

        return TaskStatus(id=status_to_update.id, name=status_to_update.name)