Python jsonpatch.JsonPatchException() Examples

The following are code examples for showing how to use jsonpatch.JsonPatchException(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: invenio-records-rest   Author: inveniosoftware   File:    MIT License 5 votes vote down vote up
def patch(self, pid, record, **kwargs):
        """Modify a record.

        Permissions: ``update_permission_factory``

        The data should be a JSON-patch, which will be applied to the record.
        Requires header ``Content-Type: application/json-patch+json``.

        Procedure description:

        #. The record is deserialized using the proper loader.

        #. The ETag is checked.

        #. The record is patched.

        #. The HTTP response is built with the help of the link factory.

        :param pid: Persistent identifier for record.
        :param record: Record object.
        :returns: The modified record.
        data = self.loaders[request.mimetype]()
        if data is None:
            raise InvalidDataRESTError()

            record = record.patch(data)
        except (JsonPatchException, JsonPointerException):
            raise PatchJSONFailureRESTError()

        if self.indexer_class:

        return self.make_response(
            pid, record, links_factory=self.links_factory) 
Example 2
Project: gnocchi   Author: gnocchixyz   File:    Apache License 2.0 4 votes vote down vote up
def patch(self):
        # NOTE(sileht): should we check for "application/json-patch+json"
        # Content-Type ?

            rt = pecan.request.indexer.get_resource_type(self._name)
        except indexer.NoSuchResourceType as e:
            abort(404, six.text_type(e))
        enforce("update resource type", rt)

        # Ensure this is a valid jsonpatch dict
        patch = deserialize_and_validate(

        # Add new attributes to the resource type
        rt_json_current = rt.jsonify()
            rt_json_next = jsonpatch.apply_patch(rt_json_current, patch)
        except jsonpatch.JsonPatchException as e:
            abort(400, six.text_type(e))
        del rt_json_next['state']

        # Validate that the whole new resource_type is valid
        schema = pecan.request.indexer.get_resource_type_schema()
            rt_json_next = voluptuous.Schema(schema.for_update, required=True)(
        except voluptuous.Error as e:
            abort(400, "Invalid input: %s" % e)

        # Get only newly formatted and deleted attributes
        add_attrs = {k: v for k, v in rt_json_next["attributes"].items()
                     if k not in rt_json_current["attributes"]}
        del_attrs = [k for k in rt_json_current["attributes"]
                     if k not in rt_json_next["attributes"]]

        if not add_attrs and not del_attrs:
            # NOTE(sileht): just returns the resource, the asked changes
            # just do nothing
            return rt

            add_attrs = schema.attributes_from_dict(add_attrs)
        except resource_type.InvalidResourceAttribute as e:
            abort(400, "Invalid input: %s" % e)

            return pecan.request.indexer.update_resource_type(
                self._name, add_attributes=add_attrs,
        except indexer.NoSuchResourceType as e:
                abort(400, six.text_type(e))