Skip to content

Commit

Permalink
feature(Edit history): list comment edit history
Browse files Browse the repository at this point in the history
- A snapshot of the comment body is saved each time the
  comments is edited.
- Users can see the edit history of a comment.
- Add Tests

[Delivers #164046268]
  • Loading branch information
cob04 committed Mar 20, 2019
1 parent f6ba080 commit 7ca6c63
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
8 changes: 8 additions & 0 deletions authors/apps/articles/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class ArticlesConfig(AppConfig):
name = 'authors.apps.articles'

def ready(self):
from . import signals # noqa
13 changes: 13 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,16 @@ def for_comment(self):
if self.comment:
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"))

class Meta:
ordering = ('-timestamp',)
verbose_name = _("Comment Snapshot")
verbose_name_plural = _("Comment Snapshots")
16 changes: 13 additions & 3 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from authors.apps.profiles.serializers import ProfileSerializer

from .models import Article, Like, ThreadedComment
from .models import Article, Like, Snapshot, ThreadedComment


class TheArticleSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -73,21 +73,31 @@ class Meta:
extra_kwargs = {'comment': {'required': True}}


class SnapshotSerializer(serializers.ModelSerializer):
"""Serializer for displaying comment snapshots."""
class Meta:
model = Snapshot
fields = ('id', 'body', 'timestamp')


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

class Meta:
model = ThreadedComment
fields = ('id', 'created_at', 'updated_at', 'body', 'author')
fields = ('id', 'created_at', 'updated_at', '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')

class Meta:
model = ThreadedComment
fields = ('id', 'created_at', 'updated_at', 'body', 'author',
'comments')
'edit_history', 'comments')
15 changes: 15 additions & 0 deletions authors/apps/articles/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Snapshot, ThreadedComment


@receiver(post_save, sender=ThreadedComment)
def take_comment_snapshot_handler(sender, instance, created, **kwargs):
"""Make a snapshot of a comment body everytime the comment is edited
except for when it has just been newly created.
"""
if created:
return
snapshot = Snapshot.objects.create(comment=instance, body=instance.body)
snapshot.save()
21 changes: 20 additions & 1 deletion authors/apps/articles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from authors.apps.core.factories import UserFactory

from ..factories import ArticleFactory
from ..models import ThreadedComment
from ..models import Snapshot, ThreadedComment


class CommentMethodTests(TestCase):
Expand Down Expand Up @@ -34,3 +34,22 @@ def test_comment_undoing_soft_deletion(self):
self.assertEqual(comment.is_active, False)
comment.undo_soft_deletion()
self.assertEqual(comment.is_active, True)


class CommentSnapshotSignalTests(TestCase):

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

def test_snapshot_is_saved_only_when_a_comment_is_edited(self):
body = "testing comment"
comment = ThreadedComment.objects.create(
author=self.user1.profile, article=self.article1, body=body)
self.assertNotEqual(comment.snapshots.all(), [])
comment.body = "edited this comment"
comment.save()
snapshots = Snapshot.objects.filter(comment=comment)
self.assertQuerysetEqual(comment.snapshots.all(),
[repr(snap) for snap in snapshots])
self.assertEqual(snapshots.first().body, comment.body)
2 changes: 1 addition & 1 deletion authors/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'authors.apps.authentication',
'authors.apps.core',
'authors.apps.profiles',
'authors.apps.articles',
'authors.apps.articles.apps.ArticlesConfig',
]
MIDDLEWARE_CLASSES = (
'whitenoise.middleware.WhiteNoiseMiddleware',
Expand Down

0 comments on commit 7ca6c63

Please sign in to comment.