diff --git a/api/preprints/views.py b/api/preprints/views.py index 9caba1c43ff..436249fb0bf 100644 --- a/api/preprints/views.py +++ b/api/preprints/views.py @@ -72,40 +72,33 @@ class PreprintOldVersionsImmutableMixin: - """Override method to reject modify requests for old preprint versions (except for withdrawal) - """ - def update(self, request, *args, **kwargs): + """Override method to reject modify requests for old preprint versions (except for withdrawal)""" + + @staticmethod + def is_edit_allowed(preprint): + return True if any([ + preprint.is_latest_version, + preprint.machine_state in ['initial', 'rejected'], + preprint.provider.reviews_workflow == Workflows.PRE_MODERATION.value and + preprint.machine_state == 'pending', + ]) else False + + def handle_request(self, request, method, *args, **kwargs): preprint = self.get_preprint(check_object_permissions=False) - is_pre_mod_pending = ( - preprint.provider.reviews_workflow == Workflows.PRE_MODERATION.value and preprint.machine_state == 'pending' - ) - if preprint.is_latest_version or preprint.machine_state == 'initial' or is_pre_mod_pending: - return super().update(request, *args, **kwargs) + if PreprintOldVersionsImmutableMixin.is_edit_allowed(preprint): + return method(request, *args, **kwargs) message = f'User can not edit previous versions of a preprint: [_id={preprint._id}]' sentry.log_message(message) raise Conflict(detail=message) + def update(self, request, *args, **kwargs): + return self.handle_request(request, super().update, *args, **kwargs) + def create(self, request, *args, **kwargs): - preprint = self.get_preprint(check_object_permissions=False) - is_pre_mod_pending = ( - preprint.provider.reviews_workflow == Workflows.PRE_MODERATION.value and preprint.machine_state == 'pending' - ) - if preprint.is_latest_version or preprint.machine_state == 'initial' or is_pre_mod_pending: - return super().create(request, *args, **kwargs) - message = f'User can not edit previous versions of a preprint: [_id={preprint._id}]' - sentry.log_message(message) - raise Conflict(detail=message) + return self.handle_request(request, super().create, *args, **kwargs) def destroy(self, request, *args, **kwargs): - preprint = self.get_preprint(check_object_permissions=False) - is_pre_mod_pending = ( - preprint.provider.reviews_workflow == Workflows.PRE_MODERATION.value and preprint.machine_state == 'pending' - ) - if preprint.is_latest_version or preprint.machine_state == 'initial' or is_pre_mod_pending: - return super().destroy(request, *args, **kwargs) - message = f'User can not edit previous versions of a preprint: [_id={preprint._id}]' - sentry.log_message(message) - raise Conflict(detail=message) + return self.handle_request(request, super().destroy, *args, **kwargs) class PreprintMixin(NodeMixin): diff --git a/osf/models/preprint.py b/osf/models/preprint.py index 747b9ae20d1..6a8c29ce7b5 100644 --- a/osf/models/preprint.py +++ b/osf/models/preprint.py @@ -1547,6 +1547,11 @@ def run_accept(self, user, comment, **kwargs): base_guid_obj.object_id = self.pk base_guid_obj.content_type = ContentType.objects.get_for_model(self) base_guid_obj.save() + + versioned_guid = self.versioned_guids.first() + if versioned_guid.is_rejected: + versioned_guid.is_rejected = False + versioned_guid.save() return ret # Override ReviewableMixin