Enhance pagination number.
authorcavaliet
Fri, 20 Jul 2012 18:27:59 +0200
changeset 706 08ff9a600163
parent 705 107a67719d9d
child 707 2313f1a9e62b
Enhance pagination number.
src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/content_project_pagination.html
src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/contentslist.html
src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html
src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/publishedprojectslist.html
src/ldt/ldt/templatetags/navigation.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/content_project_pagination.html	Fri Jul 20 18:27:59 2012 +0200
@@ -0,0 +1,42 @@
+{% load front_tags %}
+
+{% if nb_pages %}
+	<p class="prev_next_cont_proj" >{% comment %}
+	alors ? : {{begin}} - {{middle}} - {{end}} - nb_pages = {{nb_pages}}<br/>
+	alors ? : nb_pages = {{nb_pages}} - range = {{nb_pages|get_range}} - cur_page_nb = {{cur_page_nb}}<br/>{% endcomment %}
+	{% if 0 < cur_page_nb %}<a class="{{a_class}}" alt="prev">&lt;&lt;</a>&nbsp;{% endif %}
+	{% for i in begin %}
+	  {% if i == cur_page_nb %}
+        {{i|add:'1'}}
+      {% else %}
+        <a class="{{a_class}}" alt="{{i}}">{{i|add:'1'}}</a>
+      {% endif %}
+      &nbsp;
+	{% endfor %}
+	{% if middle %}
+	...&nbsp;
+	{% for i in middle %}
+	  {% if i == cur_page_nb %}
+	    {{i|add:'1'}}
+	  {% else %}
+	    <a class="{{a_class}}" alt="{{i}}">{{i|add:'1'}}</a>
+	  {% endif %}
+	  &nbsp;
+	{% endfor %}
+	{% endif %}
+	{% if end %}
+    ...&nbsp;
+    {% for i in end %}
+      {% if i == cur_page_nb %}
+        {{i|add:'1'}}
+      {% else %}
+        <a class="{{a_class}}" alt="{{i}}">{{i|add:'1'}}</a>
+      {% endif %}
+      &nbsp;
+    {% endfor %}
+    {% endif %}
+	{% if cur_page_nb|add:'1' < nb_pages %}<a class="{{a_class}}" alt="next">&gt;&gt;</a>{% endif %}
+	</p>
+{% endif %}
+
+
--- a/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/contentslist.html	Thu Jul 19 12:16:47 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/contentslist.html	Fri Jul 20 18:27:59 2012 +0200
@@ -3,19 +3,8 @@
 {% load front_tags %}
 
 {% if nb_ct_pages %}
-	<p class="prev_next_cont_proj" >
-	{% if 0 < current_content_page %}<a class="contents_page clickable" alt="prev">&lt;&lt;</a>&nbsp;&nbsp;{% endif %}
-	{% for i in nb_ct_pages|get_range %}
-	  {% if i == current_content_page %}
-	    {{i|add:'1'}}
-	  {% else %}
-	    <a class="contents_page clickable" alt="{{i}}">{{i|add:'1'}}</a>
-	  {% endif %}
-	  &nbsp;&nbsp;
-	{% endfor %}
-	{% if current_content_page|add:'1' < nb_ct_pages %}<a class="contents_page clickable" alt="next">&gt;&gt;</a>{% endif %}
-    <input type="hidden" value="{{current_content_page}}" name="current_content_page" id="current_content_page">
-	</p>
+    {% load navigation %}{% build_pagination nb_ct_pages current_content_page "contents_page clickable" %}
+	<input type="hidden" value="{{current_content_page}}" name="current_content_page" id="current_content_page">
 {% endif %}
 <div class="projectscontentsheader projectcontentsheadertitle span-12 last" id="contentsheader">
 {% trans "name" %}
--- a/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html	Thu Jul 19 12:16:47 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html	Fri Jul 20 18:27:59 2012 +0200
@@ -3,17 +3,7 @@
 {% load front_tags %}
 
 {% if nb_pj_pages %}
-    <p class="prev_next_cont_proj" >
-    {% if 0 < current_project_page %}<a class="projects_page clickable" alt="prev">&lt;&lt;</a>&nbsp;&nbsp;{% endif %}
-    {% for i in nb_pj_pages|get_range %}
-      {% if i == current_project_page %}
-        {{i|add:'1'}}
-      {% else %}
-        <a class="projects_page clickable" alt="{{i}}">{{i|add:'1'}}</a>
-      {% endif %}
-      &nbsp;&nbsp;
-    {% endfor %}
-    {% if current_project_page|add:'1' < nb_pj_pages %}<a class="projects_page clickable" alt="next">&gt;&gt;</a>{% endif %}
+    {% load navigation %}{% build_pagination nb_pj_pages current_project_page "projects_page clickable" %}
     <input type="hidden" value="{{current_project_page}}" name="current_project_page" id="current_project_page">
     </p>
 {% endif %}
