-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comments-crud): Add create, read, update and delete comment
- Add create comment - Add read comments - Add read comment - Add update comment - Add delete comment
- Loading branch information
Showing
18 changed files
with
494 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CommentsConfig(AppConfig): | ||
name = 'comments' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Generated by Django 2.1 on 2019-05-02 12:51 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('articles', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Comment', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('body', models.TextField()), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.Article')), | ||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='author', to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.db import models | ||
|
||
from authors.apps.authentication.models import User | ||
from authors.apps.articles.models import Article | ||
|
||
|
||
class Comment(models.Model): | ||
""" | ||
This class represents the comment model. | ||
""" | ||
|
||
article = models.ForeignKey(Article, related_name='comments', | ||
on_delete=models.CASCADE) | ||
body = models.TextField() | ||
author = models.ForeignKey(User, related_name='author', | ||
on_delete=models.CASCADE) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
""" | ||
This method returns a string representation of the | ||
`Comment` model instance. | ||
""" | ||
return self.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import Comment | ||
|
||
|
||
class CommentSerializer(serializers.ModelSerializer): | ||
""" | ||
This class maps the `Comment` model instance | ||
into JSON format. | ||
""" | ||
|
||
author = serializers.ReadOnlyField(source='author.username') | ||
|
||
class Meta: | ||
model = Comment | ||
# List all of the comment fields that could possibly be included in a | ||
# request or response, including fields specified explicitly above. | ||
fields = '__all__' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.urls import reverse | ||
|
||
from rest_framework.test import APITestCase, APIClient | ||
|
||
from authors.apps.articles.models import Article | ||
from authors.apps.comments.models import Comment | ||
|
||
User = get_user_model() | ||
|
||
|
||
class BaseTestCase(APITestCase): | ||
""" | ||
Testcases base for the comment API views. | ||
""" | ||
|
||
def setUp(self): | ||
"""Initialize test client.""" | ||
self.client = APIClient() | ||
self.user = User.objects.create_user( | ||
username='test1', email='test1@example.com', password='12345678' | ||
) | ||
setattr(self.user, 'email_verified', True) | ||
self.user.save() | ||
self.data = { | ||
'user': | ||
{ | ||
'email': 'test1@example.com', 'password': '12345678' | ||
} | ||
} | ||
self.login = reverse('user_login') | ||
self.login_response = self.client.post( | ||
self.login, self.data, format="json") | ||
user_token = self.login_response.data['token'] | ||
self.auth_header = 'Bearer {}'.format(user_token) | ||
self.article = Article.objects.create( | ||
id=1, | ||
title='Town hall at Andela', | ||
description='There was lots of fun', | ||
body='There was no TIA chant.', | ||
author=self.user | ||
) | ||
self.comment_data = Comment.objects.create( | ||
id=100, | ||
body="Heheheheheh ehehehehe.....", | ||
article=self.article, | ||
author=self.user | ||
) | ||
self.comment = { | ||
"comment": { | ||
"body": "His name was my name too." | ||
} | ||
} | ||
self.comment2 = { | ||
"comment": { | ||
"body": "His name was my name too..." | ||
} | ||
} | ||
self.comment3 = { | ||
"comment": { | ||
"body": "His name was my name too...XYZ" | ||
} | ||
} | ||
self.user2 = User.objects.create_user( | ||
username='test2', email='test2@example.com', password='12345678' | ||
) | ||
setattr(self.user2, 'email_verified', True) | ||
self.user2.save() | ||
self.data2 = { | ||
'user': { | ||
'email': 'test2@example.com', 'password': '12345678' | ||
} | ||
} |
Oops, something went wrong.