Exemplo n.º 1
0
 def formatter(self, value):
     if self.pattern_properties:
         pattern, field = next(iter(self.pattern_properties.items()))
         return {
             get_value(self.mapping_attribute, v, None): field.format(v)
             for v in value
         }
     elif self.additional_properties:
         return {
             get_value(self.mapping_attribute, v, None):
             self.additional_properties.format(v)
             for v in value
         }
Exemplo n.º 2
0
    def update(self, item, changes, commit=True):
        session = self._get_session()

        actual_changes = {
            key: value for key, value in changes.items()
            if self._is_change(get_value(key, item, None), value)
        }

        try:
            before_update.send(self.resource, item=item, changes=actual_changes)

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

            self.commit_or_flush(commit)
        except IntegrityError as e:
            session.rollback()

            # XXX need some better way to detect postgres engine.
            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_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 3
0
    def update(self, item, changes, commit=True):
        session = self._get_session()
        actual_changes = {
            key: value
            for key, value in changes.items()
            if get_value(key, item, None) != value
        }

        try:
            before_update.send(self.resource,
                               item=item,
                               changes=actual_changes)

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

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

            # XXX need some better way to detect postgres engine.
            if hasattr(e.orig, 'pgcode'):
                if e.orig.pgcode == '23505':  # duplicate key
                    raise DuplicateKey(detail=e.orig.diag.message_detail)
            raise

        after_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 4
0
    def update(self, item, changes, commit=True):
        actual_changes = {
            key: value
            for key, value in changes.items()
            if get_value(key, item, None) != value
        }

        signals.before_update.send(self.resource,
                                   item=item,
                                   changes=actual_changes)

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

        try:
            item.save()
        except pw.IntegrityError as e:
            if current_app.debug:
                raise BackendConflict(debug_info=e.args)
            raise BackendConflict()

        signals.after_update.send(self.resource,
                                  item=item,
                                  changes=actual_changes)
        return item
Exemplo n.º 5
0
    def update(self, item, changes):
        session = self._get_session()
        with session.begin_nested():
            actual_changes = {
                key: value
                for key, value in changes.items()
                if self._is_change(get_value(key, item, None), value)
            }
            before_update.send(self.resource, item=item, changes=actual_changes)
            try:
                for key, value in changes.items():
                    setattr(item, key, value)
                session.flush()
            except IntegrityError as exc:

                # XXX need some better way to detect postgres engine.
                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_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 6
0
    def update(self, item, changes, commit=True):
        session = self._get_session()
        actual_changes = {
            key: value for key, value in changes.items()
            if get_value(key, item, None) != value
        }

        try:
            before_update.send(self.resource, item=item, changes=actual_changes)

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

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

            # XXX need some better way to detect postgres engine.
            if hasattr(e.orig, 'pgcode'):
                if e.orig.pgcode == '23505':  # duplicate key
                    raise DuplicateKey(detail=e.orig.diag.message_detail)
            raise

        after_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 7
0
    def update(self, item, changes, commit=True):
        session = self._get_session()

        actual_changes = {
            key: value
            for key, value in changes.items()
            if self._is_change(get_value(key, item, None), value)
        }

        try:
            before_update.send(self.resource,
                               item=item,
                               changes=actual_changes)

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

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

            # XXX need some better way to detect postgres engine.
            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_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 8
0
    def formatter(self, value):
        output = {}

        if self.properties:
            output = {
                key: field.format(
                    get_value(field.attribute or key, value, field.default))
                for key, field in self.properties.items()
            }
        else:
            output = {}

        if self.pattern_properties:
            pattern, field = next(iter(self.pattern_properties.items()))

            if not self.additional_properties:
                output.update({
                    k: field.format(v)
                    for k, v in value.items()
                    if k not in self._property_attributes
                })
            else:
                raise NotImplementedError()
                # TODO match regular expression
        elif self.additional_properties:
            field = self.additional_properties
            output.update({
                k: field.format(v)
                for k, v in value.items() if k not in self._property_attributes
            })

        return output
Exemplo n.º 9
0
    def update(self, item, changes, commit=True):
        actual_changes = {
            key: value for key, value in changes.items()
            if get_value(key, item, None) != value
            }

        try:
            before_update.send(self.resource, item=item, changes=actual_changes)

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

            if commit:
                item.save()
        except OperationError:
            raise

        after_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 10