--- a/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/publishedprojectslist.html	Thu Jul 19 12:16:47 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/publishedprojectslist.html	Fri Jul 20 18:27:59 2012 +0200
@@ -3,7 +3,7 @@
 {% load front_tags %}
 
 {% if nb_pj_pages %}
-    <p class="prev_next_cont_proj" >
+    {% comment %}<p class="prev_next_cont_proj" >
     {% if 0 < current_project_page %}<a class="projects_page clickable" alt="prev">&lt;&lt;</a>&nbsp;&nbsp;{% endif %}
     {% for i in nb_pj_pages|get_range %}
       {% if i == current_project_page %}
@@ -14,8 +14,9 @@
       &nbsp;&nbsp;
     {% endfor %}
     {% if current_project_page|add:'1' < nb_pj_pages %}<a class="projects_page clickable" alt="next">&gt;&gt;</a>{% endif %}
+    </p>{% endcomment %}
+    {% load navigation %}{% build_pagination nb_pj_pages current_project_page "projects_page clickable" %}
     <input type="hidden" value="{{current_project_page}}" name="current_project_page" id="current_project_page">
-    </p>
 {% endif %}
 <div class="projectscontentsheader projectcontentsheadertitle span-12 last" id="projectsheader">
 {% trans "name" %}
--- a/src/ldt/ldt/templatetags/navigation.py	Thu Jul 19 12:16:47 2012 +0200
+++ b/src/ldt/ldt/templatetags/navigation.py	Fri Jul 20 18:27:59 2012 +0200
@@ -17,3 +17,60 @@
         if re.search(pattern, request.path):
             return css_class
     return ''
+
+
+@register.inclusion_tag("ldt/ldt_utils/partial/content_project_pagination.html", takes_context=True)
+def build_pagination(context, nb_pages, cur_page_nb, a_class, begin_pages=2, end_pages=2, before_current_pages=2, after_current_pages=2):
+    # Digg-like pagination. Adapted from http://djangosnippets.org/snippets/2199/
+    nb_pages = int(nb_pages)
+    cur_page_nb = int(cur_page_nb)
+    #assert False, str(nb_pages) + ', ' + str(cur_page_nb)
+    before = max(cur_page_nb - before_current_pages - 1, 0)
+    after = cur_page_nb + after_current_pages
+    
+    page_range = range(nb_pages)
+    #assert False, str(nb_pages) + str(page_range) 
+    begin = page_range[:begin_pages]
+    #assert False, str(nb_pages) + ", " + str(page_range) + ", " + str(begin)
+    middle = page_range[before+1:after+1]
+    end = page_range[-end_pages:]
+    last_page_number = end[-1]
+    #assert False, "a - cur_page_nb = " + str(cur_page_nb) + ", before = " + str(before) + ", after = " + str(after) + ", begin = " + str(begin) + ", middle = " + str(middle) + ", end = " + str(end)
+    
+    def collides(firstlist, secondlist):
+        """ Returns true if lists collides (have same entries)
+        >>> collides([1,2,3,4],[3,4,5,6,7])
+        True
+        >>> collides([1,2,3,4],[5,6,7])
+        False
+        """
+        return any(item in secondlist for item in firstlist)
+    
+    # If middle and end has same entries, then end is what we want
+    if collides(middle, end):
+        end = range(max(cur_page_nb-before_current_pages, 1), last_page_number+1)
+        middle = []
+        
+    #assert False, "0, " + str(nb_pages) + ", " + str(page_range) + ", " + str(begin)
+    # If begin and middle ranges has same entries, then begin is what we want
+    if collides(begin, middle):
+        begin = range(0, min(cur_page_nb + after_current_pages - 1, last_page_number)+1)
+        middle = []
+        #assert False, "1 - cur_page_nb = " + str(cur_page_nb) + ", before = " + str(before) + ", after = " + str(after) + ", begin = " + str(begin) + ", middle = " + str(middle) + ", end = " + str(end)
+        
+    # If begin and end has same entries then begin is what we want
+    if collides(begin, end):
+        begin = range(1, last_page_number+1)
+        end = []
+        #assert False, "2 - " + str(nb_pages) + ", " + str(page_range) + ", " + str(begin)
+        
+    #assert False, str(nb_pages) + ", " + str(page_range) + ", " + str(begin)
+    
+    context.update({'begin' : begin,
+                    'middle' : middle,
+                    'end' : end,
+                    'nb_pages' : nb_pages,
+                    'cur_page_nb' : cur_page_nb,
+                    'a_class' : a_class})
+    #assert False, begin + middle + end
+    return context