Skip to content

Commit

Permalink
Merge f5b4f7e into cb0a8a4
Browse files Browse the repository at this point in the history
  • Loading branch information
AnguleMathias committed Nov 13, 2018
2 parents cb0a8a4 + f5b4f7e commit 8c72344
Show file tree
Hide file tree
Showing 25 changed files with 450 additions and 143 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ designs/
# SQLite3
db.sqlite3

# Ignore articles/renderers for now
authors/apps/articles/renderers.py
# Admin
admin.py
.DS_Store
*.DS_Store
Binary file removed authors/.DS_Store
Binary file not shown.
Binary file removed authors/apps/.DS_Store
Binary file not shown.
23 changes: 21 additions & 2 deletions authors/apps/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Generated by Django 2.1.2 on 2018-11-06 07:54
# Generated by Django 2.1.2 on 2018-11-11 08:49

from django.conf import settings
import django.contrib.postgres.fields
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),
]

operations = [
Expand All @@ -18,16 +21,32 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(db_index=True, max_length=255)),
('body', models.TextField()),
('image', models.ImageField(blank=True, height_field=100, null=True, upload_to='photos/%Y/%m/%d/', width_field=100)),
('images', django.contrib.postgres.fields.ArrayField(base_field=models.TextField(), blank=True, default=None, null=True, size=None)),
('description', models.CharField(max_length=255)),
('slug', models.SlugField(max_length=40, unique=True)),
('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=30), blank=True, default=None, null=True, size=None)),
('time_to_read', models.IntegerField()),
('time_created', models.DateTimeField(auto_now_add=True, db_index=True)),
('time_updated', models.DateTimeField(auto_now=True, db_index=True)),
('average_rating', models.IntegerField(default=0)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('time_created', 'time_updated'),
},
),
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(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.Article')),
('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='comments', to=settings.AUTH_USER_MODEL)),
('dislikes', models.ManyToManyField(blank=True, related_name='comment_dislikes', to=settings.AUTH_USER_MODEL)),
('likes', models.ManyToManyField(blank=True, related_name='comment_likes', to=settings.AUTH_USER_MODEL)),
('parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='thread', to='articles.Comment')),
],
),
]
22 changes: 0 additions & 22 deletions authors/apps/articles/migrations/0002_article_author.py

This file was deleted.

23 changes: 0 additions & 23 deletions authors/apps/articles/migrations/0003_auto_20181107_0615.py

This file was deleted.

18 changes: 0 additions & 18 deletions authors/apps/articles/migrations/0004_article_average_rating.py

This file was deleted.

28 changes: 0 additions & 28 deletions authors/apps/articles/migrations/0005_comment.py

This file was deleted.

12 changes: 7 additions & 5 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'''articles/models.py'''
"""articles/models.py"""
from django.db import models
from django.contrib.postgres.fields import ArrayField
from django.utils.text import slugify
from authors.apps.authentication.models import User


class Article(models.Model):
'''Model representing articles'''
"""Model representing articles"""
title = models.CharField(db_index=True, max_length=255)
body = models.TextField()
images = ArrayField(models.TextField(), default=None,
Expand All @@ -24,15 +24,15 @@ class Article(models.Model):
average_rating = models.IntegerField(default=0)

class Meta():
'''Meta class defining order'''
"""Meta class defining order"""
ordering = ('time_created', 'time_updated',)

def save(self, *args, **kwargs):
'''override save from super'''
"""override save from super"""
super(Article, self).save(*args, **kwargs)

def __str__(self):
'''return string representation of object'''
"""return string representation of object"""
return self.title


Expand All @@ -53,6 +53,8 @@ class Comment(models.Model):
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
likes = models.ManyToManyField(User, related_name='comment_likes', blank=True)
dislikes = models.ManyToManyField(User, related_name='comment_dislikes', blank=True)

def __str__(self):
return self.body
26 changes: 25 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class CommentSerializer(serializers.ModelSerializer):
author = serializers.SerializerMethodField()
article = serializers.ReadOnlyField(source='article.title')
thread = RecursiveSerializer(many=True, read_only=True)
likes = serializers.SerializerMethodField(method_name='count_likes')
dislikes = serializers.SerializerMethodField(method_name='count_dislikes')

class Meta:
model = Comment
Expand All @@ -133,7 +135,9 @@ class Meta:
'author',
'thread',
'created_at',
'updated_at'
'updated_at',
'likes',
'dislikes'
)

def update(self, instance, valid_input, **kwargs):
Expand All @@ -160,3 +164,23 @@ def create(self, valid_input):
parent = self.context.get('parent', None)
instance = Comment.objects.create(parent=parent, **valid_input)
return instance

def count_likes(self, instance):
"""Returns the total likes of a comment"""
request = self.context.get('request')
liked_by_me = False
if request is not None and request.user.is_authenticated:
user_id = request.user.id
liked_by_me = instance.likes.all().filter(id=user_id).count() == 1
return {'count': instance.likes.count(), 'me': liked_by_me}

def count_dislikes(self, instance):
"""Returns the total dislikes of a a comment"""
request = self.context.get('request')
disliked_by_me = False
if request is not None and request.user.is_authenticated:
user_id = request.user.id
disliked_by_me = instance.dislikes.all().filter(
id=user_id).count() == 1
return {'count': instance.dislikes.count(), 'me': disliked_by_me}

7 changes: 7 additions & 0 deletions authors/apps/articles/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def setUp(self):
self.UPDATE_A_COMMENT = '/api/articles/{}/comment/{}/'
self.DELETE_A_COMMENT = '/api/articles/{}/comment/{}/'

self.LIKE_COMMENT = '/api/articles/{}/comment/{}/like/'
self.DISLIKE_COMMENT = '/api/articles/{}/comment/{}/dislike/'

self.client = APIClient()
test_user = {
"email": "njery.ngigi@gmail.com",
Expand All @@ -52,6 +55,10 @@ def setUp(self):
"tags": ["TDD"]
}

self.test_comment_data = {
"body": "this is a sample comment"
}

self.client.post(self.SIGN_UP_URL, test_user, format="json")
response = self.client.post(self.SIGN_IN_URL, test_user, format="json")
self.token = "bearer " + json.loads(response.content)["user"]["token"]
Expand Down
Loading

0 comments on commit 8c72344

Please sign in to comment.