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'))}