mango r229 - in branches/django: . mango www



Author: ovitters
Date: Sun Jun 22 20:34:03 2008
New Revision: 229
URL: http://svn.gnome.org/viewvc/mango?rev=229&view=rev

Log:
	* mango/views.py (add_paginator_to_xml, list_accounts,
	  list_foundationmembers): Paginate results from a query. Add
	  paginated entries to the XML by use of the add_paginator_to_xml
	  function.
	* www/list_foundationmembers.xsl: Correct links for paginator.


Modified:
   branches/django/   (props changed)
   branches/django/ChangeLog
   branches/django/mango/views.py
   branches/django/www/list_foundationmembers.xsl

Modified: branches/django/mango/views.py
==============================================================================
--- branches/django/mango/views.py	(original)
+++ branches/django/mango/views.py	Sun Jun 22 20:34:03 2008
@@ -1,5 +1,6 @@
 from django.http import HttpResponse, Http404, HttpResponseServerError
 from django.conf import settings
+from django.core.paginator import QuerySetPaginator
 from django.db.models import Q
 import datetime
 
@@ -49,6 +50,19 @@
     doc.write(response, 'utf-8')
     return response
 
+def add_paginator_to_xml(root, page):
+    """Add paginator information to the XML node specified by root
+
+    Note:
+     - In the PHP version, the XML also had the elements result_num and page_size"""
+    pagednode = ET.SubElement(root, 'pagedresults')
+    node = ET.SubElement(pagednode, 'total_results')
+    node.text = unicode(page.paginator.count)
+    node = ET.SubElement(pagednode, 'total_pages')
+    node.text = unicode(page.paginator.num_pages)
+    node = ET.SubElement(pagednode, 'page_num')
+    node.text = unicode(page.number)
+
 def current_datetime(request):
     now = datetime.datetime.now()
     html = "<html><body>It is now %s.</body></html>" % now
@@ -125,6 +139,9 @@
     el1 = ET.SubElement(root, 'listaccounts')
 
     accounts = models.AccountRequest.objects.all()
+    paginator = QuerySetPaginator(accounts, 25)
+    page_num = request.GET.get('page', 1)
+    accounts = paginator.page(page_num).object_list
     for account in accounts:
         el2 = ET.SubElement(el1, 'account', dict([a for a in account.__dict__.iteritems() if a[0] not in ('id', 'timestamp')]))
         el2g = ET.SubElement(el2, 'groups')
@@ -180,8 +197,13 @@
     doc, root = get_xmldoc('List Foundation Members', request)
     el1 = ET.SubElement(root, 'listfoundationmembers')
 
+    page_num = request.GET.get('page', 1)
+
     members = models.Foundationmembers.objects.all()
-    for member in members:
+    paginator = QuerySetPaginator(members, 25)
+    page = paginator.page(page_num)
+    add_paginator_to_xml(el1, page)
+    for member in page.object_list:
         membernode = ET.SubElement(el1, 'foundationmember')
         membernode.set('id', unicode(member.id))
         for field in ('firstname', 'lastname', 'comments', 'email'):

Modified: branches/django/www/list_foundationmembers.xsl
==============================================================================
--- branches/django/www/list_foundationmembers.xsl	(original)
+++ branches/django/www/list_foundationmembers.xsl	Sun Jun 22 20:34:03 2008
@@ -43,10 +43,10 @@
        </xsl:if>
        <span class="smallprint">(<xsl:value-of select="pagedresults/total_results"/> members found)</span>
        <xsl:if test="pagedresults/page_num &gt; 1">
-        <a class="button" href="{$script}?page={pagedresults/page_num - 1}">&lt;&lt; Prev</a>
+        <a class="button" href="?page={pagedresults/page_num - 1}">&lt;&lt; Prev</a>
        </xsl:if>
        <xsl:if test="pagedresults/page_num &lt; pagedresults/total_pages">
-        <a class="button" href="{$script}?page={pagedresults/page_num + 1}">Next &gt;&gt;</a>
+        <a class="button" href="?page={pagedresults/page_num + 1}">Next &gt;&gt;</a>
        </xsl:if>
       </td>
      </tr>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]