From 80e1879a5b684a5df96445247e346e4ccb20d05d Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 24 Jan 2011 02:35:52 +0000 Subject: [PATCH] cleanup views/accessed/populariy --- pandora/api/views.py | 9 +++++++-- pandora/item/models.py | 2 ++ pandora/item/views.py | 24 +++++++++++------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/pandora/api/views.py b/pandora/api/views.py index e28b6b9f..e312743c 100644 --- a/pandora/api/views.py +++ b/pandora/api/views.py @@ -7,7 +7,7 @@ import os from django.shortcuts import render_to_response from django.template import RequestContext from django.conf import settings -from django.db.models import Max +from django.db.models import Max, Sum from ox.django.shortcuts import render_to_json_response, json_response from ox.utils import json @@ -65,7 +65,12 @@ def init(request): #populate max values for percent requests for key in filter(lambda k: 'format' in k, config['itemKeys']): if key['format']['type'] == 'percent' and key['format']['args'][0] == 'auto': - value = ItemSort.objects.aggregate(Max('votes'))['votes__max'] + name = key['id'] + if name == 'popularity': + name = 'item__accessed__accessed' + value = ItemSort.objects.aggregate(Sum(name))['%s__sum'%name] + else: + value = ItemSort.objects.aggregate(Max(name))['%s__max'%name] key['format']['args'][0] = value response['data']['config'] = config diff --git a/pandora/item/models.py b/pandora/item/models.py index 1b9b5529..0a96ae5e 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -8,6 +8,7 @@ import subprocess from glob import glob from django.db import models +from django.db.models import Sum from django.core.files.base import ContentFile from django.utils import simplejson as json from django.conf import settings @@ -544,6 +545,7 @@ class Item(models.Model): s.cutsperminute = s.cuts / (s.duration/60) else: s.cutsperminute = None + s.popularity = self.accessed.aggregate(Sum('accessed'))['accessed__sum'] s.save() diff --git a/pandora/item/views.py b/pandora/item/views.py index 9abce427..25817d62 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -51,9 +51,9 @@ def _order_query(qs, sort, prefix='sort__'): key = { 'id': 'itemId', 'accessed': 'accessed__access', - 'viewed': 'accessed__access' + 'viewed': 'accessed__access', }.get(e['key'], e['key']) - if key not in ('accessed__access', ): + if key not in ('accessed__access', 'accessed__accessed'): key = "%s%s" % (prefix, key) order = '%s%s' % (operator, key) order_by.append(order) @@ -198,10 +198,13 @@ Positions def only_p_sums(m): r = {} for p in _p: - if p in ('accessed', 'viewed'): - r[p] = getattr(m, 'viewed') + if p == 'viewed' and request.user.is_authenticated(): + value = m.accessed.filter(user=request.user).annotate(v=Max('access')) + r[p] = value.exists() and value[0].v or None + elif p == 'accessed': + r[p] = m.a elif p == 'popularity': - r[p] = getattr(m, p) + r[p] = m.sort.popularity else: r[p] = m.json.get(p, '') if isinstance(r[p], datetime): @@ -214,18 +217,13 @@ Positions for p in _p: r[p] = m.get(p, '') return r - if 'viewed' in _p and request.user.is_authenticated(): - qs = qs.filter(accessed__user=request.user) qs = qs[query['range'][0]:query['range'][1]] #response['data']['items'] = [m.get_json(_p) for m in qs] if 'popularity' in _p: qs = qs.annotate(popularity=Sum('accessed__accessed')) - response['data']['items'] = [only_p_sums(m) for m in qs] - elif 'viewed' in _p and request.user.is_authenticated(): - qs = qs.annotate(viewed=Max('accessed__access')) - response['data']['items'] = [only_p_sums(m) for m in qs] - elif 'accessed' in _p: - qs = qs.annotate(viewed=Max('accessed__access')) + if 'accessed' in _p: + qs = qs.annotate(a=Max('accessed__access')) + if 'viewed' in _p or 'popularity' in _p or 'accessed' in _p: response['data']['items'] = [only_p_sums(m) for m in qs] else: response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]