cleanup views/accessed/populariy

This commit is contained in:
rolux 2011-01-24 02:35:52 +00:00
parent 0bffcaa421
commit 80e1879a5b
3 changed files with 20 additions and 15 deletions

View file

@ -7,7 +7,7 @@ import os
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.conf import settings 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.django.shortcuts import render_to_json_response, json_response
from ox.utils import json from ox.utils import json
@ -65,7 +65,12 @@ def init(request):
#populate max values for percent requests #populate max values for percent requests
for key in filter(lambda k: 'format' in k, config['itemKeys']): for key in filter(lambda k: 'format' in k, config['itemKeys']):
if key['format']['type'] == 'percent' and key['format']['args'][0] == 'auto': 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 key['format']['args'][0] = value
response['data']['config'] = config response['data']['config'] = config

View file

@ -8,6 +8,7 @@ import subprocess
from glob import glob from glob import glob
from django.db import models from django.db import models
from django.db.models import Sum
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.utils import simplejson as json from django.utils import simplejson as json
from django.conf import settings from django.conf import settings
@ -544,6 +545,7 @@ class Item(models.Model):
s.cutsperminute = s.cuts / (s.duration/60) s.cutsperminute = s.cuts / (s.duration/60)
else: else:
s.cutsperminute = None s.cutsperminute = None
s.popularity = self.accessed.aggregate(Sum('accessed'))['accessed__sum']
s.save() s.save()

View file

@ -51,9 +51,9 @@ def _order_query(qs, sort, prefix='sort__'):
key = { key = {
'id': 'itemId', 'id': 'itemId',
'accessed': 'accessed__access', 'accessed': 'accessed__access',
'viewed': 'accessed__access' 'viewed': 'accessed__access',
}.get(e['key'], e['key']) }.get(e['key'], e['key'])
if key not in ('accessed__access', ): if key not in ('accessed__access', 'accessed__accessed'):
key = "%s%s" % (prefix, key) key = "%s%s" % (prefix, key)
order = '%s%s' % (operator, key) order = '%s%s' % (operator, key)
order_by.append(order) order_by.append(order)
@ -198,10 +198,13 @@ Positions
def only_p_sums(m): def only_p_sums(m):
r = {} r = {}
for p in _p: for p in _p:
if p in ('accessed', 'viewed'): if p == 'viewed' and request.user.is_authenticated():
r[p] = getattr(m, 'viewed') 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': elif p == 'popularity':
r[p] = getattr(m, p) r[p] = m.sort.popularity
else: else:
r[p] = m.json.get(p, '') r[p] = m.json.get(p, '')
if isinstance(r[p], datetime): if isinstance(r[p], datetime):
@ -214,18 +217,13 @@ Positions
for p in _p: for p in _p:
r[p] = m.get(p, '') r[p] = m.get(p, '')
return r 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]] qs = qs[query['range'][0]:query['range'][1]]
#response['data']['items'] = [m.get_json(_p) for m in qs] #response['data']['items'] = [m.get_json(_p) for m in qs]
if 'popularity' in _p: if 'popularity' in _p:
qs = qs.annotate(popularity=Sum('accessed__accessed')) qs = qs.annotate(popularity=Sum('accessed__accessed'))
response['data']['items'] = [only_p_sums(m) for m in qs] if 'accessed' in _p:
elif 'viewed' in _p and request.user.is_authenticated(): qs = qs.annotate(a=Max('accessed__access'))
qs = qs.annotate(viewed=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]
elif 'accessed' in _p:
qs = qs.annotate(viewed=Max('accessed__access'))
response['data']['items'] = [only_p_sums(m) for m in qs] response['data']['items'] = [only_p_sums(m) for m in qs]
else: else:
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')] response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]