logic with reputation moved from view to profile manager

This commit is contained in:
Igor Yanchenko 2012-03-03 23:24:39 +02:00
parent bcc5bc96e1
commit 3d9d4d739e
2 changed files with 9 additions and 16 deletions

View file

@ -314,6 +314,13 @@ class Reputation(models.Model):
return u'T[%d], FU[%d], TU[%d]: %s' % (self.post.id, self.from_user.id, self.to_user.id, unicode(self.time))
class ProfileManager(models.Manager):
def get_query_set(self):
qs = super(ProfileManager, self).get_query_set()
if forum_settings.REPUTATION_SUPPORT:
qs = qs.extra(select={'reply_total':'Select sum(sign) from djangobb_forum_reputation group by to_user_id'})
return qs
class Profile(models.Model):
user = AutoOneToOneField(User, related_name='forum_profile', verbose_name=_('User'))
status = models.CharField(_('Status'), max_length=30, blank=True)
@ -337,6 +344,8 @@ class Profile(models.Model):
markup = models.CharField(_('Default markup'), max_length=15, default=forum_settings.DEFAULT_MARKUP, choices=MARKUP_CHOICES)
post_count = models.IntegerField(_('Post count'), blank=True, default=0)
objects = ProfileManager()
class Meta:
verbose_name = _('Profile')
verbose_name_plural = _('Profiles')

View file

@ -260,16 +260,6 @@ def show_topic(request, topic_id, full=True):
if request.user.is_authenticated():
topic.update_read(request.user)
posts = topic.posts.all().select_related()
#TODO rewrite
fixed = False
if fixed and forum_settings.REPUTATION_SUPPORT:
replies_list = Reputation.objects.filter(to_user__pk__in=users).values('to_user_id').annotate(Sum('sign'))
replies = {}
for r in replies_list:
replies[r['to_user_id']] = r['sign__sum']
for post in posts:
post.user.forum_profile.reply_total = replies.get(post.user.id, 0)
initial = {}
if request.user.is_authenticated():
@ -540,12 +530,6 @@ def delete_posts(request, topic_id):
posts = topic.posts.all().select_related()
profiles = Profile.objects.filter(user__pk__in=set(x.user.id for x in posts))
profiles = dict((x.user_id, x) for x in profiles)
for post in posts:
post.user.forum_profile = profiles[post.user.id]
initial = {}
if request.user.is_authenticated():
initial = {'markup': request.user.forum_profile.markup}