diff --git a/pandora/item/models.py b/pandora/item/models.py index 52a3b9d3..be10eabb 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 eb22460f..86bb1f41 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 or '' 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] diff --git a/pandora/templates/index.html b/pandora/templates/index.html index 39a5640b..82091243 100644 --- a/pandora/templates/index.html +++ b/pandora/templates/index.html @@ -4,18 +4,10 @@