-
Couldn't load subscription status.
- Fork 350
[ENG-8832] Most recent numbered version of a preprint fails to show for unversioned URL #11315
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
adlius
merged 6 commits into
CenterForOpenScience:feature/pbs-25-21
from
opaduchak:fix/ENG-8832
Oct 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
77e2f03
fix versioned guids resolving to the last published version instead o…
opaduchak 31f89fa
fix migration job
opaduchak f5e6e1a
handle initial review_state in fix_versioned_guids job
opaduchak 62523d4
adjusted tests to pass with new guid resolve strategy
opaduchak 3c6ecd1
handle the case for withdrawn/rejected preprints
opaduchak 5f715b2
fix missing.all()
opaduchak 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
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,73 @@ | ||
| import logging | ||
|
|
||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.core.management.base import BaseCommand | ||
| from django.db import transaction | ||
| from django.db.models import Prefetch | ||
|
|
||
| from osf.models import GuidVersionsThrough, Guid, Preprint | ||
| from osf.utils.workflows import ReviewStates | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = 'Fix' | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| '--dry-run', | ||
| action='store_true', | ||
| dest='dry_run', | ||
| help='Run the command without saving changes', | ||
| ) | ||
|
|
||
| @transaction.atomic | ||
| def handle(self, *args, **options): | ||
| dry_run = options.get('dry_run', False) | ||
| fix_versioned_guids(dry_run=dry_run) | ||
| if dry_run: | ||
| transaction.set_rollback(True) | ||
|
|
||
|
|
||
| def fix_versioned_guids(dry_run: bool): | ||
| content_type = ContentType.objects.get_for_model(Preprint) | ||
| versions_queryset = GuidVersionsThrough.objects.order_by('-version') | ||
| processed_count = 0 | ||
| updated_count = 0 | ||
| skipped_count = 0 | ||
| errors_count = 0 | ||
| for guid in ( | ||
| Guid.objects.filter(content_type=content_type) | ||
| .prefetch_related(Prefetch('versions', queryset=versions_queryset)) | ||
| .iterator(chunk_size=500) | ||
| ): | ||
| processed_count += 1 | ||
| if not guid.versions: | ||
| skipped_count += 1 | ||
| continue | ||
| for version in guid.versions.all(): | ||
| last_version_object_id = version.object_id | ||
| if guid.object_id == last_version_object_id: | ||
| skipped_count += 1 | ||
| break | ||
| if version.referent.machine_state not in (ReviewStates.INITIAL.value, ReviewStates.REJECTED.value, ReviewStates.WITHDRAWN.value): | ||
| continue | ||
| try: | ||
| guid.object_id = last_version_object_id | ||
| guid.referent = version.referent | ||
| if not dry_run: | ||
| guid.save() | ||
| updated_count += 1 | ||
| except Exception as e: | ||
| logger.error(f"Error occurred during patching {guid._id=}", exc_info=e) | ||
| errors_count += 1 | ||
|
|
||
| if dry_run: | ||
| logger.error( | ||
| f"Processed: {processed_count}, Would update: {updated_count}, Skipped: {skipped_count}, Errors: {errors_count}" | ||
| ) | ||
| else: | ||
| logger.error( | ||
| f"Processed: {processed_count}, Updated: {updated_count}, Skipped: {skipped_count}, Errors: {errors_count}" | ||
| ) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another case that I forgot to ask during my first review. And this is probably a product question too.