diff --git a/oarepo_doi/datacite_mapping.py b/oarepo_doi/datacite_mapping.py deleted file mode 100644 index 9bb59b2..0000000 --- a/oarepo_doi/datacite_mapping.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Protocol - - -class DataCiteMappingProtocol(Protocol): - def metadata_check(self, data, errors=[]) -> []: - ... - - def create_datacite_payload(self, data) -> None: - ... - - def get_doi(self, record) -> {}: - ... - - def add_doi(self, record, data, doi_value) -> None: - ... diff --git a/oarepo_doi/doi_mapping_base.py b/oarepo_doi/doi_mapping_base.py new file mode 100644 index 0000000..d082c31 --- /dev/null +++ b/oarepo_doi/doi_mapping_base.py @@ -0,0 +1,48 @@ +from abc import ABC, abstractmethod + + +class DataCiteMappingBase(ABC): + + @abstractmethod + def metadata_check(self, data): + """Checks metadata for required fields and returns errors if any.""" + pass + + @abstractmethod + def create_datacite_payload(self, data): + """Creates a DataCite payload from the given data.""" + pass + + + def get_doi(self, record): + """Extracts DOI from the record.""" + + object_identifiers = record["metadata"].get("objectIdentifiers", []) + doi = None + for id in object_identifiers: + if id["scheme"] == "DOI": + doi = id["identifier"] + return doi + + def add_doi(self, record, data, doi_value): + """Adds a DOI to the record.""" + + doi = {"scheme": "DOI", "identifier": doi_value} + + if "objectIdentifiers" in data["metadata"]: + data["metadata"]["objectIdentifiers"].append(doi) + else: + data["metadata"]["objectIdentifiers"] = [doi] + + record.update(data) + record.commit() + + def remove_doi(self, record): + """Removes DOI from the record.""" + + if "objectIdentifiers" in record["metadata"]: + for id in record["metadata"]["objectIdentifiers"]: + if id["scheme"] == "DOI": + record["metadata"]["objectIdentifiers"].remove(id) + + record.commit() diff --git a/oarepo_doi/services/components/__init__.py b/oarepo_doi/services/components/__init__.py index 32c9411..0d4ccb7 100644 --- a/oarepo_doi/services/components/__init__.py +++ b/oarepo_doi/services/components/__init__.py @@ -2,7 +2,7 @@ from invenio_records_resources.services.records.components import ServiceComponent from oarepo_doi.api import community_slug_for_credentials, create_doi, edit_doi - +from invenio_base.utils import obj_or_import_string class DoiComponent(ServiceComponent): def __init__(self, *args, **kwargs): @@ -77,3 +77,10 @@ def publish(self, identity, data=None, record=None, **kwargs): ) self.credentials(slug) edit_doi(self, record, "publish") + + def new_version(self, identity, draft=None, record=None, **kwargs): + """Update draft metadata.""" + mapping = obj_or_import_string(self.mapping[record.schema])() + doi_value = mapping.get_doi(record) + if doi_value is not None: + mapping.remove_doi(draft) diff --git a/oarepo_doi/translations/messages.pot b/oarepo_doi/translations/messages.pot index ee71cf4..659c711 100644 --- a/oarepo_doi/translations/messages.pot +++ b/oarepo_doi/translations/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2024-10-30 12:26+0100\n" +"POT-Creation-Date: 2024-10-31 09:56+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/setup.cfg b/setup.cfg index 31139c8..76e480b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-doi -version = 1.0.13 +version = 1.0.14 description = authors = Alzbeta Pokorna readme = README.md