示例#1
0
文件: manager.py 项目: tpandit/potion
    def create(self, properties, commit=True):
        # noinspection properties
        item = self.model()

        for key, value in properties.items():
            setattr(item, key, value)

        before_create.send(self.resource, item=item)

        session = self._get_session()

        try:
            session.add(item)
            if commit:
                session.commit()
        except IntegrityError as e:
            session.rollback()

            if hasattr(e.orig, 'pgcode'):
                if e.orig.pgcode == "23505":  # duplicate key
                    raise DuplicateKey(detail=e.orig.diag.message_detail)

            if current_app.debug:
                raise BackendConflict(debug_info=dict(exception_message=str(e),
                                                      statement=e.statement,
                                                      params=e.params))
            raise BackendConflict()

        after_create.send(self.resource, item=item)
        return item
示例#2
0
    def create(self, properties, commit=True):
        # noinspection properties
        item = self.model()

        for key, value in properties.items():
            setattr(item, key, value)

        before_create.send(self.resource, item=item)

        session = self._get_session()

        try:
            session.add(item)
            if commit:
                session.commit()
        except IntegrityError as e:
            session.rollback()

            if hasattr(e.orig, 'pgcode'):
                if e.orig.pgcode == "23505":  # duplicate key
                    raise DuplicateKey(detail=e.orig.diag.message_detail)

            if current_app.debug:
                raise BackendConflict(debug_info=dict(statement=e.statement, params=e.params))
            raise BackendConflict()

        after_create.send(self.resource, item=item)
        return item
示例#3
0
文件: manager.py 项目: infarm/potion
    def create(self, properties):
        session = self._get_session()
        with session.begin_nested():
            # noinspection properties
            item = self.model(**properties)

            before_create.send(self.resource, item=item)
            try:
                session.add(item)
                session.flush()
            except IntegrityError as exc:

                if hasattr(exc.orig, 'pgcode'):
                    if exc.orig.pgcode == "23505":  # duplicate key
                        raise DuplicateKey(detail=exc.orig.diag.message_detail) from exc

                if current_app.debug:
                    raise BackendConflict(
                        debug_info=dict(
                            exception_message=str(exc),
                            statement=exc.statement,
                            params=exc.params,
                        )
                    ) from exc
                raise BackendConflict() from exc

        after_create.send(self.resource, item=item)
        return item
示例#4
0
    def create(self, properties, commit=True):
        item = self.model()

        for key, value in properties.items():
            setattr(item, key, value)

        before_create.send(self.resource, item=item)

        try:
            item.save()
        except OperationError as e:
            if current_app.debug:
                raise BackendConflict(debug_info=dict(statement=e.args))
            raise BackendConflict()

        after_create.send(self.resource, item=item)
        return item
示例#5
0
    def create(self, properties, commit=True):
        item = self.model()

        for key, value in properties.items():
            setattr(item, key, value)

        before_create.send(self.resource, item=item)

        try:
            item.save()
        except OperationError as e:
            if current_app.debug:
                raise BackendConflict(debug_info=dict(statement=e.args))
            raise BackendConflict()

        after_create.send(self.resource, item=item)
        return item