Revert "added comment, topic and likes counts to profile page"

This commit is contained in:
Esteban Castro Borsani 2016-02-21 23:38:47 -03:00
parent 97a8f7f915
commit d93ab3c7e2
9 changed files with 15 additions and 204 deletions

View File

@ -21,7 +21,7 @@ def create(request, comment_id):
if form.is_valid():
like = form.save()
like.comment.increase_likes_count(request.user)
like.comment.increase_likes_count()
if request.is_ajax():
return json_response({'url_delete': like.get_delete_url(), })
@ -44,7 +44,7 @@ def delete(request, pk):
if request.method == 'POST':
like.delete()
like.comment.decrease_likes_count(request.user)
like.comment.decrease_likes_count()
if request.is_ajax():
url = reverse('spirit:comment:like:create', kwargs={'comment_id': like.comment.pk, })

View File

@ -3,7 +3,6 @@
from __future__ import unicode_literals
from django.db import models
from django.db.models.signals import post_save, post_delete
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.conf import settings
@ -12,6 +11,7 @@ from django.utils import timezone
from .managers import CommentQuerySet
COMMENT_MAX_LEN = 3000 # changing this needs migration
COMMENT, MOVED, CLOSED, UNCLOSED, PINNED, UNPINNED = range(6)
@ -66,21 +66,15 @@ class Comment(models.Model):
.filter(pk=self.pk)\
.update(modified_count=F('modified_count') + 1)
def increase_likes_count(self, acting_user):
def increase_likes_count(self):
Comment.objects\
.filter(pk=self.pk)\
.update(likes_count=F('likes_count') + 1)
if not self.topic.category.is_private:
self.user.st.increase_received_likes_count()
acting_user.st.increase_given_likes_count()
def decrease_likes_count(self, acting_user):
def decrease_likes_count(self):
Comment.objects\
.filter(pk=self.pk)\
.update(likes_count=F('likes_count') - 1)
if not self.topic.category.is_private:
self.user.st.decrease_received_likes_count()
acting_user.st.decrease_given_likes_count()
@classmethod
def create_moderation_action(cls, user, topic, action):
@ -92,17 +86,3 @@ class Comment(models.Model):
comment="action",
comment_html="action"
)
def increase_user_profile_comment_count(sender, instance, created, **kwargs):
if created and not instance.topic.category.is_private:
instance.user.st.increase_comment_count()
post_save.connect(increase_user_profile_comment_count, sender=Comment, dispatch_uid='Comment:increase_user_profile_comment_count')
def decrease_user_profile_comment_count(sender, instance, **kwargs):
if not instance.topic.category.is_private:
instance.user.st.decrease_comment_count()
post_delete.connect(decrease_user_profile_comment_count, sender=Comment, dispatch_uid='Comment:decrease_user_profile_comment_count')

View File

