diff --git a/osf/models/preprint.py b/osf/models/preprint.py index 37e3f2968ff..8094b678ecd 100644 --- a/osf/models/preprint.py +++ b/osf/models/preprint.py @@ -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 @@ -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 diff --git a/osf_tests/factories.py b/osf_tests/factories.py index a0e5f66ef5b..17cf3f1558f 100644 --- a/osf_tests/factories.py +++ b/osf_tests/factories.py @@ -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