-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prefix doi management commands
- prefix all one-off destructive DOI commands with `doi_` - add reset_staging to mint new DOIs on staging using the datacite sandbox, doi_reset_staging -> step 3, doi_mint_parent_codebase_dois - bump deps for datacite schema 4.5 and django cve
- Loading branch information
Showing
13 changed files
with
296 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import csv | ||
import logging | ||
import sys | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from library.doi import VERIFICATION_MESSAGE, get_welcome_message, DataCiteApi | ||
from library.models import Codebase, CodebaseRelease | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def reset_all_dois(interactive=True, dry_run=True): | ||
print(get_welcome_message(dry_run)) | ||
if settings.DEPLOY_ENVIRONMENT.is_production: | ||
logger.error("This command is not allowed in production.") | ||
sys.exit() | ||
logger.info("(ENV: %s) Removing all DOIs", settings.DEPLOY_ENVIRONMENT) | ||
releases_with_dois = CodebaseRelease.objects.with_doi() | ||
codebases_with_dois = Codebase.objects.with_doi() | ||
confirm = input( | ||
"WARNING: this will remove ALL existing DOIs and is unrecoverable. Type 'DELETE' to continue or Ctrl+C to quit: " | ||
) | ||
if confirm.lower() == "delete": | ||
with open("codebase_dois.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["Codebase ID", "Codebase DOI"]) | ||
for codebase in codebases_with_dois: | ||
writer.writerow([codebase.pk, codebase.doi]) | ||
Codebase.objects.update(doi=None) | ||
with open("release_doi.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["CodebaseRelease ID", "CodebaseRelease DOI"]) | ||
for release in releases_with_dois: | ||
writer.writerow([release.pk, release.doi]) | ||
CodebaseRelease.objects.update(doi=None) | ||
else: | ||
logger.info("Aborting.") | ||
sys.exit() | ||
|
||
""" | ||
assert correctness | ||
""" | ||
if not dry_run: | ||
print(VERIFICATION_MESSAGE) | ||
assert Codebase.objects.with_doi().count() == 0 | ||
assert CodebaseRelease.objects.with_doi().count() == 0 | ||
logger.info("Success. All existing codebase DOIs deleted.") | ||
|
||
""" Mint DOIs for all new Peer Reviewed Releases""" | ||
peer_reviewed_releases = CodebaseRelease.objects.reviewed().public() | ||
datacite_api = DataCiteApi(dry_run=dry_run) | ||
invalid_releases = [] | ||
for release in peer_reviewed_releases: | ||
try: | ||
datacite_api.mint_new_doi_for_release(release) | ||
except Exception as e: | ||
logger.error("Error minting DOI for release %s", release) | ||
invalid_releases.append((release, e)) | ||
|
||
for release, error in invalid_releases: | ||
with open("invalid_releases.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["CodebaseRelease ID", "Reason", "Datacite Metadata"]) | ||
writer.writerow([release.pk, error, release.datacite.to_dict()]) | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--interactive", | ||
action="store_true", | ||
help="Wait for user to press enter to continue.", | ||
default=True, | ||
) | ||
parser.add_argument( | ||
"--dry-run", action="store_true", help="Output what would have happened." | ||
) | ||
|
||
def handle(self, *args, **options): | ||
interactive = options["interactive"] | ||
dry_run = options["dry_run"] | ||
reset_all_dois(interactive, dry_run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
django/library/migrations/0031_dataciteregistrationlog_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Generated by Django 4.2.16 on 2024-10-29 21:28 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("library", "0030_peerreviewinvitation"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="DataCiteRegistrationLog", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"action", | ||
models.CharField( | ||
choices=[ | ||
("CREATE_RELEASE_DOI", "create release DOI"), | ||
("CREATE_CODEBASE_DOI", "create codebase DOI"), | ||
("UPDATE_RELEASE_METADATA", "update release metadata"), | ||
("UPDATE_CODEBASE_METADATA", "update codebase metadata"), | ||
], | ||
max_length=50, | ||
), | ||
), | ||
("timestamp", models.DateTimeField(auto_now_add=True)), | ||
("http_status", models.IntegerField(default=None, null=True)), | ||
("message", models.TextField(default=None, null=True)), | ||
("metadata_hash", models.CharField(max_length=255)), | ||
("doi", models.CharField(blank=True, max_length=255, null=True)), | ||
], | ||
), | ||
migrations.AlterModelOptions( | ||
name="peerreviewinvitation", | ||
options={"ordering": ["-date_sent"]}, | ||
), | ||
migrations.RemoveField( | ||
model_name="contributor", | ||
name="affiliations", | ||
), | ||
migrations.AlterField( | ||
model_name="codebase", | ||
name="date_created", | ||
field=models.DateTimeField(auto_now_add=True), | ||
), | ||
migrations.AlterField( | ||
model_name="codebaserelease", | ||
name="date_created", | ||
field=models.DateTimeField(auto_now_add=True), | ||
), | ||
migrations.AlterField( | ||
model_name="codebasereleasedownload", | ||
name="date_created", | ||
field=models.DateTimeField(auto_now_add=True), | ||
), | ||
migrations.AlterField( | ||
model_name="peerreviewinvitation", | ||
name="date_sent", | ||
field=models.DateTimeField(auto_now=True), | ||
), | ||
migrations.DeleteModel( | ||
name="ContributorAffiliation", | ||
), | ||
migrations.AddField( | ||
model_name="dataciteregistrationlog", | ||
name="codebase", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="datacite_logs", | ||
to="library.codebase", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="dataciteregistrationlog", | ||
name="release", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="datacite_logs", | ||
to="library.codebaserelease", | ||
), | ||
), | ||
] |
Oops, something went wrong.