improve customer controller and templates (addresses #21)

* add footer actions to customer view template
 * add pagination to customer list
This commit is contained in:
Jan Dittberner 2009-03-14 20:09:37 +01:00
parent e3fe3f8353
commit 7bfe4996b2
3 changed files with 63 additions and 9 deletions

View file

@ -1,12 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
from pyalchemybiz.lib.base import BaseController, _, c, render, redirect_to from pyalchemybiz.lib.base import BaseController, _, c, render, redirect_to, \
request
from pyalchemybiz.model import customer, meta, person from pyalchemybiz.model import customer, meta, person
from pylons.decorators import validate from pylons.decorators import validate
from pylons.decorators.rest import restrict from pylons.decorators.rest import restrict
import formencode import formencode
from formencode import htmlfill from formencode import htmlfill
import webhelpers.paginate as paginate
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -35,7 +37,12 @@ class CustomerController(BaseController):
""" """
Show a customer list. Show a customer list.
""" """
c.customers = meta.Session.query(customer.Customer).all() records = meta.Session.query(customer.Customer)
c.paginator = paginate.Page(
records,
page = int(request.params.get('page', 1)),
items_per_page = 10,
)
return render('/derived/customer/list.mako') return render('/derived/customer/list.mako')
def view(self, id=None): def view(self, id=None):

View file

@ -1,9 +1,46 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
<ul id="customers"> <%def name="heading()"><h1>Customer List</h1></%def>
% for customer in c.customers:
<li>${customer} [${h.link_to(_('view customer'), h.url_for(id=customer.id, action='view'))}] [${h.link_to(_('edit customer'), h.url_for(id=customer.id, action="edit"))}] [${h.link_to(_('delete customer'), h.url_for(id=customer.id, action="delete"))}]</li>
% endfor
</ul>
${h.link_to(_('create new customer'), h.url_for(action="new"))} <%def name="buildrow(customer, odd=True)">
%if odd:
<tr class="odd">
%else:
<tr class="even">
%endif
<td>
${h.link_to(
customer.id,
h.url_for(
controller=u'customer',
action='view',
id=unicode(customer.id)
)
)}
</td>
<td>
${customer}
</td>
</tr>
</%def>
%if len(c.paginator):
<p>${c.paginator.pager(_('$link_first $link_previous $first_item to $last_item of $item_count $link_next $link_last'))}</p>
<table class="paginator">
<tr>
<th>${_('Customer ID')}</th>
<th>${_('Customer')}</th>
</tr>
<% counter=0 %>
%for item in c.paginator:
${buildrow(item, counter%2)}
<% counter += 1 %>
%endfor
</table>
<p>${c.paginator.pager('~2~')}</p>
%else:
<p>
${_('No customers have yet been created.')}
${h.link_to(_('Add one'), h.url_for(controller='customer', action='new'))}.
</p>
%endif

View file

@ -1,5 +1,7 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
<%def name="heading()"><h1>${_('View customer')}</h1></%def>
<table> <table>
<tbody> <tbody>
<tr> <tr>
@ -11,4 +13,12 @@
</tbody> </tbody>
</table> </table>
<%def name="heading()"><h1>${_('View customer')}</h1></%def> <%def name="footer()">
<p>
${h.link_to(_('All Customers'), h.url_for(controller='customer', action='list', id=None))}
| ${h.link_to(_('New Customer'), h.url_for(controller='customer', action='new', id=None))}
| ${h.link_to(_('Edit Customer'), h.url_for(controller='customer', action='edit'))}
| ${h.link_to(_('Delete Customer'), h.url_for(controller='customer', action='delete'))}
</p>
${parent.footer()}
</%def>