Skip to content

Commit

Permalink
Merge pull request #844 from ubyssey/fix-publishable-delete
Browse files Browse the repository at this point in the history
Fix publishable delete
  • Loading branch information
JamieRL authored Aug 3, 2018
2 parents a9c9303 + a7bb774 commit 799f492
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
3 changes: 0 additions & 3 deletions dispatch/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions dispatch/modules/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 35 additions & 16 deletions dispatch/tests/test_api_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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 = {
Expand All @@ -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)

self.assertEqual(response.data['featured_video'], None)

0 comments on commit 799f492

Please sign in to comment.