diff --git a/pyalchemybiz/controllers/customer.py b/pyalchemybiz/controllers/customer.py index 74b3cd5..bc6f059 100644 --- a/pyalchemybiz/controllers/customer.py +++ b/pyalchemybiz/controllers/customer.py @@ -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') diff --git a/pyalchemybiz/controllers/index.py b/pyalchemybiz/controllers/index.py index ec78e87..503566a 100644 --- a/pyalchemybiz/controllers/index.py +++ b/pyalchemybiz/controllers/index.py @@ -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') diff --git a/pyalchemybiz/templates/derived/customer/edit.mako b/pyalchemybiz/templates/derived/customer/edit.mako index 3290995..d17521e 100644 --- a/pyalchemybiz/templates/derived/customer/edit.mako +++ b/pyalchemybiz/templates/derived/customer/edit.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'))}

${h.text('firstname', c.customer.person.firstname)}
diff --git a/pyalchemybiz/templates/derived/customer/index.mako b/pyalchemybiz/templates/derived/customer/index.mako deleted file mode 100644 index f8b950e..0000000 --- a/pyalchemybiz/templates/derived/customer/index.mako +++ /dev/null @@ -1,9 +0,0 @@ -<%inherit file="/base/customer.mako" /> - - - -${h.link_to(_('create new customer'), h.url_for(action="create"))} diff --git a/pyalchemybiz/templates/derived/customer/list.mako b/pyalchemybiz/templates/derived/customer/list.mako new file mode 100644 index 0000000..dcc2ef1 --- /dev/null +++ b/pyalchemybiz/templates/derived/customer/list.mako @@ -0,0 +1,9 @@ +<%inherit file="/base/customer.mako" /> + + + +${h.link_to(_('create new customer'), h.url_for(action="new"))} diff --git a/pyalchemybiz/templates/derived/customer/create.mako b/pyalchemybiz/templates/derived/customer/new.mako similarity index 89% rename from pyalchemybiz/templates/derived/customer/create.mako rename to pyalchemybiz/templates/derived/customer/new.mako index 2590188..d906ab1 100644 --- a/pyalchemybiz/templates/derived/customer/create.mako +++ b/pyalchemybiz/templates/derived/customer/new.mako @@ -1,6 +1,6 @@ <%inherit file="/base/customer.mako" /> -${h.form(h.url_for(action='process_create'))} +${h.form(h.url_for(action='create'))}

${h.text('firstname')}
diff --git a/pyalchemybiz/templates/derived/customer/view.mako b/pyalchemybiz/templates/derived/customer/view.mako new file mode 100644 index 0000000..d574909 --- /dev/null +++ b/pyalchemybiz/templates/derived/customer/view.mako @@ -0,0 +1,14 @@ +<%inherit file="/base/customer.mako" /> + + + + + + + + + + +
${_('First name:')}${c.customer.person.firstname}
${_('Last name:')}${c.customer.person.lastname}
+ +<%def name="heading()">

${_('View customer')}