Exemplo n.º 1
0
    def patch(self, pid, record, **kwargs):
        """Modify a record.

        The data should be a JSON-patch, which will be applied to the record.

        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()

        self.check_etag(str(record.revision_id))
        try:
            record = record.patch(data)
        except (JsonPatchException, JsonPointerException):
            raise PatchJSONFailureRESTError()

        record.commit()
        db.session.commit()
        if not (current_app.config.get('TESTING', False)
                or current_app.config.get('FAKE_DOI', False)):
            doi = None
            for rec_pid in record['_pid']:
                if rec_pid['type'] == 'DOI':
                    doi = rec_pid
            doi_pid = PersistentIdentifier.get('doi', doi['value'])
            if doi_pid:
                from .serializers import datacite_v44
                from .minters import make_record_url
                try:
                    datacite_provider = DataCiteProvider(doi_pid)
                    doc = datacite_v44.serialize(doi_pid, record)
                    url = make_record_url(pid.pid_value)
                    datacite_provider.update(url, doc)
                except:
                    current_app.logger.error(
                        "Error in DataCite metadata update", exc_info=True)
        return self.make_response(pid,
                                  record,
                                  links_factory=self.links_factory)