0
    def update(self, item, changes, commit=True):
        try:
            with Transaction(
                    should_commit=lambda *args: commit) as transaction:
                actual_changes = {
                    key: value
                    for key, value in list(changes.items())
                    if self._is_change(get_value(key, item, None), value)
                }

                for key, value in list(changes.items()):
                    # Done for the reasons described here
                    # https://stackoverflow.com/questions/42559434/updates-to-json-field-dont-persist-to-db
                    if isinstance(value, collections.Mapping):
                        flag_modified(item, key)

                authorization_id = getattr(
                    item, self.target_model_authorization_attribute)
                authorization_item = transaction.find_by_id(
                    self.authorization_model, authorization_id)
                self.update_authorization_model(item, changes,
                                                authorization_item)
                authorization_item = transaction.add_or_update(
                    authorization_item, flush=True)

                self.before_update(transaction, item, changes,
                                   authorization_item)
                potion_signals.before_update.send(self.resource,
                                                  item=item,
                                                  changes=actual_changes)

                for key, value in list(changes.items()):
                    setattr(item, key, value)
                transaction.add_or_update(item)
        except IntegrityError as exception:
            self.integrity_error_handler(exception)

        potion_signals.after_update.send(self.resource,
                                         item=item,
                                         changes=actual_changes)
        return item
Exemplo n.º 11
0
    def formatter(self, value):
        output = {}

        if self.properties:
            output = {key: field.format(get_value(field.attribute or key, value, field.default)) for key, field in self.properties.items()}
        else:
            output = {}

        if self.pattern_properties:
            pattern, field = next(iter(self.pattern_properties.items()))

            if not self.additional_properties:
                output.update({k: field.format(v) for k, v in value.items() if k not in self._property_attributes})
            else:
                raise NotImplementedError()
                # TODO match regular expression
        elif self.additional_properties:
            field = self.additional_properties
            output.update({k: field.format(v) for k, v in value.items() if k not in self._property_attributes})

        return output
Exemplo n.º 12
0
    def update(self, item, changes, commit=True):
        actual_changes = {
            key: value for key, value in changes.items()
            if get_value(key, item, None) != value
        }

        signals.before_update.send(
            self.resource, item=item, changes=actual_changes)

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

        try:
            item.save()
        except pw.IntegrityError as e:
            if current_app.debug:
                raise BackendConflict(debug_info=e.args)
            raise BackendConflict()

        signals.after_update.send(
            self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 13
0
    def update(self, item, changes, commit=True):
        actual_changes = {
            key: value
            for key, value in changes.items()
            if get_value(key, item, None) != value
        }

        try:
            before_update.send(self.resource,
                               item=item,
                               changes=actual_changes)

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

            if commit:
                item.save()
        except OperationError:
            raise

        after_update.send(self.resource, item=item, changes=actual_changes)
        return item
Exemplo n.º 14
0
 def _sort_items(items, sort):
     for key, reverse in reversed(sort):
         items = sorted(items,
                        key=lambda item: get_value(key, item, None),
                        reverse=reverse)
     return items
Exemplo n.º 15
0
 def output(self, key, obj):
     key = key if self.attribute is None else self.attribute
     return self.format(get_value(key, obj, self.default))
Exemplo n.º 16
0
 def update_attribute(resource, item, value):
     attribute = field.attribute or route.attribute
     item = resource.manager.update(item, {attribute: value})
     return get_value(attribute, item, field.default)
Exemplo n.º 17
0
 def _sort_items(items, sort):
     for field, key, reverse in reversed(sort):
         items = sorted(items, key=lambda item: get_value(key, item, None), reverse=reverse)
     return items
Exemplo n.º 18
0
 def read_attribute(resource, item):
     return get_value(attribute, item, field.default)
Exemplo n.º 19
0
 def update_attribute(resource, item, value):
     attribute = field.attribute or route.attribute
     item = resource.manager.update(item, {attribute: value})
     return get_value(attribute, item, field.default)
Exemplo n.º 20
0
 def read_attribute(resource, item):
     return get_value(attribute, item, field.default)
Exemplo n.º 21
0
 def formatter(self, value):
     if self.pattern_properties:
         pattern, field = next(iter(self.pattern_properties.items()))
         return {get_value(self.mapping_attribute, v, None): field.format(v) for v in value}
     elif self.additional_properties:
         return {get_value(self.mapping_attribute, v, None): self.additional_properties.format(v) for v in value}
Exemplo n.º 22
0
 def output(self, key, obj):
     key = key if self.attribute is None else self.attribute
     return self.format(get_value(key, obj, self.default))