Skip to content

Commit

Permalink
feat(comments-crud): Add create, read, update and delete comment
Browse files Browse the repository at this point in the history
- Add create comment
- Add read comments
- Add read comment
- Add update comment
- Add delete comment
  • Loading branch information
mwinel committed May 3, 2019
1 parent 8e82eae commit 6d629f0
Show file tree
Hide file tree
Showing 18 changed files with 494 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var/
*.egg
.idea/
.vscode/*
.DS_Store/*
authors/.DS_Store
*.DS_Store
authors/*.DS_Store

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
4 changes: 3 additions & 1 deletion authors/apps/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 2.1 on 2019-05-02 06:27

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


Expand All @@ -24,6 +25,7 @@ class Migration(migrations.Migration):
('updated_at', models.DateTimeField(default=django.utils.timezone.now)),
('favorited', models.BooleanField(default=False)),
('favorite_count', models.IntegerField(default=0)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username')),
],
),
]
23 changes: 0 additions & 23 deletions authors/apps/articles/migrations/0002_article_author.py

This file was deleted.

17 changes: 16 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .models import Article
from .validations import ValidateArticleCreation
from authors.apps.comments.serializers import CommentSerializer


class ArticleSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -47,7 +48,21 @@ def validate(self, data):
class Meta:
"""class for returning our field."""

comments = CommentSerializer(many=True)

model = Article
fields = '__all__'
fields = (
'id',
'title',
'description',
'body',
'slug',
'created_at',
'updated_at',
'favorited',
'favorite_count',
'comments'
)
depth = 1
read_only_fields = ('created_at', 'updated_at', 'author',
'favorited', 'favorite_count', 'slug')
Empty file.
3 changes: 3 additions & 0 deletions authors/apps/comments/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions authors/apps/comments/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CommentsConfig(AppConfig):
name = 'comments'
29 changes: 29 additions & 0 deletions authors/apps/comments/migrations/0001_initial.py
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.
25 changes: 25 additions & 0 deletions authors/apps/comments/models.py
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
18 changes: 18 additions & 0 deletions authors/apps/comments/serializers.py
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.
73 changes: 73 additions & 0 deletions authors/apps/comments/tests/base.py
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'
}
}
Loading

0 comments on commit 6d629f0

Please sign in to comment.