use better CRUD action names in customer controller (addresses #21)

This commit is contained in:
Jan Dittberner 2009-03-08 15:14:05 +01:00
parent 475c9de3b4
commit f623c3d500
7 changed files with 82 additions and 42 deletions

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import logging import logging
from pyalchemybiz.lib.base import * from pyalchemybiz.lib.base import *
@ -7,52 +8,77 @@ log = logging.getLogger(__name__)
class CustomerController(BaseController): class CustomerController(BaseController):
"""
Controller for customer related operations.
"""
def index(self): def list(self):
sess = meta.Session() """
cust_q = sess.query(customer.Customer) Show a customer list.
"""
cust_q = meta.Session.query(customer.Customer)
c.customers = cust_q.all() c.customers = cust_q.all()
return render('/derived/customer/index.mako') return render('/derived/customer/list.mako')
def create(self): def view(self, id):
sess = meta.Session() """
return render('/derived/customer/create.mako') Display a customer's details.
"""
cust_q = meta.Session.query(customer.Customer)
c.customer = cust_q.get(int(id))
return render('/derived/customer/view.mako')
def new(self):
"""
Displays a form for creating a new customer.
"""
return render('/derived/customer/new.mako')
def edit(self, id): def edit(self, id):
sess = meta.Session() """
cust_q = sess.query(customer.Customer) Displays a form for editing customer with id.
c.customer = cust_q.filter(customer.Customer.id==id).one() """
cust_q = meta.Session.query(customer.Customer)
c.customer = cust_q.get(int(id))
return render('/derived/customer/edit.mako') return render('/derived/customer/edit.mako')
def delete(self, id): def delete(self, id):
sess = meta.Session() """
cust_q = sess.query(customer.Customer) Deletes a customer.
cust = cust_q.filter(customer.Customer.id==id).one() """
sess.delete(cust.person) cust_q = meta.Session.query(customer.Customer)
sess.delete(cust) cust = cust_q.get(int(id))
sess.commit() meta.Session.delete(cust.person)
meta.Session.delete(cust)
meta.Session.commit()
redirect_to(action='index') redirect_to(action='list')
def process_create(self): def create(self):
sess = meta.Session() """
Saves the information submitted from new() and redirects to
list().
"""
cust = customer.Customer() cust = customer.Customer()
sess.save(cust) meta.Session.add(cust)
cust.person = person.Person() cust.person = person.Person()
cust.person.firstname = request.params['firstname'] cust.person.firstname = request.params['firstname']
cust.person.lastname = request.params['lastname'] cust.person.lastname = request.params['lastname']
sess.save(cust.person) meta.Session.add(cust.person)
sess.commit() meta.Session.commit()
redirect_to(action='index') redirect_to(action='list')
def process_edit(self, id): def save(self, id):
sess = meta.Session() """
cust_q = sess.query(customer.Customer) Saves the information submitted from edit() and redirects to
cust = cust_q.filter(customer.Customer.id==id).one() list().
"""
cust_q = meta.Session.query(customer.Customer)
cust = cust_q.get(int(id))
cust.person.firstname = request.params['firstname'] cust.person.firstname = request.params['firstname']
cust.person.lastname = request.params['lastname'] cust.person.lastname = request.params['lastname']
sess.add(cust.person) meta.Session.add(cust.person)
sess.commit() meta.Session.commit()
redirect_to(action='index') redirect_to(action='list')

View file

@ -8,5 +8,5 @@ log = logging.getLogger(__name__)
class IndexController(BaseController): class IndexController(BaseController):
def index(self): def index(self):
c.menuitems = [(_("Customer management"), "customer", "index")] c.menuitems = [(_("Customer management"), "customer", "list")]
return render('/derived/index/index.mako') return render('/derived/index/index.mako')

View file

@ -1,6 +1,6 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
${h.form(h.url_for(action='process_edit'))} ${h.form(h.url_for(action='save'))}
<fieldset id="editcustomerform"> <fieldset id="editcustomerform">
<label for="firstname">${_('First name:')}</label><br /> <label for="firstname">${_('First name:')}</label><br />
${h.text('firstname', c.customer.person.firstname)}<br /> ${h.text('firstname', c.customer.person.firstname)}<br />

View file

@ -1,9 +0,0 @@
<%inherit file="/base/customer.mako" />
<ul id="customers">
% for customer in c.customers:
<li>${customer | h} [${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="create"))}

View file

@ -0,0 +1,9 @@
<%inherit file="/base/customer.mako" />
<ul id="customers">
% for customer in c.customers:
<li>${customer | h} [${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"))}

View file

@ -1,6 +1,6 @@
<%inherit file="/base/customer.mako" /> <%inherit file="/base/customer.mako" />
${h.form(h.url_for(action='process_create'))} ${h.form(h.url_for(action='create'))}
<fieldset id="createcustomerform"> <fieldset id="createcustomerform">
<label for="firstname">${_('First name:')}</label><br /> <label for="firstname">${_('First name:')}</label><br />
${h.text('firstname')}<br /> ${h.text('firstname')}<br />

View file

@ -0,0 +1,14 @@
<%inherit file="/base/customer.mako" />
<table>
<tbody>
<tr>
<th>${_('First name:')}</th><td>${c.customer.person.firstname}</td>
</tr>
<tr>
<th>${_('Last name:')}</th><td>${c.customer.person.lastname}</td>
</tr>
</tbody>
</table>
<%def name="heading()"><h1>${_('View customer')}</h1></%def>