use better CRUD action names in customer controller (addresses #21)
This commit is contained in:
parent
475c9de3b4
commit
f623c3d500
7 changed files with 82 additions and 42 deletions
|
@ -1,3 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
|
||||
from pyalchemybiz.lib.base import *
|
||||
|
@ -7,52 +8,77 @@ log = logging.getLogger(__name__)
|
|||
|
||||
|
||||
class CustomerController(BaseController):
|
||||
"""
|
||||
Controller for customer related operations.
|
||||
"""
|
||||
|
||||
def index(self):
|
||||
sess = meta.Session()
|
||||
cust_q = sess.query(customer.Customer)
|
||||
def list(self):
|
||||
"""
|
||||
Show a customer list.
|
||||
"""
|
||||
cust_q = meta.Session.query(customer.Customer)
|
||||
c.customers = cust_q.all()
|
||||
return render('/derived/customer/index.mako')
|
||||
return render('/derived/customer/list.mako')
|
||||
|
||||
def create(self):
|
||||
sess = meta.Session()
|
||||
return render('/derived/customer/create.mako')
|
||||
def view(self, id):
|
||||
"""
|
||||
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):
|
||||
sess = meta.Session()
|
||||
cust_q = sess.query(customer.Customer)
|
||||
c.customer = cust_q.filter(customer.Customer.id==id).one()
|
||||
"""
|
||||
Displays a form for editing customer with id.
|
||||
"""
|
||||
cust_q = meta.Session.query(customer.Customer)
|
||||
c.customer = cust_q.get(int(id))
|
||||
return render('/derived/customer/edit.mako')
|
||||
|
||||
def delete(self, id):
|
||||
sess = meta.Session()
|
||||
cust_q = sess.query(customer.Customer)
|
||||
cust = cust_q.filter(customer.Customer.id==id).one()
|
||||
sess.delete(cust.person)
|
||||
sess.delete(cust)
|
||||
sess.commit()
|
||||
"""
|
||||
Deletes a customer.
|
||||
"""
|
||||
cust_q = meta.Session.query(customer.Customer)
|
||||
cust = cust_q.get(int(id))
|
||||
meta.Session.delete(cust.person)
|
||||
meta.Session.delete(cust)
|
||||
meta.Session.commit()
|
||||
|
||||
redirect_to(action='index')
|
||||
redirect_to(action='list')
|
||||
|
||||
def process_create(self):
|
||||
sess = meta.Session()
|
||||
def create(self):
|
||||
"""
|
||||
Saves the information submitted from new() and redirects to
|
||||
list().
|
||||
"""
|
||||
cust = customer.Customer()
|
||||
sess.save(cust)
|
||||
meta.Session.add(cust)
|
||||
cust.person = person.Person()
|
||||
cust.person.firstname = request.params['firstname']
|
||||
cust.person.lastname = request.params['lastname']
|
||||
sess.save(cust.person)
|
||||
sess.commit()
|
||||
meta.Session.add(cust.person)
|
||||
meta.Session.commit()
|
||||
|
||||
redirect_to(action='index')
|
||||
redirect_to(action='list')
|
||||
|
||||
def process_edit(self, id):
|
||||
sess = meta.Session()
|
||||
cust_q = sess.query(customer.Customer)
|
||||
cust = cust_q.filter(customer.Customer.id==id).one()
|
||||
def save(self, id):
|
||||
"""
|
||||
Saves the information submitted from edit() and redirects to
|
||||
list().
|
||||
"""
|
||||
cust_q = meta.Session.query(customer.Customer)
|
||||
cust = cust_q.get(int(id))
|
||||
cust.person.firstname = request.params['firstname']
|
||||
cust.person.lastname = request.params['lastname']
|
||||
sess.add(cust.person)
|
||||
sess.commit()
|
||||
meta.Session.add(cust.person)
|
||||
meta.Session.commit()
|
||||
|
||||
redirect_to(action='index')
|
||||
redirect_to(action='list')
|
||||
|
|
|
@ -8,5 +8,5 @@ log = logging.getLogger(__name__)
|
|||
class IndexController(BaseController):
|
||||
|
||||
def index(self):
|
||||
c.menuitems = [(_("Customer management"), "customer", "index")]
|
||||
c.menuitems = [(_("Customer management"), "customer", "list")]
|
||||
return render('/derived/index/index.mako')
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<%inherit file="/base/customer.mako" />
|
||||
|
||||
${h.form(h.url_for(action='process_edit'))}
|
||||
${h.form(h.url_for(action='save'))}
|
||||
<fieldset id="editcustomerform">
|
||||
<label for="firstname">${_('First name:')}</label><br />
|
||||
${h.text('firstname', c.customer.person.firstname)}<br />
|
||||
|
|
|
@ -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"))}
|
9
pyalchemybiz/templates/derived/customer/list.mako
Normal file
9
pyalchemybiz/templates/derived/customer/list.mako
Normal 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"))}
|
|
@ -1,6 +1,6 @@
|
|||
<%inherit file="/base/customer.mako" />
|
||||
|
||||
${h.form(h.url_for(action='process_create'))}
|
||||
${h.form(h.url_for(action='create'))}
|
||||
<fieldset id="createcustomerform">
|
||||
<label for="firstname">${_('First name:')}</label><br />
|
||||
${h.text('firstname')}<br />
|
14
pyalchemybiz/templates/derived/customer/view.mako
Normal file
14
pyalchemybiz/templates/derived/customer/view.mako
Normal 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>
|
Loading…
Reference in a new issue