Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
authorymh <ymh.work@gmail.com>
Thu, 23 Jun 2011 16:00:38 +0200
changeset 55 e1098febb9d3
parent 54 deaa9393e6a2 (diff)
parent 52 1f01957a3eae (current diff)
child 56 e70cbbc093cc
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
web/hdabo/settings.py
web/hdabo/views.py
--- a/web/hdabo/settings.py	Wed Jun 22 17:53:22 2011 +0200
+++ b/web/hdabo/settings.py	Thu Jun 23 16:00:38 2011 +0200
@@ -111,8 +111,17 @@
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
+    #django-debug-toolbar
+    'debug_toolbar.middleware.DebugToolbarMiddleware',
 )
 
+#django-debug-toolbar
+INTERNAL_IPS = ('127.0.0.1','localhost',)
+DEBUG_TOOLBAR_CONFIG = { 
+    'SHOW_TOOLBAR_CALLBACK': lambda(req): True,
+} 
+
+
 ROOT_URLCONF = 'hdabo.urls'
 
 TEMPLATE_DIRS = (
@@ -132,6 +141,8 @@
     'django_extensions',
     'haystack',
     'hdabo',
+    #django-debug-toolbar
+    'debug_toolbar',
 )
 
 # A sample logging configuration. The only tangible logging
@@ -163,4 +174,7 @@
 
 DBPEDIA_URI_TEMPLATE = "http://dbpedia.org/resource/%s"
 
+SEARCH_STAR_CHARACTER = "*"
+PAGINATION_DEFAULT_NB_BY_PAGE = 50
+
 from hdabo.config import * #@UnusedWildImport
--- a/web/hdabo/templates/partial/tag_table.html	Wed Jun 22 17:53:22 2011 +0200
+++ b/web/hdabo/templates/partial/tag_table.html	Thu Jun 23 16:00:38 2011 +0200
@@ -32,8 +32,8 @@
 	            {% endif %}
 	        </td>
 	        <td class="text_centered">
-	            {% if tag.dbpedia_uri and tag.dbpedia_uri != ""  %}
-	            <a href="{{tag.dbpedia_uri}}" target="_blank"><img src="{{STATIC_URL}}hdabo/img/arrow_green_right.png" ></a>
+	            {% if t.tag.dbpedia_uri and t.tag.dbpedia_uri != ""  %}
+	            <a href="{{t.tag.dbpedia_uri}}" target="_blank"><img src="{{STATIC_URL}}hdabo/img/arrow_green_right.png" ></a>
 	            {% else %}
 	            &nbsp;
 	            {% endif %}
@@ -62,8 +62,8 @@
 	            {% endif %}
 	        </td>
 	        <td class="text_centered">
-                {% if tag.dbpedia_uri and tag.dbpedia_uri != ""  %}
-                <a href="{{tag.dbpedia_uri}}" target="_blank"><img src="{{STATIC_URL}}hdabo/img/arrow_green_right.png" ></a>
+                {% if t.tag.dbpedia_uri and t.tag.dbpedia_uri != ""  %}
+                <a href="{{t.tag.dbpedia_uri}}" target="_blank"><img src="{{STATIC_URL}}hdabo/img/arrow_green_right.png" ></a>
                 {% else %}
                 &nbsp;
                 {% endif %}
--- a/web/hdabo/views.py	Wed Jun 22 17:53:22 2011 +0200
+++ b/web/hdabo/views.py	Thu Jun 23 16:00:38 2011 +0200
@@ -7,16 +7,16 @@
 from django.http import HttpResponseBadRequest
 from django.shortcuts import render_to_response, redirect
 from django.template import RequestContext
-from django.utils.http import urlquote, urlencode
+from django.utils.http import urlquote
 from haystack.constants import DJANGO_ID
 from haystack.query import SearchQuerySet
-from hdabo.wp_utils import process_tag
 from hdabo.utils import OrderedDict
-from hdabo.wp_utils import (normalize_tag, query_wikipedia_title,
-    get_or_create_tag)
+from hdabo.wp_utils import (normalize_tag, query_wikipedia_title, 
+    get_or_create_tag, process_tag)
 from models import Datasheet, Organisation, Tag, TagCategory, TaggedSheet
 from wikitools import wiki
 import django.utils.simplejson as json
+import re
 
 
 
