Skip to content

Commit

Permalink
fix: doi version identifier not generating correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sagargg committed Jul 29, 2024
1 parent 00a43a9 commit fb6e8a5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
14 changes: 7 additions & 7 deletions ckanext/doi/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_prefix(cls):
)
return prefix

def generate_doi(self, suffix=None):
def generate_doi(self, identifier=None):
"""
Generate a new DOI which isn't currently in use.
Expand All @@ -89,15 +89,15 @@ def generate_doi(self, suffix=None):
attempts = 5

while attempts > 0:
# generate a random 8 character identifier
identifier = ''.join(random.choice(valid_characters) for _ in range(8))

year = dt.now().year
# form the doi using the prefix
if suffix:
doi = f'{self.prefix}/{year}.{suffix}'
if identifier:
doi = identifier
else:
doi = f'{self.prefix}/{year}.{identifier}'

# generate a random 8 character identifier
random_identifier = ''.join(random.choice(valid_characters) for _ in range(8))
doi = f'{self.prefix}/{year}.{random_identifier}'
if DOIQuery.read_doi(doi) is None:
try:
self.client.metadata_get(doi)
Expand Down
12 changes: 8 additions & 4 deletions ckanext/doi/model/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,26 @@ def read_package(
from ckanext.doi.lib.api import DataciteClient

doi_exist = Session.query(DOI).filter(DOI.package_id == package_id).first()
suffix = None

identifier = None
if is_version and version:
# Find the DOI for the version
version_doi = (
Session.query(DOI).filter(DOI.package_id == is_version).first()
)

if version_doi:
# Remove existing version suffix and append the new version
identifier = re.sub(r'\.v\d+$', '', version_doi.identifier)
suffix = f"{identifier}.v{version}"
identifier = f"{identifier}.v{version}"

# If no DOI exists and creation is allowed
if doi_exist is None and create_if_none:
client = DataciteClient()
new_doi = client.generate_doi(suffix=suffix)
new_doi = client.generate_doi(identifier=identifier)
new_doi = cls.create(new_doi, package_id)
return new_doi

return doi_exist

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion ckanext/doi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def after_dataset_update(self, context, pkg_dict):
is_active = pkg_dict.get('state', 'active') == 'active' and not pkg_dict.get(
'private', False
)
if True:
if is_active:

package_id = pkg_dict['id']

Expand Down

0 comments on commit fb6e8a5

Please sign in to comment.