Skip to content

Commit

Permalink
Merge 141197e into 5f3d3bf
Browse files Browse the repository at this point in the history
  • Loading branch information
njeri-ngigi committed Dec 29, 2018
2 parents 5f3d3bf + 141197e commit 04e179c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,10 @@ class Tag(TimeStamp):

def __str__(self):
return self.tag

@classmethod
def edit_tags(cls):
tags = set(Article.objects.values_list('tags', flat=True))
if None in tags:
tags.remove(None)
Tag.objects.exclude(pk__in=(tags)).delete()
1 change: 1 addition & 0 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def update(self, instance, validated_data):
'description', instance.description)
if tags:
instance.tags.set(tags)
Tag.edit_tags()
instance.images = validated_data.get('images', instance.images)
instance.time_to_read = self.get_time_to_read(
instance.body, instance.images)
Expand Down
9 changes: 9 additions & 0 deletions authors/apps/articles/tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def test_update_article(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(article_data["title"],
json.loads(response.content)["title"])
# test update tags
response = self.client.put(
self.TESTARTICLE, {"tagList": ["new tag"]}, HTTP_AUTHORIZATION=self.token, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("new tag",
json.loads(response.content)["tagList"])
response2 = self.get_article_tags()
self.assertNotIn("math", json.loads(response2.content)["tags"])

# test non-author
response = self.client.put(
self.TESTARTICLE, article_data, HTTP_AUTHORIZATION=self.non_user_token, format="json")
Expand Down
1 change: 1 addition & 0 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def destroy(self, request, slug):
if email != article.author:
raise PermissionDenied
article.delete()
Tag.edit_tags()
return Response(dict(message="Article {} deleted successfully".format(slug)), status=status.HTTP_200_OK)


Expand Down

0 comments on commit 04e179c

Please sign in to comment.