diff --git a/api/users/views.py b/api/users/views.py index 620beec44c38..927b5dc2f9b7 100644 --- a/api/users/views.py +++ b/api/users/views.py @@ -436,6 +436,7 @@ def get_default_queryset(self): user = self.get_user() return user.preprints.filter( machine_state='initial', + deleted__isnull=True, ) def get_queryset(self): diff --git a/api_tests/users/views/test_user_draft_preprint.py b/api_tests/users/views/test_user_draft_preprint.py index a62c2acaa932..1c3af6f525be 100644 --- a/api_tests/users/views/test_user_draft_preprint.py +++ b/api_tests/users/views/test_user_draft_preprint.py @@ -8,7 +8,7 @@ PreprintProviderFactory, ) from api.base.settings.defaults import API_BASE - +from django.utils import timezone @pytest.mark.django_db class TestPreprintDraftList: @@ -97,6 +97,20 @@ def abandoned_public_preprint(self, admin, provider, subject, public_project): machine_state='initial' ) + @pytest.fixture() + def deleted_preprint(self, admin, provider, subject, public_project): + preprint = PreprintFactory( + creator=admin, + provider=provider, + project=public_project, + is_published=False, + is_public=False, + machine_state='initial', + ) + preprint.deleted = timezone.now() + preprint.save() + return preprint + def test_gets_preprint_drafts(self, app, admin, abandoned_public_preprint, abandoned_private_preprint, published_preprint): res = app.get( f'/{API_BASE}users/{admin._id}/draft_preprints/', @@ -131,3 +145,16 @@ def test_get_projects_logged_in_as_write_user(self, app, admin, write_contrib, a expect_errors=True ) assert res.status_code == 403 + + def test_deleted_drafts_ecluded(self, app, admin, abandoned_public_preprint, abandoned_private_preprint, published_preprint, deleted_preprint): + res = app.get( + f'/{API_BASE}users/{admin._id}/draft_preprints/', + auth=admin.auth + ) + assert res.status_code == 200 + + ids = [each['id'] for each in res.json['data']] + assert abandoned_public_preprint._id in ids + assert abandoned_private_preprint._id in ids + assert published_preprint._id not in ids + assert deleted_preprint._id not in ids # Make sure deleted preprints are not listed