From eb61f626393837a59c7d5adaee05fb0c65bfce25 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 11 Nov 2011 18:45:46 +0100 Subject: [PATCH] fix last accessed --- pandora/item/models.py | 6 ++++-- pandora/item/views.py | 11 +++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 52a3b9d3f..be10eabb4 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -14,7 +14,7 @@ import unicodedata from urllib import quote from django.db import models, transaction -from django.db.models import Count, Q, Sum +from django.db.models import Count, Q, Sum, Max from django.core.files.base import ContentFile from django.conf import settings from django.contrib.auth.models import User, Group @@ -605,6 +605,7 @@ class Item(models.Model): 'parts', 'pixels', 'timesaccessed', + 'accessed', 'resolution', 'width', 'height', @@ -720,6 +721,7 @@ class Item(models.Model): s.timesaccessed = self.accessed.aggregate(Sum('accessed'))['accessed__sum'] if not s.timesaccessed: s.timesaccessed = 0 + s.accessed = self.accessed.aggregate(Max('access'))['access__max'] s.save() #update cached values in clips self.clips.all().update(director=s.director, title=s.title) @@ -1208,7 +1210,7 @@ class Access(models.Model): self.accessed += 1 super(Access, self).save(*args, **kwargs) timesaccessed = Access.objects.filter(item=self.item).aggregate(Sum('accessed'))['accessed__sum'] - ItemSort.objects.filter(item=self.item).update(timesaccessed=timesaccessed) + ItemSort.objects.filter(item=self.item).update(timesaccessed=timesaccessed, accessed=self.access) def __unicode__(self): if self.user: diff --git a/pandora/item/views.py b/pandora/item/views.py index eb22460f0..6983e80cc 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -52,9 +52,8 @@ def _order_query(qs, sort, prefix='sort__'): operator = '' key = { 'id': 'itemId', - 'accessed': 'accessed__access', }.get(e['key'], e['key']) - if key not in ('accessed__access', 'accessed__accessed'): + if key not in ('itemId', ): key = "%s%s" % (prefix, key) order = '%s%s' % (operator, key) order_by.append(order) @@ -237,9 +236,9 @@ Positions r = {} for p in _p: if p == 'accessed': - r[p] = m.a + r[p] = m.sort.accessed elif p == 'timesaccessed': - r[p] = m.timesaccessed + r[p] = m.sort.timesaccessed else: r[p] = m.json.get(p, '') if 'clip_qs' in query: @@ -256,10 +255,6 @@ Positions return r qs = qs[query['range'][0]:query['range'][1]] #response['data']['items'] = [m.get_json(_p) for m in qs] - if 'timesaccessed' in _p: - qs = qs.annotate(timesaccessed=Sum('accessed__accessed')) - if 'accessed' in _p: - qs = qs.annotate(a=Max('accessed__access')) if 'viewed' in _p or 'timesaccessed' in _p or 'accessed' in _p: qs = qs.select_related() response['data']['items'] = [only_p_sums(m) for m in qs]