--- a/src/jocondelab/views/front_office.py Wed Oct 30 11:55:33 2013 +0100
+++ b/src/jocondelab/views/front_office.py Thu Oct 31 12:42:46 2013 +0100
@@ -6,16 +6,18 @@
'''
from core.models import (Notice, Thesaurus, Term)
-from jocondelab.models import (DbpediaFields, Country, ContributableTerm, TagcloudTerm)
+from django.conf import settings
+from django.core.cache import cache
from django.core.paginator import Paginator
-import django.utils.simplejson as json
-from django.conf import settings
+from django.db.models import Sum, Count
+from django.db.models.query import prefetch_related_objects
+from django.shortcuts import redirect
+from django.utils.http import urlencode
+from django.utils.translation import ugettext
from django.views.generic import DetailView, TemplateView
-from django.db.models import Sum, Count
+from jocondelab.models import (DbpediaFields, Country, ContributableTerm, TagcloudTerm)
+import django.utils.simplejson as json
import random
-from django.shortcuts import redirect
-from django.utils.translation import ugettext
-from django.utils.http import urlencode
import logging
logger = logging.getLogger(__name__)
@@ -80,11 +82,25 @@
qs = Notice.objects.prefetch_related("images").filter(image=True)
if emptysearch:
- context["count"] = qs.count()
+ if not cache.get('notice_count'):
+ cache.set('notice_count', qs.count(), settings.DB_QUERY_CACHE_TIME)
+ context["count"] = cache.get('notice_count')
# Optimize random : order_by('?') is too slow
- #ns = qs.order_by('?')[:npp]
- rdm = random.randint(0, context["count"] - npp - 1)
- ns = qs[rdm:rdm+npp]
+ # generate_series(1, 100) and not generate_series(1, 12) to be sure we have existing ids
+ orm_request = """
+SELECT "core_notice"."id", "core_notice"."ref", "core_notice"."adpt", "core_notice"."appl", "core_notice"."aptn", "core_notice"."attr", "core_notice"."autr", "core_notice"."bibl", "core_notice"."comm", "core_notice"."contact", "core_notice"."coor", "core_notice"."copy", "core_notice"."dacq", "core_notice"."data", "core_notice"."dation", "core_notice"."ddpt", "core_notice"."decv", "core_notice"."deno", "core_notice"."depo", "core_notice"."desc", "core_notice"."desy", "core_notice"."dims", "core_notice"."dmaj", "core_notice"."dmis", "core_notice"."domn", "core_notice"."drep", "core_notice"."ecol", "core_notice"."epoq", "core_notice"."etat", "core_notice"."expo", "core_notice"."gene", "core_notice"."geohi", "core_notice"."hist", "core_notice"."image", "core_notice"."insc", "core_notice"."inv", "core_notice"."label", "core_notice"."labo", "core_notice"."lieux", "core_notice"."loca", "core_notice"."loca2", "core_notice"."mill", "core_notice"."milu", "core_notice"."mosa", "core_notice"."msgcom", "core_notice"."museo", "core_notice"."nsda", "core_notice"."onom", "core_notice"."paut", "core_notice"."pdat", "core_notice"."pdec", "core_notice"."peoc", "core_notice"."peri", "core_notice"."peru", "core_notice"."phot", "core_notice"."pins", "core_notice"."plieux", "core_notice"."prep", "core_notice"."puti", "core_notice"."reda", "core_notice"."refim", "core_notice"."repr", "core_notice"."srep", "core_notice"."stat", "core_notice"."tech", "core_notice"."tico", "core_notice"."titr", "core_notice"."util", "core_notice"."video", "core_notice"."www"
+FROM (
+ SELECT 1 + floor(random() * %i)::integer AS id
+ FROM generate_series(1, 100) g
+ GROUP BY 1
+ ) r
+JOIN "core_notice" USING (id)
+WHERE "core_notice"."image" = true
+LIMIT 12;
+ """
+ ns = list(Notice.objects.raw(orm_request % context["count"]))
+ # list because prefetch_related_objects needs list and not QuerySet or RawQuerySet
+ prefetch_related_objects(ns, ['images'])
else:
uri_cache = {}
if dbpedia_uris: