fix pagination to just use page numbers... less bugs
authorTero Marttila <terom@fixme.fi>
Tue, 10 Feb 2009 00:19:56 +0200
changeset 75 c5ce145fdd70
parent 74 1ab95857d584
child 76 cc3ab2c39ded
fix pagination to just use page numbers... less bugs
handlers.py
templates/channel_search.tmpl
urls.py
--- a/handlers.py	Tue Feb 10 00:04:15 2009 +0200
+++ b/handlers.py	Tue Feb 10 00:19:56 2009 +0200
@@ -165,16 +165,23 @@
     )
 
 @preferences.handler(prefs.formatter, prefs.count)
-def channel_search (request, channel, formatter, count, q=None, skip=0, max=None) :
+def channel_search (request, channel, formatter, count, q=None, page=1, max=1) :
     """
-        Display the search form for the channel for GET, or do the search for POST
+        Display the search form for the channel for GET, or do the search for POST.
     """
 
+    # calculate skip offset from page/count
+    skip = page * count
+
     # got a search query?
     if q :
         try :
             # do search
             lines = search_index.search_simple(channel, q, count, skip)
+
+            # update max?
+            if max and page > max :
+                max = page
         
         except log_search.NoResultsFound :
             # no lines
@@ -194,6 +201,7 @@
         channel         = channel,
         search_query    = q,
         count           = count,
+        page            = page,
         skip            = skip,
         max             = max,
         lines           = lines,
--- a/templates/channel_search.tmpl	Tue Feb 10 00:04:15 2009 +0200
+++ b/templates/channel_search.tmpl	Tue Feb 10 00:19:56 2009 +0200
@@ -1,39 +1,38 @@
 <%inherit file="channel.tmpl" />
 
-<%def name="paginate(url, count, skip, max, _more=None, **args)">
-    ## update max?
-    % if more :
-        <% max = h.max(max, skip) %>
-    % else :
-        <% max = skip %>
-    % endif
-    ## number of pages
-    <% page_count = max / count + 1 %>
+<%def name="paginate(url, count, page_cur, page_max, _more=None, _last=False, **args)">
+    <%doc>
+        Pagination works using page numbers, with a specific number of maximum pages displayed. If _more is True,
+        then instead of a "Next" button, we have a "More" button, which goes to the max+1'th page, unless _last is
+        True, whereupon it's not displayed
+    </%doc>
     <div class="paginate">
         <ul>
             <li>
-            % if skip :
-                <a href="${h.build_url(url, count=count, skip=h.skip_prev(count, skip), max=max, **args)}">&laquo; Prev</a>
+            % if page_cur > 1 :
+                <a href="${h.build_url(url, count=count, page=page_cur-1, max=max, **args)}">&laquo; Prev</a>
             % else :
                 <span>&laquo; Prev</span>
             %endif
             </li>
-        % for page in xrange(page_count) :
+        % for page in xrange(1, page_max + 1) :
             <li>
-            % if page == skip / count :
-                <strong>${page + 1}</strong>
+            % if page == page_cur :
+                <strong>${page}</strong>
             % else :
-                <a href="${h.build_url(url, count=count, skip=h.skip_page(count, page), max=max, **args)}">${page + 1}</a>
+                <a href="${h.build_url(url, count=count, page=page, max=page_max, **args)}">${page}</a>
             % endif
             </li>
         % endfor
             <li>
-            % if _more :
-                <a href="${h.build_url(url, count=count, skip=h.skip_next(count, max), **args)}">More &raquo;</a>
-            % elif False : 
-                <a href="${h.build_url(url, count=count, skip=h.skip_next(count, max), **args)}">Next &raquo;</a>
-            % else : ## last page
+            % if _more and _last :
+                <span>More &raquo;</span>
+            % elif _more : 
+                <a href="${h.build_url(url, count=count, page=page_max+1, **args)}">More &raquo;</a>
+            % elif page_cur == page_max : ## last page
                 <span>Next &raquo;</span>
+            % else : 
+                <a href="${h.build_url(url, count=count, page=page+1, **args)}">Next &raquo;</a>
             % endif
             </li>
         </ul>
@@ -71,11 +70,11 @@
 % else :
 <div id="title">${channel.title} :: Search '${search_query}'</div>
 
-${paginate(urls.channel_search, count, skip, max, channel=channel, q=search_query, _more=bool(lines))}
+${paginate(urls.channel_search, count, page, max, channel=channel, q=search_query, _more=True, _last=not(bool(lines)))}
 % if lines :
 <%include file="lines.tmpl" />
 % else :
 <div id="search-error">No results found</div>
 % endif
-${paginate(urls.channel_search, count, skip, max, channel=channel, q=search_query, _more=bool(lines))}
+${paginate(urls.channel_search, count, page, max, channel=channel, q=search_query, _more=True, _last=not(bool(lines)))}
 % endif
--- a/urls.py	Tue Feb 10 00:04:15 2009 +0200
+++ b/urls.py	Tue Feb 10 00:19:56 2009 +0200
@@ -39,7 +39,7 @@
 channel_link        = url('/channels/{channel:cid}/link/{timestamp:ts}',                    handlers.channel_link                       )
 channel_calendar    = url('/channels/{channel:cid}/calendar/{year:int=0}/{month:int=0}',    handlers.channel_calendar                   )
 channel_date        = url('/channels/{channel:cid}/date/{date:date}',                       handlers.channel_date                       )
-channel_search      = url('/channels/{channel:cid}/search/?q=&skip:int=0&max:int=',         handlers.channel_search                     )
+channel_search      = url('/channels/{channel:cid}/search/?q=&page:int=0&max:int=1',        handlers.channel_search                     )
 
 # mapper
 mapper = urltree.URLTree(urls)