Skip to content

Commit ef9c076

Browse files
[ENG-8247] Ability to delete draft preprints from database (#11191)
## Purpose User should be able to delete draft preprints, so that we don't save them in database ## Changes Added `delete` method. User can delete a preprint only if it's in initial state, so it means this preprint is a draft ## Ticket https://openscience.atlassian.net/browse/ENG-8247
1 parent 42b3829 commit ef9c076

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

api/preprints/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def create(self, request, *args, **kwargs):
279279
return super().create(request, *args, **kwargs)
280280

281281

282-
class PreprintDetail(PreprintOldVersionsImmutableMixin, PreprintMetricsViewMixin, JSONAPIBaseView, generics.RetrieveUpdateAPIView, PreprintMixin, WaterButlerMixin):
282+
class PreprintDetail(PreprintOldVersionsImmutableMixin, PreprintMetricsViewMixin, JSONAPIBaseView, generics.RetrieveUpdateDestroyAPIView, PreprintMixin, WaterButlerMixin):
283283
"""The documentation for this endpoint can be found [here](https://developer.osf.io/#operation/preprints_read).
284284
"""
285285
permission_classes = (
@@ -326,6 +326,11 @@ def get_parser_context(self, http_request):
326326
res['legacy_type_allowed'] = True
327327
return res
328328

329+
def delete(self, request, *args, **kwargs):
330+
if self.get_preprint().machine_state == 'initial':
331+
return super().delete(request, *args, **kwargs)
332+
333+
raise ValidationError('You cannot delete created preprint')
329334

330335
class PreprintNodeRelationship(PreprintOldVersionsImmutableMixin, JSONAPIBaseView, generics.RetrieveUpdateAPIView, PreprintMixin):
331336
permission_classes = (

api_tests/preprints/views/test_preprint_detail.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,41 @@ def published_preprint(self, user):
195195
def url(self, user):
196196
return f'/{API_BASE}preprints/{{}}/'
197197

198-
def test_cannot_delete_preprints(
199-
self, app, user, url, unpublished_preprint, published_preprint):
200-
res = app.delete(url.format(unpublished_preprint._id), auth=user.auth, expect_errors=True)
201-
assert res.status_code == 405
202-
assert unpublished_preprint.deleted is None
203-
204-
res = app.delete(url.format(published_preprint._id), auth=user.auth, expect_errors=True)
205-
assert res.status_code == 405
206-
assert published_preprint.deleted is None
198+
def test_can_delete_draft_preprints(
199+
self, app, user, url, unpublished_preprint
200+
):
201+
202+
url = f'/{API_BASE}preprints/{unpublished_preprint._id}/'
203+
res = app.delete(url, auth=user.auth)
204+
assert res.status_code == 204
205+
206+
res = app.get(url, auth=user.auth, expect_errors=True)
207+
assert res.status_code == 404
208+
209+
def test_cannot_delete_published_preprints(self, app, user, url, published_preprint):
210+
url = f'/{API_BASE}preprints/{published_preprint._id}/'
211+
212+
res = app.delete(url, auth=user.auth, expect_errors=True)
213+
assert res.json['errors'][0]['detail'] == 'You cannot delete created preprint'
214+
res = app.get(url, auth=user.auth)
215+
assert res.status_code == 200
216+
217+
def test_cannot_delete_in_moderation_preprints(self, app, user, url):
218+
pre_moderation_preprint = PreprintFactory(creator=user, reviews_workflow='pre-moderation')
219+
post_moderation_preprint = PreprintFactory(creator=user, reviews_workflow='post-moderation')
220+
221+
url = f'/{API_BASE}preprints/{pre_moderation_preprint._id}/'
222+
res = app.delete(url, auth=user.auth, expect_errors=True)
223+
assert res.json['errors'][0]['detail'] == 'You cannot delete created preprint'
224+
res = app.get(url, auth=user.auth)
225+
assert res.status_code == 200
226+
227+
url = f'/{API_BASE}preprints/{post_moderation_preprint._id}/'
228+
229+
res = app.delete(url, auth=user.auth, expect_errors=True)
230+
assert res.json['errors'][0]['detail'] == 'You cannot delete created preprint'
231+
res = app.get(url, auth=user.auth)
232+
assert res.status_code == 200
207233

208234

209235
@pytest.mark.django_db

0 commit comments

Comments
 (0)