@@ -24,14 +24,21 @@
 def home(request):
     
     # Get all organizations
-    orgas = Organisation.objects.all().order_by('name')
+    #ds_queryset = Datasheet.objects.filter(organisation="hdabo_organisation.id")
+    
+    orgas = Organisation.objects.all()\
+        .order_by('name').annotate(nb_all=Count("datasheet"))\
+        .extra(select={
+            'nb_val':"SELECT COUNT(*) FROM hdabo_datasheet WHERE hdabo_datasheet.validated=true and hdabo_datasheet.organisation_id=hdabo_organisation.id",
+            'nb_unval':"SELECT COUNT(*) FROM hdabo_datasheet WHERE hdabo_datasheet.validated=false and hdabo_datasheet.organisation_id=hdabo_organisation.id",
+        })
     # Count all validated, unvalidated sheets for each organisation
     org_list = []
     for orga in orgas :
-        all_datasheets = Datasheet.objects.filter(organisation=orga)
-        nb_all = len(all_datasheets)
-        nb_val = len(all_datasheets.filter(validated=True))
-        nb_unval = len(all_datasheets.filter(validated=False))
+        #all_datasheets = Datasheet.objects.filter(organisation=orga)
+        nb_all = orga.nb_all#len(all_datasheets)
+        nb_val = orga.nb_val#len(all_datasheets.filter(validated=True))
+        nb_unval = orga.nb_unval#len(all_datasheets.filter(validated=False))
         org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'nb_unval':nb_unval})
     
     return render_to_response("organisation_list.html",
@@ -56,27 +63,28 @@
     # If valid = 0, we search unvalidated sheets
     # If valid = 1, we search validated sheets
     # If valid = 2, we search AND DISPLAY all sheets
+    datasheets_qs = Datasheet.objects.filter(organisation=orga).order_by("id").select_related("format")
+    
     if valid == "1" :
         # We count all the validated sheets
-        datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=True)
-        nb_sheets = len(datasheets)
+        datasheets_qs = datasheets_qs.filter(validated=True)
         # And select the current one
-        datasheets = [datasheets[start_index]]
+        datasheets = [datasheets_qs[start_index]]
     elif valid != "2":
         valid = "0"
         # We count all the validated sheets
-        datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=False)
-        nb_sheets = len(datasheets)
+        datasheets_qs = datasheets_qs.filter(validated=False)
         # And select the current one
-        datasheets = [datasheets[start_index]]
-    else :
-        datasheets = Datasheet.objects.filter(organisation=orga)
-        nb_sheets = len(datasheets)
+        datasheets = [datasheets_qs[start_index]]
+    else:
+        datasheets = datasheets_qs
+
+    nb_sheets = datasheets_qs.count()
     
     # We get the ORDERED tags if we display one sheet (case valid = 0 and 1)
     ordered_tags = None
     if valid != "2" :
-        ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).order_by('order')
+        ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).select_related("tag").order_by('order')
     
     displayed_index = start_index + 1;
     prev_index = max(start_index - 1, 0);
@@ -95,61 +103,23 @@
 #@login_required
 def all_tags(request, num_page=None, nb_by_page=None, searched=None):
     
-    default_nb_py_page = 50
     # If the view is asked after a form sent with post vars, it means that searched is a post var.
     if u"searched" in request.POST :
         searched = request.POST["searched"]
-        nb_by_page = default_nb_py_page
+        nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
         num_page = 1
     
     # Get paginator and current page 
     current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, searched)
-    nb_total = p.count
     
     prev_page = max(num_page - 1, 1)
     next_page = min(num_page + 1, p.num_pages)
     last_page = p.num_pages
