Skip to content

Commit

Permalink
De-duplicate and normalize unfinished and unpublished check
Browse files Browse the repository at this point in the history
  • Loading branch information
cslzchen committed Dec 31, 2024
1 parent c471ad5 commit deaeb4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,18 @@ def create(cls, provider, title, creator, description):

return preprint

def has_unpublished_pending_version(self):
guid_obj = self.guids.first()
last_not_rejected_version = guid_obj.versions.filter(is_rejected=False).order_by('-version').first().referent
return last_not_rejected_version.machine_state == 'pending' and not last_not_rejected_version.is_published

def check_unfinished_pending_version(self):
guid_obj = self.guids.first()
last_not_rejected_version = guid_obj.versions.filter(is_rejected=False).order_by('-version').first().referent
def get_last_not_rejected_version(self):
guid_obj = self.get_guid()
return guid_obj.versions.filter(is_rejected=False).order_by('-version').first().referent

def check_unpublished_pending_version(self):
last_not_rejected_version = self.get_last_not_rejected_version()
if last_not_rejected_version.date_published:
return None
return last_not_rejected_version if last_not_rejected_version.machine_state == 'pending' else None

def check_initiated_but_unfinished_version(self):
last_not_rejected_version = self.get_last_not_rejected_version()
return last_not_rejected_version if last_not_rejected_version.machine_state == 'initial' else None

@classmethod
Expand All @@ -335,15 +339,20 @@ def create_version(cls, create_from_guid, auth):
if not source_preprint.has_permission(auth.user, ADMIN):
sentry.log_message('User must have admin permissions to create new version.')
raise PermissionsError
if source_preprint.has_unpublished_pending_version():
sentry.log_message('Fail to create a new version since an unpublished pending version already exists.')
unpublished_pending_version = source_preprint.check_unpublished_pending_version()
if unpublished_pending_version:
sentry.log_message('Unpublished pending version found; failed to create a new one: '
f'[version={unpublished_pending_version.version}, '
f'_id={unpublished_pending_version._id}, '
f'state={unpublished_pending_version.machine_state}].')
raise UnpublishedPendingPreprintVersionExists
unfinished_version = source_preprint.check_unfinished_pending_version()
if unfinished_version:
initiated_but_unfinished_version = source_preprint.check_initiated_but_unfinished_version()
if initiated_but_unfinished_version:
sentry.log_message(f'Unfinished version found, using it instead of creating a new one: '
f'[version={unfinished_version.version}, _id={unfinished_version._id}, '
f'state={unfinished_version.machine_state}].')
return unfinished_version, None
f'[version={initiated_but_unfinished_version.version}, '
f'_id={initiated_but_unfinished_version._id}, '
f'state={initiated_but_unfinished_version.machine_state}].')
return initiated_but_unfinished_version, None

# Note: last version may not be the latest version
last_version_number = guid_obj.versions.order_by('-version').first().version
Expand Down
2 changes: 1 addition & 1 deletion osf_tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def create_version(cls, create_from, creator=None, final_machine_state='accepted

# Run a few checks
assert final_machine_state in ['pending', 'accepted']
assert create_from and not create_from.has_unpublished_pending_version()
assert create_from and not create_from.check_unpublished_pending_version()
guid_obj = create_from.guids.first()
latest_version = guid_obj.referent
assert latest_version.is_latest_version
Expand Down

0 comments on commit deaeb4c

Please sign in to comment.