Skip to content

Commit

Permalink
Merge pull request #37 from andela/comments-should-track-edit-history…
Browse files Browse the repository at this point in the history
…-164798221

#164798221 Users should be Able to track comment History
  • Loading branch information
ja-odur committed Apr 18, 2019
2 parents 8ce10eb + d7d4156 commit 1d0946b
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 5 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ facebook-sdk = "*"
google-api-python-client = "*"
python-twitter = "*"
django-filter = "*"
django-simple-history = "*"

[requires]
python_version = "3.6.5"
28 changes: 26 additions & 2 deletions authors/apps/articles/tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
changed_article_data,
article_data_no_body,
like_data, dislike_data,
user1_rating, user2_rating_fail,
user1_rating, user2_rating_fail,
comment_1, comment_2, comment_3,
search_article_data
)
from rest_framework import status
Expand Down Expand Up @@ -216,4 +217,27 @@ def test_keyword_search(self):
keyword_get_resp = self.client.get('/api/articles/?keyword=slu', format='json')
article_title = keyword_get_resp.data.get('results')[0]['title']
self.assertIn('slu', article_title)


def test_create_article_comment_history(self):
user_token = self.create_user(test_user_data)
resp = self.client.post('/api/articles/', article_data,
HTTP_AUTHORIZATION=user_token, format='json')
article_slug = resp.data['article']['slug']
post_comment = self.client.post(f'/api/articles/{article_slug}/comments/',
comment_1, HTTP_AUTHORIZATION=user_token, format='json')
comment_id = post_comment.data["id"]

self.client.put(f'/api/articles/{article_slug}/comments/{comment_id}',
comment_2, HTTP_AUTHORIZATION=user_token, format='json')
self.client.put(f'/api/articles/{article_slug}/comments/{comment_id}',
comment_3, HTTP_AUTHORIZATION=user_token, format='json')
get_comment_history = self.client.get(f'/api/comments/{comment_id}/history',
HTTP_AUTHORIZATION=user_token, format='json')

self.assertIn(comment_1["comment"]["body"],
get_comment_history.data["history"][2]["comment_body"])
self.assertIn(comment_2["comment"]["body"],
get_comment_history.data["history"][1]["comment_body"])
self.assertEqual(get_comment_history.data["number_of_edits"], 2)


20 changes: 19 additions & 1 deletion authors/apps/articles/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@
}

user1_rating = {'ratings':4}
user2_rating_fail = {'ratings':10}
user2_rating_fail = {'ratings':10}

comment_1 = {
"comment": {
"body": "Machester was fake yesterday trully"
}
}

comment_2 = {
"comment": {
"body": "But Barca was better"
}
}

comment_3 = {
"comment": {
"body": "But did you see the Tottenham GAME!!"
}
}
41 changes: 41 additions & 0 deletions authors/apps/comments/migrations/0002_historicalcomment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.2 on 2019-04-17 09:26

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('articles', '0005_merge_20190416_1226'),
('comments', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='HistoricalComment',
fields=[
('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('body', models.TextField()),
('createdAt', models.DateTimeField(blank=True, editable=False)),
('updatedAt', models.DateTimeField(blank=True, editable=False)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField()),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('article', models.ForeignKey(blank=True, db_constraint=False, default='', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='articles.Article')),
('author', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('parent', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='comments.Comment')),
],
options={
'verbose_name': 'historical comment',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': 'history_date',
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]
2 changes: 2 additions & 0 deletions authors/apps/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import models
from authors.apps.authentication.models import User
from authors.apps.articles.models import Article
from simple_history.models import HistoricalRecords


class Comment(models.Model):
Expand All @@ -16,6 +17,7 @@ class Comment(models.Model):
body = models.TextField()
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
history = HistoricalRecords()

def __str__(self):
return self.body
Expand Down
4 changes: 3 additions & 1 deletion authors/apps/comments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from .views import (
CommentListAPIView,
CommentDetailAPIView,
CommentReplyDetailsView
CommentReplyDetailsView,
CommentEditHistoryView
)

app_name = 'comments'
Expand All @@ -14,4 +15,5 @@
path('comments', CommentListAPIView.as_view(), name='list'),
path('comments/<int:parent_id>/replies', CommentReplyDetailsView.as_view(), name='comment-replies'),
path('comments/<int:pk>', CommentDetailAPIView.as_view(), name='comment-detail'),
path('comments/<int:pk>/history',CommentEditHistoryView.as_view(), name='comment-edit-history'),
]
29 changes: 28 additions & 1 deletion authors/apps/comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
DestroyAPIView,
CreateAPIView,
RetrieveUpdateAPIView,
ListCreateAPIView
ListCreateAPIView,
GenericAPIView
)
from rest_framework.permissions import (
AllowAny,
Expand Down Expand Up @@ -73,3 +74,29 @@ class to retrieve all replies about a given comment

def get_queryset(self):
return self.queryset.filter(parent_id=self.kwargs.get('parent_id')).order_by('createdAt')

class CommentEditHistoryView(GenericAPIView):
"""
history of comment for particular user
"""
def get(self, request, pk):
comment = get_object_or_404(Comment, id=pk)
comment_history = comment.history.all()

edit_history = []
for edit in list(comment_history):


comment_edit_history = {
"date": edit.history_date,
"id": edit.history_id,
"comment_body": edit.body
}
edit_history.append(comment_edit_history)

comment = Comment.objects.get(pk=pk)

return Response({
"history": edit_history,
"number_of_edits": len(edit_history) - 1
}, status=status.HTTP_200_OK)
1 change: 1 addition & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'django_extensions',
'rest_framework',
'rest_framework_swagger',
'simple_history',

'authors.apps.authentication',
'authors.apps.core',
Expand Down

0 comments on commit 1d0946b

Please sign in to comment.