improve customer controller and templates (addresses #21)

* fix xhtml compliance
 * add form validation
 * move common template code to a separate file
This commit is contained in:
Jan Dittberner 2009-03-14 17:42:54 +01:00
parent 30b74165c6
commit e3fe3f8353
6 changed files with 70 additions and 27 deletions

View file

@ -1,12 +1,31 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
from pyalchemybiz.lib.base import * from pyalchemybiz.lib.base import BaseController, _, c, render, redirect_to
from pyalchemybiz.model import customer, meta, person from pyalchemybiz.model import customer, meta, person
from pylons.decorators import validate
from pylons.decorators.rest import restrict
import formencode
from formencode import htmlfill
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class NewCustomerForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
firstname = formencode.validators.String(
not_empty = True,
messages = {
'empty': _('''Please enter customer's firstname.''')
})
lastname = formencode.validators.String(
not_empty = True,
messages = {
'empty': _('''Please enter customer's lastname.''')
})
class CustomerController(BaseController): class CustomerController(BaseController):
""" """
Controller for customer related operations. Controller for customer related operations.
@ -16,11 +35,10 @@ class CustomerController(BaseController):
""" """
Show a customer list. Show a customer list.
""" """
cust_q = meta.Session.query(customer.Customer) c.customers = meta.Session.query(customer.Customer).all()
c.customers = cust_q.all()
return render('/derived/customer/list.mako') return render('/derived/customer/list.mako')
def view(self, id): def view(self, id=None):
""" """
Display a customer's details. Display a customer's details.
""" """
@ -38,26 +56,40 @@ class CustomerController(BaseController):
""" """
return render('/derived/customer/new.mako') return render('/derived/customer/new.mako')
def edit(self, id): def edit(self, id=None):
""" """
Displays a form for editing customer with id. Displays a form for editing customer with id.
""" """
if id is None:
abort(404)
cust_q = meta.Session.query(customer.Customer) cust_q = meta.Session.query(customer.Customer)
c.customer = cust_q.get(int(id)) cust = cust_q.get(int(id))
return render('/derived/customer/edit.mako') if cust is None:
abort(404)
values = {
'firstname': cust.person.firstname,
'lastname': cust.person.lastname,
}
return htmlfill.render(render('/derived/customer/edit.mako'), values)
def delete(self, id): def delete(self, id=None):
""" """
Deletes a customer. Deletes a customer.
""" """
if id is None:
abort(404)
cust_q = meta.Session.query(customer.Customer) cust_q = meta.Session.query(customer.Customer)
cust = cust_q.get(int(id)) cust = cust_q.get(int(id))
if cust is None or cust.person is None:
abort(404)
meta.Session.delete(cust.person) meta.Session.delete(cust.person)
meta.Session.delete(cust) meta.Session.delete(cust)
meta.Session.commit() meta.Session.commit()
redirect_to(action='list', id=None) redirect_to(action='list', id=None)
@restrict('POST')
@validate(schema=NewCustomerForm(), form='new')
def create(self): def create(self):
""" """
Saves the information submitted from new() and redirects to Saves the information submitted from new() and redirects to
@ -66,22 +98,27 @@ class CustomerController(BaseController):
cust = customer.Customer() cust = customer.Customer()
meta.Session.add(cust) meta.Session.add(cust)
cust.person = person.Person() cust.person = person.Person()
cust.person.firstname = request.params['firstname'] for k, v in self.form_result.items():
cust.person.lastname = request.params['lastname'] setattr(cust.person, k, v)
meta.Session.add(cust.person) meta.Session.add(cust.person)
meta.Session.commit() meta.Session.commit()
redirect_to(action='list', id=None) redirect_to(action='list', id=None)
def save(self, id): @restrict('POST')
@validate(schema=NewCustomerForm(), form='edit')
def save(self, id=None):
""" """
Saves the information submitted from edit() and redirects to Saves the information submitted from edit() and redirects to
list(). list().
""" """
cust_q = meta.Session.query(customer.Customer) cust_q = meta.Session.query(customer.Customer)
cust = cust_q.get(int(id)) cust = cust_q.get(int(id))
cust.person.firstname = request.params['firstname'] if cust is None:
cust.person.lastname = request.params['lastname'] abort(404)
for k, v in self.form_result.items():
if getattr(cust.person, k) != v:
setattr(cust.person, k, v)
meta.Session.add(cust.person) meta.Session.add(cust.person)
meta.Session.commit() meta.Session.commit()

View file

@ -0,0 +1,8 @@
html, body {
font-family: Verdana, sans-serif;
}
.error-message {
color: #ff0000;
font-size: 80%;
}

View file

@ -10,7 +10,7 @@
${self.head()} ${self.head()}
</head> </head>
<body> <body>
${self.header()} <div id="header">${self.header()}</div>
${self.tabs()} ${self.tabs()}
${self.menu()} ${self.menu()}
${self.heading()} ${self.heading()}
@ -22,7 +22,7 @@
<%def name="title()">${_('PyAlchemyBiz')}</%def> <%def name="title()">${_('PyAlchemyBiz')}</%def>
<%def name="head()"></%def> <%def name="head()"></%def>
<%def name="header()"><a name="top"></a></%def> <%def name="header()"><a id="top"></a></%def>
<%def name="tabs()"></%def> <%def name="tabs()"></%def>
<%def name="menu()"></%def> <%def name="menu()"></%def>
<%def name="heading()"><h1>${c.heading or _('PyAlchemyBiz')}</h1></%def> <%def name="heading()"><h1>${c.heading or _('PyAlchemyBiz')}</h1></%def>

View file

@ -1,12 +1,9 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
<%namespace file="fields.mako" name="fields" import="*"/>
${h.form(h.url_for(action='save'))} ${h.form(h.url_for(action='save'))}
<fieldset id="editcustomerform"> <fieldset id="editcustomerform">
<label for="firstname">${_('First name:')}</label><br /> <legend>${_('Edit customer')}</legend>
${h.text('firstname', c.customer.person.firstname)}<br /> ${fields.body()}
<label for="lastname">${_('Last name:')}</label><br />
${h.text('lastname', c.customer.person.lastname)}<br />
${h.submit('save', _('Save changes'))} ${h.submit('save', _('Save changes'))}
</fieldset> </fieldset>
${h.end_form()} ${h.end_form()}

View file

@ -0,0 +1,4 @@
<label for="firstname">${_('First name:')}</label><br />
${h.text('firstname')}<br />
<label for="lastname">${_('Last name:')}</label><br />
${h.text('lastname')}<br />

View file

@ -1,12 +1,9 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
<%namespace file="fields.mako" name="fields" import="*"/>
${h.form(h.url_for(action='create'))} ${h.form(h.url_for(action='create'))}
<fieldset id="createcustomerform"> <fieldset id="createcustomerform">
<label for="firstname">${_('First name:')}</label><br /> <legend>${_('Create new customer')}</legend>
${h.text('firstname')}<br /> ${fields.body()}
<label for="lastname">${_('Last name:')}</label><br />
${h.text('lastname')}<br />
${h.submit('submit', _('Add new customer'))} ${h.submit('submit', _('Add new customer'))}
</fieldset> </fieldset>
${h.end_form()} ${h.end_form()}