@ -396,31 +396,6 @@ class CommentViewTest(TestCase):
self.assertIn('error', res.keys())
self.assertIn('image', res['error'].keys())
def test_comment_publish_count(self):
"""
Creating or deleting a comment updates the profile comment_count
"""
utils.login(self)
form_data = {'comment': 'foobar', }
response = self.client.post(reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk, }),
form_data)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 1)
Comment.objects.get(user=self.user).delete()
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 0)
def test_private_comment_publish_count(self):
"""
Creating a private comment does not update the profile comment_count
"""
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 1) # setUp creates a topic
private = utils.create_private_topic(user=self.user)
utils.login(self)
form_data = {'comment': 'foobar', }
response = self.client.post(reverse('spirit:comment:publish', kwargs={'topic_id': private.topic.pk, }),
form_data)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 0)
class CommentModelsTest(TestCase):
@ -438,39 +413,21 @@ class CommentModelsTest(TestCase):
comment.increase_modified_count()
self.assertEqual(Comment.objects.get(pk=comment.pk).modified_count, 1)
def test_comment_likes_count(self):
def test_comment_increase_likes_count(self):
"""
Increase/Decrease like_count on comment like
Increase like_count on comment like
"""
comment = utils.create_comment(topic=self.topic)
comment.increase_likes_count(self.user)
comment.increase_likes_count()
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).given_likes_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).received_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=comment.user.st.pk).given_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=comment.user.st.pk).received_likes_count, 1)
comment.decrease_likes_count(self.user)
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).given_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).received_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=comment.user.st.pk).given_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=comment.user.st.pk).received_likes_count, 0)
def test_private_comment_likes_count(self):
def test_comment_decrease_likes_count(self):
"""
Private comments do not affect the like count
Decrease like_count on remove comment like
"""
private = utils.create_private_topic(user=self.user)
comment = utils.create_comment(topic=private.topic)
comment.increase_likes_count(self.user)
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).given_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).received_likes_count, 0)
comment.decrease_likes_count(self.user)
comment = utils.create_comment(topic=self.topic, likes_count=1)
comment.decrease_likes_count()
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).given_likes_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).received_likes_count, 0)
def test_comment_create_moderation_action(self):
"""

View File

@ -3,7 +3,6 @@
from __future__ import unicode_literals
from django.db import models
from django.db.models.signals import post_save, post_delete
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.conf import settings
@ -101,17 +100,3 @@ class Topic(models.Model):
Topic.objects\
.filter(pk=self.pk)\
.update(comment_count=F('comment_count') - 1)
def increase_user_profile_comment_count(sender, instance, created, **kwargs):
if created and not instance.category.is_private:
instance.user.st.increase_topic_count()
post_save.connect(increase_user_profile_comment_count, sender=Topic, dispatch_uid='Topic:increase_user_profile_comment_count')
def decrease_user_profile_comment_count(sender, instance, **kwargs):
if not instance.category.is_private:
instance.user.st.decrease_topic_count()
post_delete.connect(decrease_user_profile_comment_count, sender=Topic, dispatch_uid='Topic:decrease_user_profile_comment_count')

View File

@ -19,7 +19,6 @@ from .forms import TopicForm
from ..comment.models import Comment
from ..comment.bookmark.models import CommentBookmark
from .notification.models import TopicNotification
from spirit.user.models import UserProfile
from .unread.models import TopicUnread
@ -320,29 +319,6 @@ class TopicViewTest(TestCase):
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_b, ])
def test_topic_publish_count(self):
"""
Creating or deleting a topic updates the profile topic_count and comment_count
"""
utils.login(self)
category = utils.create_category()
form_data = {'comment': 'foo', 'title': 'foobar', 'category': category.pk}
response = self.client.post(reverse('spirit:topic:publish'),
form_data)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 1)
Topic.objects.get(user=self.user).delete()
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 0)
def test_private_topic_publish_count(self):
"""
Creating a private topic does not update the profile topic_count and comment_count
"""
utils.create_private_topic(user=self.user)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 0)
class TopicFormTest(TestCase):

View File

@ -1,24 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('spirit_user', '0004_auto_20150731_2351'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='given_likes_count',
field=models.PositiveIntegerField(default=0, verbose_name='given likes count'),
),
migrations.AddField(
model_name='userprofile',
name='received_likes_count',
field=models.PositiveIntegerField(default=0, verbose_name='received likes count'),
),
]

View File

@ -1,32 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
def calculate_user_profile_stats(apps, schema_editor):
UserProfile = apps.get_model("spirit_user", "UserProfile")
Topic = apps.get_model("spirit_topic", "Topic")
Comment = apps.get_model("spirit_comment", "Comment")
CommentLike = apps.get_model("spirit_comment_like", "CommentLike")
for profile in UserProfile.objects.all():
profile.topic_count = Topic.objects.filter(category__is_private=False, user__st=profile).count()
profile.comment_count = Comment.objects.filter(topic__category__is_private=False, user__st=profile).count()
profile.given_likes_count = CommentLike.objects.filter(comment__topic__category__is_private=False, user__st=profile).count()
profile.received_likes_count = CommentLike.objects.filter(comment__topic__category__is_private=False, comment__user__st=profile).count()
profile.save()
def backwards_migration_noop(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('spirit_user', '0005_auto_20151206_1214'),
]
operations = [
migrations.RunPython(calculate_user_profile_stats, backwards_migration_noop),
]

View File

@ -3,7 +3,6 @@
from __future__ import unicode_literals
from django.db import models
from django.db.models import F
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
@ -30,8 +29,6 @@ class UserProfile(models.Model):
topic_count = models.PositiveIntegerField(_("topic count"), default=0)
comment_count = models.PositiveIntegerField(_("comment count"), default=0)
given_likes_count = models.PositiveIntegerField(_("given likes count"), default=0)
received_likes_count = models.PositiveIntegerField(_("received likes count"), default=0)
class Meta:
verbose_name = _("forum profile")
@ -49,30 +46,6 @@ class UserProfile(models.Model):
def get_absolute_url(self):
return reverse('spirit:user:detail', kwargs={'pk': self.user.pk, 'slug': self.slug})
def increase_comment_count(self):
UserProfile.objects.filter(pk=self.pk).update(comment_count=F('comment_count') + 1)
def decrease_comment_count(self):
UserProfile.objects.filter(pk=self.pk).update(comment_count=F('comment_count') - 1)
def increase_topic_count(self):
UserProfile.objects.filter(pk=self.pk).update(topic_count=F('topic_count') + 1)
def decrease_topic_count(self):
UserProfile.objects.filter(pk=self.pk).update(topic_count=F('topic_count') - 1)
def increase_given_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(given_likes_count=F('given_likes_count') + 1)
def decrease_given_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(given_likes_count=F('given_likes_count') - 1)
def increase_received_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(received_likes_count=F('received_likes_count') + 1)
def decrease_received_likes_count(self):
UserProfile.objects.filter(pk=self.pk).update(received_likes_count=F('received_likes_count') - 1)
class User(AbstractUser):
# Backward compatibility

View File

@ -15,10 +15,6 @@
--><li>
<div class="profile-title">{% trans "Seen" %}</div>
<div class="profile-date">{{ p_user.st.last_seen|shortnaturaltime }}</div>
</li><!--
--><li>
<div class="profile-title">{% trans "Likes received" %}</div>
<div class="profile-date">{{ p_user.st.received_likes_count }}</div>
</li>
{% if user.st.is_administrator %}
@ -47,7 +43,7 @@
{% endif %}
<ul class="tabs">
<li><a class="tab-link{% ifequal active_tab 0 %} is-selected{% endifequal %}" href="{% url "spirit:user:detail" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Comments" %} ({{ p_user.st.comment_count }})</a></li><!--
--><li><a class="tab-link{% ifequal active_tab 1 %} is-selected{% endifequal %}" href="{% url "spirit:user:topics" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Topics" %} ({{ p_user.st.topic_count }})</a></li><!--
--><li><a class="tab-link{% ifequal active_tab 2 %} is-selected{% endifequal %}" href="{% url "spirit:user:likes" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Likes" %} ({{ p_user.st.given_likes_count }})</a></li>
<li><a class="tab-link{% ifequal active_tab 0 %} is-selected{% endifequal %}" href="{% url "spirit:user:detail" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Comments" %}</a></li><!--
--><li><a class="tab-link{% ifequal active_tab 1 %} is-selected{% endifequal %}" href="{% url "spirit:user:topics" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Topics" %}</a></li><!--
--><li><a class="tab-link{% ifequal active_tab 2 %} is-selected{% endifequal %}" href="{% url "spirit:user:likes" pk=p_user.pk slug=p_user.st.slug %}" >{% trans "Likes" %}</a></li>
</ul>