Skip to content

Commit

Permalink
added last_commenter field to topic
Browse files Browse the repository at this point in the history
  • Loading branch information
alesdotio committed May 28, 2016
1 parent 9aebbe9 commit ecb1323
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions spirit/comment/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ def test_comment_posted(self):
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 1)
comment = Comment.objects.get(pk=comment.pk) # do not use the cached comment.topic!
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 2)

Expand Down
22 changes: 22 additions & 0 deletions spirit/topic/migrations/0003_topic_last_commenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('spirit_topic', '0002_auto_20150828_2003'),
]

operations = [
migrations.AddField(
model_name='topic',
name='last_commenter',
field=models.ForeignKey(related_name='st_topics_last', on_delete=django.db.models.deletion.SET_NULL, blank=True, to=settings.AUTH_USER_MODEL, null=True),
),
]
28 changes: 28 additions & 0 deletions spirit/topic/migrations/0004_update_last_commenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


def forwards(apps, schema_editor):
Topic = apps.get_model("spirit_topic", "Topic")
for topic in Topic.objects.all():
last_comment = topic.comment_set.filter(is_removed=False).first()
if last_comment:
topic.last_commenter = last_comment.user
topic.save()


def noop(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('spirit_topic', '0003_topic_last_commenter'),
]

operations = [
migrations.RunPython(forwards, reverse_code=noop),
]
9 changes: 9 additions & 0 deletions spirit/topic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Topic(models.Model):
slug = AutoSlugField(populate_from="title", db_index=False, blank=True)
date = models.DateTimeField(_("date"), default=timezone.now)
last_active = models.DateTimeField(_("last active"), default=timezone.now)
last_commenter = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='st_topics_last', null=True, blank=True, on_delete=models.SET_NULL)

is_pinned = models.BooleanField(_("pinned"), default=False)
is_globally_pinned = models.BooleanField(_("globally pinned"), default=False)
Expand Down Expand Up @@ -91,12 +92,20 @@ def increase_view_count(self):
.update(view_count=F('view_count') + 1)

def increase_comment_count(self):
self.update_last_commenter()
Topic.objects\
.filter(pk=self.pk)\
.update(comment_count=F('comment_count') + 1, last_active=timezone.now())

def decrease_comment_count(self):
# todo: update last_active to last() comment
self.update_last_commenter()
Topic.objects\
.filter(pk=self.pk)\
.update(comment_count=F('comment_count') - 1)

def update_last_commenter(self):
last_comment = self.comment_set.filter(is_removed=False).first()
if last_comment and last_comment.user != self.last_commenter:
self.last_commenter = last_comment.user
self.save()
10 changes: 10 additions & 0 deletions spirit/topic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from djconfig.utils import override_djconfig

from spirit.comment.utils import comment_posted
from ..core.tests import utils
from . import utils as utils_topic
from ..comment.models import MOVED
Expand Down Expand Up @@ -430,6 +431,15 @@ def test_topic_decrease_comment_count(self):
self.topic.decrease_comment_count()
self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 9)

def test_topic_last_commenter(self):
"""
update_last_commenter
"""
new_user = utils.create_user()
comment = utils.create_comment(topic=self.topic, user=new_user)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=self.topic.pk).last_commenter, new_user)

def test_topic_new_comments_count(self):
"""
Should return the new replies count
Expand Down

0 comments on commit ecb1323

Please sign in to comment.