Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
authorcavaliet
Wed, 22 Jun 2011 17:53:22 +0200
changeset 52 1f01957a3eae
parent 51 6b1338c7964c
child 53 50f3f48467e8
child 55 e1098febb9d3
Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
web/hdabo/templates/partial/all_tags_table.html
web/hdabo/views.py
--- a/web/hdabo/templates/partial/all_tags_table.html	Wed Jun 22 12:21:00 2011 +0200
+++ b/web/hdabo/templates/partial/all_tags_table.html	Wed Jun 22 17:53:22 2011 +0200
@@ -7,7 +7,9 @@
         <th class="text_centered">Lien D</th>
         <th>Catégorie</th>
         <th class="large_25 text_centered">Supprimer<br/>le lien W</th>
-        <th>Alias</th></tr>
+        <th>Alias</th>
+        <th class="text_centered">Nb de<br/>fiches</th>
+        <th>Popularité</th></tr>
     {% for tag in tags %}
     <tr class="imageline {% cycle 'hdabooddline' 'hdaboevenline' %}">
         <td class="reset_wp_info">{{tag.id}}</td>
@@ -29,7 +31,9 @@
         </td>
         <td class="tag_category" id="{{tag.id}}">{% if tag.category %}{{ tag.category }}{% endif %}</td>
         <td class="text_centered"><img src="{{STATIC_URL}}hdabo/img/red_cross.png" class="remove_wp_link" id="{{tag.id}}" alt="{{tag.label}}" /></td>
-        <td class="tag_alias" id="{{tag.id}}" >{% if tag.alias %}{{tag.alias}}{% endif %}</td></tr>
+        <td class="tag_alias" id="{{tag.id}}" >{% if tag.alias %}{{tag.alias}}{% endif %}</td>
+        <td class="text_centered">{{tag.num_ds}}</td>
+        <td class="text_centered">{{tag.popularity}}</td></tr>
     {% endfor %}
     </table>
 {% endblock %}
--- a/web/hdabo/views.py	Wed Jun 22 12:21:00 2011 +0200
+++ b/web/hdabo/views.py	Wed Jun 22 17:53:22 2011 +0200
@@ -3,7 +3,7 @@
 from django.conf import settings
 from django.contrib.auth.decorators import login_required #@UnusedImport
 from django.core.paginator import Paginator
-from django.db.models import Max
+from django.db.models import Max, Count
 from django.http import HttpResponseBadRequest
 from django.shortcuts import render_to_response, redirect
 from django.template import RequestContext
@@ -96,49 +96,21 @@
 def all_tags(request, num_page=None, nb_by_page=None, searched=None):
     
     default_nb_py_page = 50
-    star_character = "*"
     # 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
         num_page = 1
     
-    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]).order_by('label')
-        elif not searched.endswith(star_character) and searched.startswith(star_character) :
-            alltags = Tag.objects.filter(label__iendswith=searched[1:]).order_by('label')
-        elif searched.endswith(star_character) and searched.startswith(star_character) :
-            alltags = Tag.objects.filter(label__icontains=searched[1:-1]).order_by('label')
-        else :
-            alltags = Tag.objects.filter(label__iexact=searched).order_by('label')
-    else :
-        alltags = Tag.objects.order_by('label')
-        
-    nb_total = len(alltags)
-    # 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
-    else :
-        nb_by_page = default_nb_py_page
-    if num_page :
-        try:
-            num_page = int(num_page)
-        except :
-            num_page = 1
-    else :
-        num_page = 1
-    p = Paginator(alltags, nb_by_page)
-    current_page = p.page(num_page)
+    # 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)),
@@ -232,20 +204,31 @@
 #@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)
+    
+    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 = "*"
     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]).order_by('label')
+            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:]).order_by('label')
+            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]).order_by('label')
+            alltags = Tag.objects.filter(label__icontains=searched[1:-1])
         else :
-            alltags = Tag.objects.filter(label__iexact=searched).order_by('label')
+            alltags = Tag.objects.filter(label__iexact=searched)
     else :
-        alltags = Tag.objects.order_by('label')
+        alltags = Tag.objects.all()
+    alltags = alltags.annotate(num_ds=Count('datasheet')).order_by('-popularity','-num_ds','label')
     
     # We build the paginator for the requested list
     if nb_by_page :
@@ -265,9 +248,7 @@
     p = Paginator(alltags, nb_by_page)
     current_page = p.page(num_page)
     
-    return render_to_response("partial/all_tags_table.html",
-                              {'tags':current_page.object_list},
-                              context_instance=RequestContext(request))
+    return current_page, p, num_page, nb_by_page
 
 
 #@login_required