89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
|
|
from pyalchemybiz.lib.base import *
|
|
from pyalchemybiz.model import customer, meta, person
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class CustomerController(BaseController):
|
|
"""
|
|
Controller for customer related operations.
|
|
"""
|
|
|
|
def list(self):
|
|
"""
|
|
Show a customer list.
|
|
"""
|
|
cust_q = meta.Session.query(customer.Customer)
|
|
c.customers = cust_q.all()
|
|
return render('/derived/customer/list.mako')
|
|
|
|
def view(self, id):
|
|
"""
|
|
Display a customer's details.
|
|
"""
|
|
if id is None:
|
|
abort(404)
|
|
cust_q = meta.Session.query(customer.Customer)
|
|
c.customer = cust_q.get(int(id))
|
|
if c.customer is None:
|
|
abort(404)
|
|
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):
|
|
"""
|
|
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):
|
|
"""
|
|
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='list', id=None)
|
|
|
|
def create(self):
|
|
"""
|
|
Saves the information submitted from new() and redirects to
|
|
list().
|
|
"""
|
|
cust = customer.Customer()
|
|
meta.Session.add(cust)
|
|
cust.person = person.Person()
|
|
cust.person.firstname = request.params['firstname']
|
|
cust.person.lastname = request.params['lastname']
|
|
meta.Session.add(cust.person)
|
|
meta.Session.commit()
|
|
|
|
redirect_to(action='list', id=None)
|
|
|
|
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']
|
|
meta.Session.add(cust.person)
|
|
meta.Session.commit()
|
|
|
|
redirect_to(action='list', id=None)
|