ORM db request optimisation.
authorcavaliet
Thu, 21 Feb 2013 17:06:43 +0100
changeset 1115 7fc32e1b2abf
parent 1114 a4c1ba7c1250
child 1116 a528352a15ac
ORM db request optimisation.
src/ldt/ldt/ldt_utils/templates/front/front_home.html
src/ldt/ldt/ldt_utils/views/front.py
src/ldt/ldt/ldt_utils/views/workspace.py
--- a/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Thu Feb 21 13:13:20 2013 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Thu Feb 21 17:06:43 2013 +0100
@@ -112,7 +112,7 @@
             <a href="{% url ldt.ldt_utils.views.front.group_info group.id %}">{% thumbnail group.get_profile.image "54x40" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}" alt="{% trans 'group picture' %}" title="{% trans 'view more infos on this group'%}">{% endthumbnail %}</a>
         </div>
         <div class="txt_groupes_actifs">
-            <div class="bulle_people" title="{% blocktrans count nb=group.user_set.count %}{{nb}} user in this group{% plural %}{{nb}} users in this group{% endblocktrans %}">{{ group.user_set.count }}</div>
+            <div class="bulle_people" title="{% blocktrans count nb=group.nb_users %}{{nb}} user in this group{% plural %}{{nb}} users in this group{% endblocktrans %}">{{ group.nb_users }}</div>
             <p><a href="{% url ldt.ldt_utils.views.front.group_info group.id %}" class="under" title="{% trans 'view more infos on this group'%}"><b>{{group.name}}</b></a></p>
             <p>{% if group.get_profile.description|striptags|length > 69 %}{{group.get_profile.description|striptags|slice:":69"}}...{% else %}{{group.get_profile.description|striptags}}{% endif %}</p>
         </div>
--- a/src/ldt/ldt/ldt_utils/views/front.py	Thu Feb 21 13:13:20 2013 +0100
+++ b/src/ldt/ldt/ldt_utils/views/front.py	Thu Feb 21 17:06:43 2013 +0100
@@ -2,6 +2,7 @@
 from django.contrib.auth.models import Group, User
 from django.core.paginator import Paginator, InvalidPage, EmptyPage
 from django.core.urlresolvers import reverse
+from django.db.models import Count
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from guardian.shortcuts import get_objects_for_group
@@ -13,23 +14,24 @@
 from ldt.utils.url import absstatic
 import base64
 
+import logging
+logger = logging.getLogger(__name__)
 
 
 def front_home(request):
     # Get the 3 last annotated contents
-    last_contents = Content.safe_objects.order_by('-stat_annotation__last_annotated').exclude(stat_annotation__nb_annotations=0)[:3]
+    last_contents = Content.safe_objects.order_by('-stat_annotation__last_annotated').select_related('stat_annotation').exclude(stat_annotation__nb_annotations=0)[:3]
     # Get the most annotated contents
-    most_contents = Content.safe_objects.order_by('-stat_annotation__nb_annotations')[:8]
+    most_contents = Content.safe_objects.order_by('-stat_annotation__nb_annotations').select_related('stat_annotation')[:8]
     # Get the active groups
-    active_groups = Group.objects.exclude(name=settings.PUBLIC_GROUP_NAME)[:5]
+    active_groups = Group.objects.select_related("profile").annotate(nb_users=Count("user")).exclude(name=settings.PUBLIC_GROUP_NAME)[:5]
     # Get the main tag list
     front_tags = settings.FRONT_TAG_LIST
     # Get the all tags list
     tag_cloud = get_content_tags()
-
     
     is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
-
+    
     return render_to_response("front/front_home.html",
                               {'last_contents': last_contents, 'most_contents':most_contents, 'active_groups':active_groups, 'front_tags':front_tags,
                                'tag_cloud': tag_cloud, 'is_gecko': is_gecko},
@@ -65,11 +67,11 @@
         tag_label = request.GET.get("tag")
         # Get all the public contents group
         if tag_label is None :
-            content_list = Content.safe_objects.all()
+            content_list = Content.safe_objects.all().select_related('stat_annotation')
         else :
-            content_list = TaggedItem.objects.get_by_model(Content.safe_objects.all(), '"'+tag_label+'"')
+            content_list = TaggedItem.objects.get_by_model(Content.safe_objects.all().select_related('stat_annotation'), '"'+tag_label+'"')
     else :
-        content_list = Content.safe_objects.filter(title__icontains=media_title)
+        content_list = Content.safe_objects.filter(title__icontains=media_title).select_related('stat_annotation')
     
     
     nb = settings.LDT_FRONT_MEDIA_PER_PAGE
@@ -158,7 +160,6 @@
             content_list = TaggedItem.objects.get_by_model(Content.objects.all(), '"'+content_tag+'"')
         results, nb, nb_segment = get_search_results(request, search, field, page, content_list)
 
-
     return render_to_response('front/front_search_results.html', {'results': results, 'nb_results' : nb, 'nb_segment':nb_segment, 'search' : search, 'field': field, 'tag_label':content_tag, 'colorurl': colorurl, 'i18nurl': i18nurl, 'language': language_code, 'baseurl': baseurl}, context_instance=RequestContext(request))
 
 
--- a/src/ldt/ldt/ldt_utils/views/workspace.py	Thu Feb 21 13:13:20 2013 +0100
+++ b/src/ldt/ldt/ldt_utils/views/workspace.py	Thu Feb 21 17:06:43 2013 +0100
@@ -320,7 +320,7 @@
     results = get_results_with_context(field, search, content_list)
     all_segments = Segment.objects.filter(element_id__in=[e['element_id'] for e in results])
     all_projects = Project.objects.filter(ldt_id__in=[e['project_id'] for e in results], state=2)
-    all_contents = Content.objects.filter(iri_id__in=[e['iri_id'] for e in results])
+    all_contents = Content.objects.filter(iri_id__in=[e['iri_id'] for e in results]).select_related('stat_annotation')
     viewable_projects_id = [p.ldt_id for p in all_projects]
     nb_segment=0       
     complete_results = []