Skip to content

Commit

Permalink
feat-users-like-comments-163383199 (#48)
Browse files Browse the repository at this point in the history
- users can like/dislike comments
- users can un-like/un-dislike comments
- users have to be authenticated to perfom like/dislike
- users cannot like their comments

[Starts #163383199]
  • Loading branch information
pbkabali authored and malep2007 committed Feb 22, 2019
1 parent fa707f0 commit 7ca9a7c
Show file tree
Hide file tree
Showing 15 changed files with 691 additions and 67 deletions.
2 changes: 1 addition & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ class ArticleReporterSerializer(serializers.ModelSerializer):

class Meta:
model = models.Report
fields = ('reporter', 'article', 'reason',)
fields = ('id', 'reporter', 'article', 'reason',)
29 changes: 22 additions & 7 deletions authors/apps/comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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, null=True)),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='articles.Article')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
('updated_at', models.DateTimeField(
auto_now=True,
null=True)),
('article', models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='articles.Article')),
('author', models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='profiles.Profile')),
],
options={
'ordering': ['created_at'],
Expand All @@ -31,12 +40,18 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='CommentReply',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='comments.Comment')),
('author', models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='profiles.Profile')),
('comment', models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='comments.Comment')),
],
options={
'ordering': ['created_at'],
Expand Down
29 changes: 29 additions & 0 deletions authors/apps/comments/migrations/0003_auto_20190220_0230.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.1.5 on 2019-02-19 23:30

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('comments', '0002_auto_20190211_1918'),
]

operations = [
migrations.AddField(
model_name='comment',
name='disliked_by',
field=models.ManyToManyField(
related_name='disliked_comments',
to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='comment',
name='liked_by',
field=models.ManyToManyField(
related_name='liked_comments',
to=settings.AUTH_USER_MODEL),
),
]
14 changes: 14 additions & 0 deletions authors/apps/comments/migrations/0004_merge_20190221_1355.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.1.5 on 2019-02-21 10:55

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('comments', '0003_historicalcomment_historicalcommentreply'),
('comments', '0003_auto_20190220_0230'),
]

operations = [
]
14 changes: 14 additions & 0 deletions authors/apps/comments/migrations/0006_merge_20190222_1010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.1.5 on 2019-02-22 07:10

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('comments', '0005_historicalcomment_commenting_on'),
('comments', '0004_merge_20190221_1355'),
]

operations = [
]
12 changes: 12 additions & 0 deletions authors/apps/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Comment(models.Model):
author = models.ForeignKey(Profile, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
edit_history = HistoricalRecords()
liked_by = models.ManyToManyField(to=settings.AUTH_USER_MODEL,
related_name='liked_comments')
disliked_by = models.ManyToManyField(to=settings.AUTH_USER_MODEL,
related_name='disliked_comments')

class Meta:
"""
Expand All @@ -37,6 +41,14 @@ def url(self):
return settings.URL + reverse('comments:comment-details',
kwargs={"pk": self.pk})

@property
def like_count(self):
return self.liked_by.count()

@property
def dislike_count(self):
return self.disliked_by.count()


class CommentReply(models.Model):
"""The model defines the Comment-Replies table as stored in the DB
Expand Down
2 changes: 2 additions & 0 deletions authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Meta:
"article",
"id",
'commenting_on',
"like_count",
"dislike_count"
)
read_only_fields = (
"article",
Expand Down
24 changes: 23 additions & 1 deletion authors/apps/comments/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db.models.signals import post_save
from django.db.models.signals import (
post_save, m2m_changed as model_field_changed_signal)
from django.dispatch import receiver, Signal

from authors.apps.comments.models import Comment
Expand All @@ -7,6 +8,9 @@
# custom signal we shall send when a comment is published
# the rationale for this custom signal is discussed in the articles app
comment_published_signal = Signal(providing_args=["comment"])
comment_liked_signal = Signal(
providing_args=["comment", "user_model", "id"]
)


class CommentsSignalSender:
Expand All @@ -18,3 +22,21 @@ def on_comment_post_save(sender, **kwargs):
if kwargs['created']:
comment_published_signal.send(CommentsSignalSender,
comment=kwargs['instance'])


@receiver(model_field_changed_signal, sender=Comment.liked_by.through)
def on_like_comment(sender, **kwargs):
"""
on_like_comment is run when a user likes a comment. Then calls a signal
to notify the user
"""
comment = kwargs.get("instance")
user_model = kwargs.get("model")
action = kwargs.get("action")
pk_set = kwargs.get("pk_set")
if action == "post_add":
user_id = [pk for pk in pk_set]
comment_liked_signal.send(CommentsSignalSender,
comment=comment,
user_model=user_model,
id=user_id[0])
Loading

0 comments on commit 7ca9a7c

Please sign in to comment.