-
Notifications
You must be signed in to change notification settings - Fork 0
Backend model changes (add ORCID ID) #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b53256
feat: Add ORCID integration backend support (model changes, migration…
klpoland 1f48202
use migration to convert authors field instead
klpoland c3f8f34
orcid validation, migration edits
klpoland e24d240
dont update authors automatically
klpoland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
gateway/sds_gateway/api_methods/migrations/0016_convert_dataset_authors_to_object_format.py
This file contains hidden or 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,165 @@ | ||
| """Migration to convert dataset authors from string format to object format.""" | ||
|
|
||
| import json | ||
| import logging | ||
|
|
||
| from django.db import migrations | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def convert_authors_to_object_format(apps, schema_editor): | ||
| """Convert dataset authors from string format to object format.""" | ||
| Dataset = apps.get_model("api_methods", "Dataset") | ||
|
|
||
| # Get all datasets with authors | ||
| datasets = Dataset.objects.filter(authors__isnull=False).exclude(authors="") | ||
| total_count = datasets.count() | ||
|
|
||
| if total_count == 0: | ||
| logger.info("No datasets with authors found to convert") | ||
| return | ||
|
|
||
| logger.info("Converting %d datasets with authors to new format", total_count) | ||
|
|
||
| updated_count = 0 | ||
| skipped_count = 0 | ||
| error_count = 0 | ||
|
|
||
| for dataset in datasets: | ||
| try: | ||
| # Parse current authors | ||
| if isinstance(dataset.authors, str): | ||
| try: | ||
| current_authors = json.loads(dataset.authors) | ||
| except (json.JSONDecodeError, TypeError): | ||
| # Skip datasets with invalid JSON | ||
| logger.warning("Dataset %s has invalid authors JSON, skipping", dataset.uuid) | ||
| skipped_count += 1 | ||
| continue | ||
| else: | ||
| current_authors = dataset.authors | ||
|
|
||
| if not current_authors: | ||
| skipped_count += 1 | ||
| continue | ||
|
|
||
| # Check if already in new format (has dict with 'name' key) | ||
| if isinstance(current_authors, list) and current_authors: | ||
| if isinstance(current_authors[0], dict) and "name" in current_authors[0]: | ||
| logger.debug("Dataset %s already in new format, skipping", dataset.uuid) | ||
| skipped_count += 1 | ||
| continue | ||
|
|
||
| # Convert string authors to object format | ||
| if isinstance(current_authors[0], str): | ||
| new_authors = [] | ||
| for author in current_authors: | ||
| if isinstance(author, str): | ||
| new_authors.append({"name": author, "orcid_id": ""}) | ||
| else: | ||
| # Handle unexpected format | ||
| logger.warning( | ||
| "Dataset %s has unexpected author format: %s", | ||
| dataset.uuid, author | ||
| ) | ||
| new_authors.append({"name": str(author), "orcid_id": ""}) | ||
|
|
||
| # Update the dataset | ||
| dataset.authors = json.dumps(new_authors) | ||
| dataset.save(update_fields=["authors"]) | ||
| updated_count += 1 | ||
| logger.debug("Converted dataset %s authors", dataset.uuid) | ||
| else: | ||
| # Authors are already objects, skip | ||
| skipped_count += 1 | ||
| else: | ||
| # Empty or invalid authors list | ||
| skipped_count += 1 | ||
|
|
||
| except Exception as e: | ||
| error_count += 1 | ||
| logger.error("Error converting dataset %s: %s", dataset.uuid, e) | ||
|
|
||
| logger.info( | ||
| "Author conversion complete: %d updated, %d skipped, %d errors", | ||
| updated_count, skipped_count, error_count | ||
| ) | ||
|
|
||
|
|
||
| def reverse_authors_to_string_format(apps, schema_editor): | ||
| """Reverse migration: convert authors back to string format.""" | ||
| Dataset = apps.get_model("api_methods", "Dataset") | ||
|
|
||
| # Get all datasets with authors | ||
| datasets = Dataset.objects.filter(authors__isnull=False).exclude(authors="") | ||
| total_count = datasets.count() | ||
|
|
||
| if total_count == 0: | ||
| logger.info("No datasets with authors found to reverse") | ||
| return | ||
|
|
||
| logger.info("Reversing %d datasets with authors to string format", total_count) | ||
|
|
||
| updated_count = 0 | ||
| skipped_count = 0 | ||
| error_count = 0 | ||
|
|
||
| for dataset in datasets: | ||
| try: | ||
| # Parse current authors | ||
| if isinstance(dataset.authors, str): | ||
| try: | ||
| current_authors = json.loads(dataset.authors) | ||
| except (json.JSONDecodeError, TypeError): | ||
| skipped_count += 1 | ||
| continue | ||
| else: | ||
| current_authors = dataset.authors | ||
|
|
||
| if not current_authors: | ||
| skipped_count += 1 | ||
| continue | ||
|
|
||
| # Check if in object format (has dict with 'name' key) | ||
| if isinstance(current_authors, list) and current_authors: | ||
| if isinstance(current_authors[0], dict) and "name" in current_authors[0]: | ||
| # Convert object authors back to string format | ||
| string_authors = [author["name"] for author in current_authors] | ||
|
|
||
| # Update the dataset | ||
| dataset.authors = json.dumps(string_authors) | ||
| dataset.save(update_fields=["authors"]) | ||
| updated_count += 1 | ||
| logger.debug("Reversed dataset %s authors", dataset.uuid) | ||
| else: | ||
| # Already in string format, skip | ||
| skipped_count += 1 | ||
| else: | ||
| # Empty or invalid authors list | ||
| skipped_count += 1 | ||
|
|
||
| except Exception as e: | ||
| error_count += 1 | ||
| logger.error("Error reversing dataset %s: %s", dataset.uuid, e) | ||
|
|
||
| logger.info( | ||
| "Author reversal complete: %d updated, %d skipped, %d errors", | ||
| updated_count, skipped_count, error_count | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| """Migration to convert dataset authors to object format.""" | ||
|
|
||
| dependencies = [ | ||
| ("api_methods", "0015_rename_postprocesseddata_deprecatedpostprocesseddata_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| convert_authors_to_object_format, | ||
| reverse_authors_to_string_format, | ||
| hints={"target_db": "default"}, | ||
| ), | ||
| ] | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0016_alter_usersharepermission_permission_level | ||
| 0016_convert_dataset_authors_to_object_format |
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
gateway/sds_gateway/users/migrations/0011_user_orcid_id_alter_user_is_approved.py
This file contains hidden or 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,24 @@ | ||
| # Generated by Django 4.2.14 on 2025-09-04 12:52 | ||
|
|
||
| from django.db import migrations, models | ||
| from ..validators import validate_orcid_id | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('users', '0010_userapikey_description_and_name'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='user', | ||
| name='orcid_id', | ||
| field=models.CharField( | ||
| blank=True, | ||
| max_length=19, | ||
| validators=[validate_orcid_id], | ||
| verbose_name='ORCID ID' | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0010_userapikey_description_and_name | ||
| 0011_user_orcid_id_alter_user_is_approved |
This file contains hidden or 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 hidden or 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,17 @@ | ||
| """Custom validators for the users app.""" | ||
|
|
||
| import re | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
|
|
||
| def validate_orcid_id(value): | ||
| """Validate the ORCID ID format.""" | ||
| if not value: | ||
| return value | ||
|
|
||
| if not re.match(r"^\d{4}-\d{4}-\d{4}-\d{4}$", value): | ||
| raise ValidationError(_("ORCID ID must be in the format 0000-0000-0000-0000.")) | ||
|
|
||
| return value |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.