random optimisation, menu debug, v0.20 V00.20
authorcavaliet
Thu, 31 Oct 2013 12:42:46 +0100
changeset 171 9136e10ad374
parent 170 3e9dd7d99ba0
child 172 14fb2c397f17
random optimisation, menu debug, v0.20
src/jocondelab/__init__.py
src/jocondelab/settings.py
src/jocondelab/static/jocondelab/js/front-home.js
src/jocondelab/templates/jocondelab/front_base.html
src/jocondelab/views/front_office.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():
--- 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
 
--- 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);
--- 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 @@
                     <a href="{{url}}" title="{% trans "Choisissez un artiste, un sujet représenté, un domaine ou une époque d'un simple clic." %}">{% trans 'Mots-clés' %}</a>
                 </li>
                 {% url 'random_describe' as url %}
-                <li class="menu-item{% if request.path == url %} menu-item-current{% endif %}">
+                <li class="menu-item{% if '/describe/' in request.path %} menu-item-current{% endif %}">
                     <a href="{{url}}" title="{% trans "Aidez-nous à décrire le sujet d'une œuvre à l'aide de Wikipédia." %}">{% trans "Contribuer" %}</a>
                 </li>
-                <li class="menu-item">
-                    <a href="{% url 'front_students' %}">{% trans 'Travaux étudiants' %}</a>
+                {% url 'front_students' as url %}
+                <li class="menu-item{% if request.path == url %} menu-item-current{% endif %}">
+                    <a href="{{url}}" title="{% trans "Consultez les travaux des étudiants faits à partir de la base Joconde." %}">{% trans 'Travaux étudiants' %}</a>
                 </li>
             </ul>
         </div>
--- 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: