Skip to content

Commit

Permalink
feat(Articles):Users can report Articles
Browse files Browse the repository at this point in the history
Users can report articles that violate the terms
of use.
An article can oly be reported a maximum of five times
Admins/superusers can restore reported articles
When an article is reportes five times, it is
removed from published articles
Only admin can view a list of reported articles
and also delete or revert them

[Delivers #162163282]
  • Loading branch information
kafuuma Henry authored and kafuuma Henry committed Dec 20, 2018
1 parent 54d51ba commit c1d36f6
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 27 deletions.
22 changes: 20 additions & 2 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Article(models.Model):
Published is like a draft field, helps authors to wor
and save them to draft before publishing them
"""
published = models.BooleanField(default=False)
published = models.BooleanField(default=True)

"""
An article can have many tags and the reverse is true
Expand Down Expand Up @@ -106,7 +106,7 @@ class Comments(models.Model):
parent = models.ForeignKey(
'self', null=True, blank=True, on_delete=models.CASCADE)
updated_at = models.DateTimeField(auto_now_add=True)

likes_count = models.IntegerField(default=0)

def __str__(self):
Expand Down Expand Up @@ -135,10 +135,28 @@ class Likes(models.Model):

like = models.BooleanField()


class ComentLikes(models.Model):

comment = models.ForeignKey(Comments, on_delete=models.CASCADE)

profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)

like = models.BooleanField()


class ReportArticle(models.Model):
"""This class implements a model report articles that violate
terms of agreement
"""
article_id = models.ForeignKey(Article, on_delete=models.CASCADE)
article_slug = models.ForeignKey(
Article, to_field="slug", db_column="slug", related_name='a_slug', on_delete=models.CASCADE, null=True)
reported_by = models.ForeignKey(Profile, on_delete=models.CASCADE)
reason = models.CharField(db_index=True, null=False, max_length=255)
reported_at = models.DateTimeField(auto_now_add=True)

objects = models.Manager()

class Meta:
ordering = ['-reported_at']
34 changes: 32 additions & 2 deletions authors/apps/articles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def render(self, data, media_type=None, renderer_context=None):
the default JSONRenderer to handle rendering errors, so we need to
check for this case.
"""

errors = data.get('errors', None)

if errors is not None:
"""
As mentioned about, we will let the default JSONRenderer handle
rendering errors.
return super(ArticlesJSONRenderer, self).render(data)
"""
return super(ArticleJSONRenderer, self).render(data)
"""Finally, we can render our data under the "user" namespace."""
return json.dumps({
'article': data
Expand All @@ -38,3 +38,33 @@ def render(self, data, media_type=None, renderer_context=None):
})


class ArticleReportJSONRenderer(JSONRenderer):
charset = 'utf-8'

def render(self, data, media_type=None, renderer_context=None):

errors = data.get('errors', None)

if errors is not None:

return super(ArticleReportJSONRenderer, self).render(data)

return json.dumps({
'reported': data
})


class ArticleListReportJSONRenderer(JSONRenderer):
charset = 'utf-8'

def render(self, data, media_type=None, renderer_context=None):

errors = data.get('errors', None)

if errors is not None:

return super(ArticleListReportJSONRenderer, self).render(data)

return json.dumps({
'reported': data
})
21 changes: 17 additions & 4 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Article,
ArticleImg,
Tag,
Favourites, Likes
Favourites,
Likes,
ReportArticle,
)


Expand All @@ -28,7 +30,7 @@ class Meta:
List all of the fields that could possibly be included in a request
or response, this includes fields specified explicitly above.
"""
fields = ['title', 'body', 'description', 'tagList',
fields = ['id', 'title', 'body', 'description', 'tagList',
'author', 'slug', 'published', 'created_at', 'updated_at', ]

def create(self, validated_data):
Expand Down Expand Up @@ -63,7 +65,7 @@ class ArticleImgSerializer(serializers.ModelSerializer):

class Meta:
model = ArticleImg
fields = ['image_url', 'description',
fields = ['id', 'image_url', 'description',
'position_in_body_before', 'article']


Expand All @@ -76,7 +78,7 @@ class Meta:
List all of the fields that could possibly be included in a request
or response, this includes fields specified explicitly above.
"""
fields = ['title', 'body', 'description',
fields = ['id', 'title', 'body', 'description',
'author', 'slug', 'published', 'created_at', 'updated_at', ]


Expand Down Expand Up @@ -108,3 +110,14 @@ class LikeArticleViewSerializer(serializers.ModelSerializer):
class Meta:
model = Likes
fields = ['id', 'article', 'profile', 'like']


class ReportArticleSerializer(serializers.ModelSerializer):
reported_by = ProfileSerializer(read_only=True)
reported_at = serializers.DateTimeField(read_only=True)
reason = serializers.CharField()

class Meta:
model = ReportArticle
fields = ['article_id', 'article_slug',
'reported_by', 'reason', 'reported_at', ]
14 changes: 14 additions & 0 deletions authors/apps/articles/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ def setUp(self):
}
}

self.report_article_data = {

"report": {
"reason": "article contains porn"
}
}

self.report_article_data_empty_reason = {

"report": {
"reason": ""
}
}

url = reverse('registration')
self.client.post(url, self.data, format='json')

Expand Down
Loading

0 comments on commit c1d36f6

Please sign in to comment.