-    
-    star_character = "*"
-    search_def = (('0', urlquote('0'+star_character)),
-                  ('1', urlquote('1'+star_character)),
-                  ('2', urlquote('2'+star_character)),
-                  ('3', urlquote('3'+star_character)),
-                  ('4', urlquote('4'+star_character)),
-                  ('5', urlquote('5'+star_character)),
-                  ('6', urlquote('6'+star_character)),
-                  ('7', urlquote('7'+star_character)),
-                  ('8', urlquote('8'+star_character)),
-                  ('9', urlquote('9'+star_character)),
-                  ('A', urlquote('a'+star_character)),
-                  ('B', urlquote('b'+star_character)),
-                  ('C', urlquote('c'+star_character)),
-                  ('D', urlquote('d'+star_character)),
-                  ('E', urlquote('e'+star_character)),
-                  ('F', urlquote('f'+star_character)),
-                  ('G', urlquote('g'+star_character)),
-                  ('H', urlquote('h'+star_character)),
-                  ('I', urlquote('i'+star_character)),
-                  ('J', urlquote('j'+star_character)),
-                  ('K', urlquote('k'+star_character)),
-                  ('L', urlquote('l'+star_character)),
-                  ('M', urlquote('m'+star_character)),
-                  ('N', urlquote('n'+star_character)),
-                  ('O', urlquote('o'+star_character)),
-                  ('P', urlquote('p'+star_character)),
-                  ('Q', urlquote('q'+star_character)),
-                  ('R', urlquote('r'+star_character)),
-                  ('S', urlquote('s'+star_character)),
-                  ('T', urlquote('t'+star_character)),
-                  ('U', urlquote('u'+star_character)),
-                  ('V', urlquote('v'+star_character)),
-                  ('W', urlquote('w'+star_character)),
-                  ('X', urlquote('x'+star_character)),
-                  ('Y', urlquote('y'+star_character)),
-                  ('Z', urlquote('z'+star_character)))
-    
+        
+    search_def = tuple([(c,urlquote(c + settings.SEARCH_STAR_CHARACTER)) for c in '01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'])
+        
     return render_to_response("all_tags.html",
-                              {'nb_total':nb_total, 'tags':current_page.object_list, 'current_page':current_page,
+                              {'nb_total':p.count, 'tags':current_page.object_list, 'current_page':current_page,
                                'prev_page':prev_page, 'next_page':next_page, 'last_page':last_page,
                                'num_page':num_page, 'nb_by_page':nb_by_page, 'searched':searched,
                                'categories':json.dumps(get_categories()),
@@ -204,40 +174,33 @@
 #@login_required
 def get_all_tags_table(request, num_page=None, nb_by_page=None, searched=None):
     
-    current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, searched)
+    current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, searched) #@UnusedVariable
     
     return render_to_response("partial/all_tags_table.html",
                               {'tags':current_page.object_list},
                               context_instance=RequestContext(request))
 
 
-#@login_required
 def get_current_page(num_page=None, nb_by_page=None, searched=None):
     
-    default_nb_py_page = 50
-    star_character = "*"
+    base_queryset = Tag.objects.all()
+    
     if searched and searched != "" :
-        # searched terms are word, word* or *word* (* = star_character)
-        if searched.endswith(star_character) and not searched.startswith(star_character) :
-            alltags = Tag.objects.filter(label__istartswith=searched[:-1])
-        elif not searched.endswith(star_character) and searched.startswith(star_character) :
-            alltags = Tag.objects.filter(label__iendswith=searched[1:])
-        elif searched.endswith(star_character) and searched.startswith(star_character) :
-            alltags = Tag.objects.filter(label__icontains=searched[1:-1])
-        else :
-            alltags = Tag.objects.filter(label__iexact=searched)
-    else :
-        alltags = Tag.objects.all()
-    alltags = alltags.annotate(num_ds=Count('datasheet')).order_by('-popularity','-num_ds','label')
+        searched = searched.strip()
+        regex = "^%s$" % (re.escape(searched).replace(re.escape(settings.SEARCH_STAR_CHARACTER),".*"))
+        base_queryset = base_queryset.filter(label__iregex=regex)
+
+    alltags = base_queryset.annotate(num_ds=Count('datasheet')).order_by('-popularity','-num_ds','label')
+    #alltags = alltags.order_by('-popularity','label')
     
     # We build the paginator for the requested list
     if nb_by_page :
         try:
             nb_by_page = int(nb_by_page)
         except :
-            nb_by_page = default_nb_py_page
+            nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
     else :
-        nb_by_page = default_nb_py_page
+        nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
     if num_page :
         try:
             num_page = int(num_page)
@@ -246,6 +209,9 @@
     else :
         num_page = 1
     p = Paginator(alltags, nb_by_page)
+    
+    #use the much simpler base query to have the count
+    p._count = base_queryset.count()
     current_page = p.page(num_page)
     
     return current_page, p, num_page, nb_by_page