Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#164046267 Users can highlight text and comment on it #36

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ThreadedComment(TimeStamped):
on_delete=models.CASCADE)
body = models.TextField(_("Body"))
is_active = models.BooleanField(default=True)
highlight = models.TextField(_("Article Highlight"), blank=True)

objects = models.Manager()
active_objects = managers.CommentQuerySet.as_manager()
Expand Down Expand Up @@ -108,13 +109,21 @@ def edited(self):
"""Return whether the comment has been edited."""
return self.snapshots.all().exists()

@property
def is_article_highlighted(self):
"""Return whether an article has been highlighted in this comment."""
if self.highlight:
return True
return False


class Snapshot(models.Model):
"""Model to take snapshots of comments everytime they are edited."""
timestamp = models.DateTimeField(auto_now_add=True)
comment = models.ForeignKey('ThreadedComment', related_name='snapshots',
on_delete=models.CASCADE)
body = models.TextField(_("Body"))
highlight = models.TextField(_("Highlight"), null=True, blank=True)

class Meta:
ordering = ('-timestamp',)
Expand Down
18 changes: 13 additions & 5 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class Meta():

class ArticleCommentInputSerializer(serializers.ModelSerializer):
"""Seriliazes input data and creates a new article comment."""
highlight = serializers.CharField(min_length=1, required=False)

class Meta:
model = ThreadedComment
fields = ('article', 'author', 'body')
fields = ('article', 'author', 'body', "highlight")
extra_kwargs = {'article': {'required': True}}


Expand All @@ -77,27 +79,33 @@ class SnapshotSerializer(serializers.ModelSerializer):
"""Serializer for displaying comment snapshots."""
class Meta:
model = Snapshot
fields = ('id', 'body', 'timestamp')
fields = ('id', 'highlight', 'body', 'timestamp')


class EmbededCommentOutputSerializer(serializers.ModelSerializer):
"""Seriliazes comment and gives output data."""
author = ProfileSerializer()
edit_history = SnapshotSerializer(many=True, source='snapshots')
is_highlight_comment = serializers.BooleanField(
source="is_article_highlighted")

class Meta:
model = ThreadedComment
fields = ('id', 'created_at', 'updated_at', 'edited', 'body', 'author',
'edit_history')
fields = ('id', 'created_at', 'updated_at', 'edited',
'is_highlight_comment', 'highlight', 'body',
'author', 'edit_history')


class ThreadedCommentOutputSerializer(serializers.ModelSerializer):
"""Seriliazes comment and gives output data."""
author = ProfileSerializer()
comments = EmbededCommentOutputSerializer(many=True)
edit_history = SnapshotSerializer(many=True, source='snapshots')
is_highlight_comment = serializers.BooleanField(
source="is_article_highlighted")

class Meta:
model = ThreadedComment
fields = ('id', 'created_at', 'updated_at', 'edited', 'body', 'author',
fields = ('id', 'created_at', 'updated_at', 'edited', 'body',
'is_highlight_comment', 'highlight', 'author',
'edit_history', 'comments')
3 changes: 2 additions & 1 deletion authors/apps/articles/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def take_comment_snapshot_handler(sender, instance, created, **kwargs):
"""
if created:
return
snapshot = Snapshot.objects.create(comment=instance, body=instance.body)
snapshot = Snapshot.objects.create(comment=instance, body=instance.body,
highlight=instance.highlight)
snapshot.save()
18 changes: 18 additions & 0 deletions authors/apps/articles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ def test_snapshot_is_saved_only_when_a_comment_is_edited(self):
self.assertEqual(snapshots.first().body, comment.body)
same_comment = ThreadedComment.objects.get(id = comment.id)
self.assertTrue(same_comment.edited)


class ArticleHighlightComment(TestCase):

def setUp(self):
self.user1 = UserFactory.create()
self.article = ArticleFactory.create(author=self.user1)

def test_commenting_on_article_with_text_hightlight(self):
body = "I dont think so"
comment = ThreadedComment.objects.create(
author=self.user1.profile, article=self.article, body=body)
body2 = "I dont think so"
self.assertFalse(comment.is_article_highlighted)
comment2 = ThreadedComment.objects.create(author=self.user1.profile,
article=self.article, body=body2,
highlight="blah blah text")
self.assertTrue(comment2.is_article_highlighted)