From bf62d083d818f074522f7fb095f93987afbb5bdc Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Wed, 12 Sep 2012 09:31:21 +0200 Subject: Improve the user voting display (only one SQL query) --- hyperkitty/models.py | 20 -------------------- hyperkitty/templates/user_profile.html | 13 +++++-------- hyperkitty/views/accounts.py | 26 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 30 deletions(-) (limited to 'hyperkitty') diff --git a/hyperkitty/models.py b/hyperkitty/models.py index 6a8bd63..c84ef51 100644 --- a/hyperkitty/models.py +++ b/hyperkitty/models.py @@ -53,26 +53,6 @@ class UserProfile(models.Model): karma = models.IntegerField(default=1) - def _get_votes(self): - "Returns all the votes by a user" - # Extract all the votes by this user - try: - votes = Rating.objects.filter(user=self.user) - except Rating.DoesNotExist: - votes = {} - - # TODO: warning, not thread-safe, should get the cached connection from - # the WSGI environment - store = get_store(settings.KITTYSTORE_URL) - for vote in votes: - list_name = vote.list_address.split('@')[0] - message = store.get_message_by_id_from_list(vote.list_address, vote.messageid) - vote.message = message - - return votes - - votes = property(_get_votes) - def __unicode__(self): """Unicode representation""" return u'%s' % (unicode(self.user)) diff --git a/hyperkitty/templates/user_profile.html b/hyperkitty/templates/user_profile.html index 3077141..8f73e81 100644 --- a/hyperkitty/templates/user_profile.html +++ b/hyperkitty/templates/user_profile.html @@ -33,12 +33,12 @@ + {% if settings.USE_MOCKUPS %} -

Up Votes :

+

Up Votes :

Down Votes :

- + {% endif %} diff --git a/hyperkitty/views/accounts.py b/hyperkitty/views/accounts.py index 2c68315..49d726f 100644 --- a/hyperkitty/views/accounts.py +++ b/hyperkitty/views/accounts.py @@ -29,7 +29,7 @@ from django.contrib.auth.decorators import (login_required, permission_required, user_passes_test) from django.contrib.auth.forms import AuthenticationForm -from hyperkitty.models import UserProfile +from hyperkitty.models import UserProfile, Rating from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.http import HttpResponse, HttpResponseRedirect @@ -41,6 +41,8 @@ from urlparse import urlparse from forms import RegistrationForm from hyperkitty.utils import log +from hyperkitty.lib import get_store + def user_logout(request): logout(request) @@ -74,6 +76,7 @@ def user_login(request, template='login.html'): def user_profile(request, user_email=None): if not request.user.is_authenticated(): return redirect('user_login') + t = loader.get_template('user_profile.html') # try to render the user profile. try: @@ -82,10 +85,29 @@ def user_profile(request, user_email=None): except: user_profile = UserProfile.objects.create(user=request.user) - t = loader.get_template('user_profile.html') + try: + votes = Rating.objects.filter(user=request.user) + except Rating.DoesNotExist: + votes = {} + store = get_store(request) + votes_up = [] + votes_down = [] + for vote in votes: + message = store.get_message_by_id_from_list( + vote.list_address, vote.message_id) + vote_data = {"list_address": vote.list_address, + "message_id": vote.message_id, + "message": message, + } + if vote.vote == 1: + votes_up.append(vote_data) + elif vote.vote == -1: + votes_down.append(vote_data) c = RequestContext(request, { 'user_profile' : user_profile, + 'votes_up': votes_up, + 'votes_down': votes_down, }) return HttpResponse(t.render(c)) -- cgit