11import logging
22from pathlib import Path
3- import random
43from typing import TYPE_CHECKING , Any
54from urllib import parse
65
1918from isic .core .services .collection import (
2019 collection_get_creators_in_attribution_order ,
2120 collection_lock ,
22- collection_update ,
2321)
2422from isic .core .services .snapshot import snapshot_images
2523from isic .core .tasks import (
@@ -101,17 +99,12 @@ def collection_build_draft_doi(*, doi_id: str) -> dict:
10199 }
102100
103101
104- def collection_generate_random_doi_id ():
105- # pad DOI with leading zeros so all DOIs are prefix/6 digits
106- return f"{ settings .ISIC_DATACITE_DOI_PREFIX } /{ random .randint (10_000 , 999_999 ):06} " # noqa: S311
107-
108-
109102def collection_check_create_doi_allowed (
110103 * , user : User , collection : Collection , supplemental_files = None
111104) -> None :
112105 if not user .has_perm ("core.create_doi" , collection ):
113106 raise ValidationError ("You don't have permissions to do that." )
114- if collection . doi :
107+ if hasattr ( collection , " doi" ) :
115108 raise ValidationError ("This collection already has a DOI." )
116109 if not collection .public :
117110 raise ValidationError ("A collection must be public to issue a DOI." )
@@ -175,21 +168,13 @@ def collection_create_doi(*, user: User, collection: Collection, supplemental_fi
175168 user = user , collection = collection , supplemental_files = supplemental_files
176169 )
177170
178- doi_id = collection_generate_random_doi_id ()
179- draft_doi_dict = collection_build_draft_doi (doi_id = doi_id )
180- doi_dict = collection_build_doi (collection = collection , doi_id = doi_id )
181-
182171 with transaction .atomic ():
183172 # First, create the local DOI record to validate uniqueness within our known set
184- doi = Doi (
185- id = doi_id , slug = slugify (collection .name ), creator = user , url = f"https://doi.org/{ doi_id } "
186- )
173+ doi = Doi (slug = slugify (collection .name ), collection = collection , creator = user )
187174 doi .full_clean ()
188175 doi .save ()
189176
190- # Lock the collection, set the DOI on it
191177 collection_lock (collection = collection )
192- collection_update (collection = collection , doi = doi , ignore_lock = True )
193178
194179 if supplemental_files :
195180 for supplemental_file in supplemental_files :
@@ -200,17 +185,20 @@ def collection_create_doi(*, user: User, collection: Collection, supplemental_fi
200185 size = supplemental_file ["blob" ].size ,
201186 )
202187
188+ draft_doi_dict = collection_build_draft_doi (doi_id = doi .id )
189+ doi_dict = collection_build_doi (collection = collection , doi_id = doi .id )
190+
203191 # Reserve the DOI using the draft mechanism.
204192 # If it fails, transaction will rollback, nothing in our database will change.
205193 _datacite_create_doi (draft_doi_dict )
206194
207195 # Convert to a published DOI. If this fails, someone will have to come along later and
208196 # retry to publish it. (May want a django-admin action for this if it ever happens.)
209- _datacite_update_doi (doi_dict , doi_id )
197+ _datacite_update_doi (doi_dict , doi . id )
210198
211- create_doi_bundle_task .delay_on_commit (doi_id )
212- fetch_doi_citations_task .delay_on_commit (doi_id )
213- fetch_doi_schema_org_dataset_task .delay_on_commit (doi_id )
199+ create_doi_bundle_task .delay_on_commit (doi . id )
200+ fetch_doi_citations_task .delay_on_commit (doi . id )
201+ fetch_doi_schema_org_dataset_task .delay_on_commit (doi . id )
214202
215203 logger .info ("User %d created DOI %s for collection %d" , user .id , doi .id , collection .id )
216204
0 commit comments