# HG changeset patch # User cavaliet # Date 1383219766 -3600 # Node ID 9136e10ad374850ac782d645c1763ae516b8d895 # Parent 3e9dd7d99ba04ad778b52784bc19e4e0021ef49b random optimisation, menu debug, v0.20 diff -r 3e9dd7d99ba0 -r 9136e10ad374 src/jocondelab/__init__.py --- a/src/jocondelab/__init__.py Wed Oct 30 11:55:33 2013 +0100 +++ b/src/jocondelab/__init__.py Thu Oct 31 12:42:46 2013 +0100 @@ -1,4 +1,4 @@ -VERSION = (0, 19, 0, "final", 0) +VERSION = (0, 20, 0, "final", 0) def get_version(): diff -r 3e9dd7d99ba0 -r 9136e10ad374 src/jocondelab/settings.py --- a/src/jocondelab/settings.py Wed Oct 30 11:55:33 2013 +0100 +++ b/src/jocondelab/settings.py Thu Oct 31 12:42:46 2013 +0100 @@ -262,6 +262,8 @@ TERM_LIST_PAGE_SIZE = 20 PAGINATOR_VISIBLE_RANGE = 5 +# 24 hours DB_QUERY_CACHE_TIME +DB_QUERY_CACHE_TIME = 86400 from config import * # @UnusedWildImport diff -r 3e9dd7d99ba0 -r 9136e10ad374 src/jocondelab/static/jocondelab/js/front-home.js --- a/src/jocondelab/static/jocondelab/js/front-home.js Wed Oct 30 11:55:33 2013 +0100 +++ b/src/jocondelab/static/jocondelab/js/front-home.js Thu Oct 31 12:42:46 2013 +0100 @@ -6,7 +6,7 @@ h = 0, n = Math.min($items.length, Math.floor(baseWidth/150)), w = Math.floor(baseWidth/n); - console.log(n,w); + //console.log(n,w); $items.css("width", w + "px"); $items.each(function() { var $this = $(this); diff -r 3e9dd7d99ba0 -r 9136e10ad374 src/jocondelab/templates/jocondelab/front_base.html --- a/src/jocondelab/templates/jocondelab/front_base.html Wed Oct 30 11:55:33 2013 +0100 +++ b/src/jocondelab/templates/jocondelab/front_base.html Thu Oct 31 12:42:46 2013 +0100 @@ -107,11 +107,12 @@ {% trans 'Mots-clés' %} {% url 'random_describe' as url %} - - diff -r 3e9dd7d99ba0 -r 9136e10ad374 src/jocondelab/views/front_office.py --- 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: