From b55e1db153101a4868b1bbd7804c86a28b557f82 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 20 Mar 2012 19:38:18 +0100 Subject: [PATCH] save numberoflists in session table, fixes #687 --- pandora/itemlist/views.py | 3 +++ pandora/user/models.py | 7 +++++-- pandora/user/tasks.py | 9 +++++++++ pandora/user/views.py | 4 +--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pandora/itemlist/views.py b/pandora/itemlist/views.py index d1408e053..24e79d13f 100644 --- a/pandora/itemlist/views.py +++ b/pandora/itemlist/views.py @@ -20,6 +20,7 @@ import models from ox.django.api import actions from item import utils from item.models import Item +from user.tasks import update_numberoflists def get_list_or_404_json(id): id = id.split(':') @@ -251,6 +252,7 @@ def addList(request): list.poster_frames = tuple(data['posterFrames']) list.save() + update_numberoflists.delay(request.user.username) if 'items' in data: for item in Item.objects.filter(itemId__in=data['items']): @@ -401,6 +403,7 @@ def removeList(request): response = json_response() if list.editable(request.user): list.delete() + update_numberoflists.delay(request.user.username) else: response = json_response(status=403, text='not allowed') return render_to_json_response(response) diff --git a/pandora/user/models.py b/pandora/user/models.py index 683ae9f90..533657268 100644 --- a/pandora/user/models.py +++ b/pandora/user/models.py @@ -37,6 +37,8 @@ class SessionData(models.Model): system = models.CharField(default='', max_length=255) browser = models.CharField(default='', max_length=255) + numberoflists = models.IntegerField(default=0) + objects = managers.SessionDataManager() groupssort = models.CharField(default=None,blank=True,null=True, max_length=255) @@ -101,6 +103,7 @@ class SessionData(models.Model): self.level = self.user.get_profile().level self.firstseen = self.user.date_joined self.groupssort = ''.join([g.name for g in self.user.groups.all()]) + self.numberoflists = self.user.lists.count() else: self.level = 0 self.groupssort = None @@ -163,7 +166,7 @@ class SessionData(models.Model): j['level'] = p.get_level() j['newsletter'] = p.newsletter j['notes'] = p.notes - j['numberoflists'] = self.user.lists.count() + j['numberoflists'] = p.numberoflists if keys: for key in j.keys(): if key not in keys: @@ -294,7 +297,7 @@ def user_json(user, keys=None): 'level': p.get_level(), 'newsletter': p.newsletter, 'notes': p.notes, - 'numberoflists': user.lists.count(), + 'numberoflists': p.numberoflists, 'username': user.username, } if keys: diff --git a/pandora/user/tasks.py b/pandora/user/tasks.py index 1e80c969a..a8bbb1703 100644 --- a/pandora/user/tasks.py +++ b/pandora/user/tasks.py @@ -16,3 +16,12 @@ def parse_data(key): session_data = models.SessionData.objects.get(session_key=key) session_data.parse_data() session_data.save() + +@task(ignore_resulsts=True, queue='default') +def update_numberoflists(username): + user = models.User.objects.get(username=username) + models.SessionData.objects.filter( + user=user + ).update( + numberoflists=user.lists.count() + ) diff --git a/pandora/user/views.py b/pandora/user/views.py index 17c6c22ab..8dd4a347b 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -442,11 +442,9 @@ def order_query(qs, sort): 'timesseen': 'timesseen', 'useragent': 'useragent', 'username': 'username', + 'numberoflists': 'numberoflists', 'windowsize': 'windowsize', }.get(e['key'], 'user__profile__%s'%e['key']) - if key == 'user__profile__numberoflists': - qs = qs.annotate(numberoflists=Sum('user__lists')) - key = 'numberoflists' order = '%s%s' % (operator, key) order_by.append(order) if order_by: