Skip to content

Commit

Permalink
Merge pull request #37 from andela/ft-track-edit-history-160617689
Browse files Browse the repository at this point in the history
#160617689 track comment edit history
  • Loading branch information
Walukagga Patrick committed Oct 17, 2018
2 parents 76e32cb + 4513a0a commit 3a4f378
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 9 deletions.
23 changes: 23 additions & 0 deletions authors/apps/comments/migrations/0002_commenthistory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.1.1 on 2018-10-16 13:55

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('comments', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='CommentHistory',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField()),
('updated_at', models.DateTimeField(auto_now_add=True)),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='comments.Comment')),
],
),
]
16 changes: 15 additions & 1 deletion authors/apps/comments/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models.signals import post_save

from authors.apps.articles.models import Article
from authors.apps.authentication.models import User
Expand All @@ -22,7 +23,6 @@ class Comment(models.Model):

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# objects = CommentManager()

def children(self):
return Comment.objects.filter(parent=self)
Expand All @@ -32,3 +32,17 @@ def is_parent(self):
if self.parent is not True:
return False
return True


class CommentHistory(models.Model):
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
body = models.TextField()
updated_at = models.DateTimeField(auto_now_add=True)


def create_history(sender, **kwargs):
body = kwargs['instance'].body
CommentHistory.objects.create(comment=kwargs['instance'], body=body)


post_save.connect(create_history, sender=Comment)
3 changes: 3 additions & 0 deletions authors/apps/comments/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ class CommentJSONRenderer(AhJSONRenderer):
class CommentThreadJSONRenderer(AhJSONRenderer):
charset = 'utf-8'
object_label = 'thread'

class CommentHistoryJSONRenderer(AhJSONRenderer):
object_label = 'edit-version'
9 changes: 8 additions & 1 deletion authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from authors.apps.authentication.models import User
from authors.apps.profiles.models import Profile

from .models import Comment
from .models import Comment, CommentHistory


class CommentSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -52,3 +52,10 @@ def validate(self, data):
'Please insert the body of the comment'
)
return data


class CommentHistorySerializer(serializers.ModelSerializer):

class Meta:
model = CommentHistory
fields = ('comment', 'body', 'updated_at')
3 changes: 2 additions & 1 deletion authors/apps/comments/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.urls import path

from .views import (CommentListCreateView, CommentsView,
CommentThreadListCreateView)
CommentThreadListCreateView, CommentHistoryView)

app_name = 'comments'
urlpatterns = [
path('<str:slug>/comments/', CommentListCreateView.as_view(),name='comments'),
path('<str:slug>/comments/<int:id>', CommentsView.as_view(),name='indivdual comment'),
path('<str:slug>/comments/<int:id>/thread', CommentThreadListCreateView.as_view(),name='thread comment'),
path('<str:slug>/comments/<int:id>/edit-history/', CommentHistoryView.as_view(),name='edit history'),
]
30 changes: 25 additions & 5 deletions authors/apps/comments/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from authors.apps.articles.models import Article
from django.db.models import Count
from django.shortcuts import get_object_or_404, render
from rest_framework import generics, serializers, status
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import (IsAuthenticated,
from rest_framework.permissions import (AllowAny, IsAuthenticated,
IsAuthenticatedOrReadOnly)
from rest_framework.response import Response

from .models import Comment
from .renderer import CommentJSONRenderer, CommentThreadJSONRenderer
from .serializers import CommentChildSerializer, CommentSerializer
from authors.apps.articles.models import Article

from .models import Comment, CommentHistory
from .renderer import (CommentHistoryJSONRenderer, CommentJSONRenderer,
CommentThreadJSONRenderer)
from .serializers import (CommentChildSerializer, CommentHistorySerializer,
CommentSerializer)

# Create your views here.


class CommentListCreateView(generics.ListCreateAPIView):
serializer_class = CommentSerializer
permission_classes = (IsAuthenticated,)
Expand Down Expand Up @@ -102,3 +107,18 @@ def get(self, request, *args, **kwargs):
slug=article_slug, parent=self.kwargs['id'])
serializer = self.serializer_class(comment, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class CommentHistoryView(generics.ListAPIView):
serializer_class = CommentHistorySerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
renderer_classes = (CommentHistoryJSONRenderer,)
queryset = CommentHistory.objects.all()

def get(self, request, *args, **kwargs):
comment_history = self.queryset.filter(
comment_id=self.kwargs['id'])
if comment_history.count() < 2:
return Response({"msg": "This comment has never been edited"})
serializer = self.serializer_class(comment_history, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
18 changes: 18 additions & 0 deletions authors/apps/profiles/migrations/0003_auto_20181017_1030.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-10-17 07:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0002_profile_favorites'),
]

operations = [
migrations.AlterField(
model_name='followinguser',
name='date_added',
field=models.DateTimeField(auto_now_add=True),
),
]
17 changes: 16 additions & 1 deletion tests/test_articles/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@ def test_make_a_thread_with_no_data(self):
f'/api/articles/{self.slug}/comments/{comment_id}/thread',format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


def test_retrieve_edit_history(self):
comment = self.create_a_comment()
comment_id = comment.data['id']
self.client.put(
f'/api/articles/{self.slug}/comments/{comment_id}', self.test_comment_edited, format="json")
response =self.client.get(
f'/api/articles/{self.slug}/comments/{comment_id}/edit-history/')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_no_edit_history(self):
comment=self.create_a_comment()
comment_id = comment.data['id']
response =self.client.get(
f'/api/articles/{self.slug}/comments/{comment_id}/edit-history/')
self.assertIn("never been edited", str(response.data))




Expand Down

0 comments on commit 3a4f378

Please sign in to comment.