From 60e262aa4cd2fa8fde4f7889c7f49a5fe1bd8cd9 Mon Sep 17 00:00:00 2001 From: JamieRL Date: Fri, 3 Aug 2018 13:30:30 -0700 Subject: [PATCH 1/2] override publishable delete to delete the parent object and cause cascade delete, add test case for deleting multiple versions of an article. Removed check for delete method in get_publishable_queryset() --- dispatch/api/mixins.py | 6 ++-- dispatch/modules/content/models.py | 6 ++++ dispatch/tests/test_api_articles.py | 51 ++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/dispatch/api/mixins.py b/dispatch/api/mixins.py index 425d63889..235a34a6e 100644 --- a/dispatch/api/mixins.py +++ b/dispatch/api/mixins.py @@ -38,9 +38,9 @@ def get_publishable_queryset(self): if version is not None: queryset = queryset.filter(revision_id=version) - elif self.request.method == 'DELETE': - if queryset: - queryset = queryset.filter(id=F('parent_id')) + # elif self.request.method == 'DELETE': + # if queryset: + # queryset = queryset.filter(id=F('parent_id')) else: queryset = queryset.filter(head=True) else: diff --git a/dispatch/modules/content/models.py b/dispatch/modules/content/models.py index 87374300e..18023c3ca 100644 --- a/dispatch/modules/content/models.py +++ b/dispatch/modules/content/models.py @@ -206,6 +206,12 @@ def save(self, revision=True, *args, **kwargs): return self + # Overriding delete the parent article to cascade delete all versions + def delete(self, *args, **kwargs): + if self.parent == self: + return super(Publishable, self).delete(*args, **kwargs) + return self.parent.delete() + def save_featured_image(self, data): """ Handles saving the featured image. diff --git a/dispatch/tests/test_api_articles.py b/dispatch/tests/test_api_articles.py index db627e0a0..584385f8d 100644 --- a/dispatch/tests/test_api_articles.py +++ b/dispatch/tests/test_api_articles.py @@ -433,6 +433,25 @@ def test_delete_article(self): response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + def test_delete_article_multiple_versions(self): + """Ensure that deleting an article deletes all versions of the article""" + + article = DispatchTestHelpers.create_article(self.client) + + # Update the article to create a new version + data = { + 'content': [] + } + + url = reverse('api-articles-detail', args=[article.data['id']]) + response = self.client.patch(url, data, format='json') + + # Delete the article + response = self.client.delete(url, format='json') + + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertFalse(Article.objects.filter(parent__id=article.data['id']).exists()) + def test_publish_article(self): """Ensure that an existing article can be published""" @@ -693,14 +712,14 @@ def test_author_query_multiple_authors(self): self.assertEqual(data['results'][1]['headline'], 'Article 2') self.assertEqual(data['results'][2]['headline'], 'Article 1') self.assertEqual(data['count'], 3) - + def test_set_featured_image(self): """Ensure that a featured image can be set""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-images-list') - + image_file = 'test_image_a.jpg' with open(self.get_input_file(image_file)) as test_image: @@ -722,9 +741,9 @@ def test_set_featured_image_no_id(self): """Ensure that there must have an image_id in order to set a featured image""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-images-list') - + image_file = 'test_image_a.jpg' with open(self.get_input_file(image_file)) as test_image: @@ -747,16 +766,16 @@ def test_set_featured_image_no_id(self): } response = self.client.patch(url, data, format='json') - + self.assertEqual(response.data['featured_image'], None) def test_remove_featured_image(self): """Ensure that a featured image can be removed""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-images-list') - + image_file = 'test_image_a.jpg' with open(self.get_input_file(image_file)) as test_image: @@ -777,14 +796,14 @@ def test_remove_featured_image(self): } response = self.client.patch(url, data, format='json') - + self.assertEqual(response.data['featured_image'], None) def test_set_featured_video(self): """Ensure that a featured video can be set""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-videos-list') video = DispatchTestHelpers.create_video(self.client, 'testVideo') @@ -805,7 +824,7 @@ def test_set_featured_video_no_id(self): """Ensure that there must have a video_id in order to set a featured video""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-videos-list') video = DispatchTestHelpers.create_video(self.client, 'testVideo') @@ -827,16 +846,16 @@ def test_set_featured_video_no_id(self): } response = self.client.patch(url, data, format='json') - + self.assertEqual(response.data['featured_video'], None) def test_remove_featured_video(self): """Ensure that a featured video can be removed""" article = DispatchTestHelpers.create_article(self.client) - + url = reverse('api-videos-list') - + video = DispatchTestHelpers.create_video(self.client, 'testVideo') data = { @@ -854,5 +873,5 @@ def test_remove_featured_video(self): } response = self.client.patch(url, data, format='json') - - self.assertEqual(response.data['featured_video'], None) \ No newline at end of file + + self.assertEqual(response.data['featured_video'], None) From a7bb77448875889412935e498e6510da9fb67284 Mon Sep 17 00:00:00 2001 From: JamieRL Date: Fri, 3 Aug 2018 13:32:07 -0700 Subject: [PATCH 2/2] remove commented code --- dispatch/api/mixins.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/dispatch/api/mixins.py b/dispatch/api/mixins.py index 235a34a6e..929206382 100644 --- a/dispatch/api/mixins.py +++ b/dispatch/api/mixins.py @@ -38,9 +38,6 @@ def get_publishable_queryset(self): if version is not None: queryset = queryset.filter(revision_id=version) - # elif self.request.method == 'DELETE': - # if queryset: - # queryset = queryset.filter(id=F('parent_id')) else: queryset = queryset.filter(head=True) else: