Skip to content

Commit

Permalink
Merge pull request #5 from oarepo/alzbetapokorna/be-502-new-version-r…
Browse files Browse the repository at this point in the history
…equest-when-a-new-version-of-a-published-record

abstract mapping class
  • Loading branch information
Alzpeta authored Nov 11, 2024
2 parents ac71ad6 + 6216c37 commit 10c679f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
15 changes: 0 additions & 15 deletions oarepo_doi/datacite_mapping.py

This file was deleted.

48 changes: 48 additions & 0 deletions oarepo_doi/doi_mapping_base.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 8 additions & 1 deletion oarepo_doi/services/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion oarepo_doi/translations/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-doi
version = 1.0.13
version = 1.0.14
description =
authors = Alzbeta Pokorna <[email protected]>
readme = README.md
Expand Down

0 comments on commit 10c679f

Please sign in to comment.