model cleanup

* add __repr__() to all current module objects (fixes #10)
 * pep8 fixes in all Python files (addresses #9)
 * create subdirectory for customer templates (addresses #2)


git-svn-id: file:///var/www/wwwusers/usr01/svn/pyalchemybiz/trunk@8 389c73d4-bf09-4d3d-a15e-f94a37d0667a
This commit is contained in:
Jan Dittberner 2008-10-05 22:32:59 +00:00
parent 1228fcef3c
commit b53e8c48df
20 changed files with 67 additions and 25 deletions

View file

@ -12,6 +12,7 @@ import pyalchemybiz.lib.app_globals as app_globals
import pyalchemybiz.lib.helpers import pyalchemybiz.lib.helpers
from pyalchemybiz.config.routing import make_map from pyalchemybiz.config.routing import make_map
def load_environment(global_conf, app_conf): def load_environment(global_conf, app_conf):
"""Configure the Pylons environment via the ``pylons.config`` """Configure the Pylons environment via the ``pylons.config``
object object

View file

@ -12,6 +12,7 @@ from pylons.wsgiapp import PylonsApp
from pyalchemybiz.config.environment import load_environment from pyalchemybiz.config.environment import load_environment
def make_app(global_conf, full_stack=True, **app_conf): def make_app(global_conf, full_stack=True, **app_conf):
"""Create a Pylons WSGI application and return it """Create a Pylons WSGI application and return it

View file

@ -7,6 +7,7 @@ refer to the routes manual at http://routes.groovie.org/docs/
from pylons import config from pylons import config
from routes import Mapper from routes import Mapper
def make_map(): def make_map():
"""Create, configure and return the routes Mapper""" """Create, configure and return the routes Mapper"""
map = Mapper(directory=config['pylons.paths']['controllers'], map = Mapper(directory=config['pylons.paths']['controllers'],

View file

@ -5,16 +5,17 @@ from pyalchemybiz.model import customer, meta
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class CustomerController(BaseController): class CustomerController(BaseController):
def index(self): def index(self):
sess = meta.Session() sess = meta.Session()
cust_q = sess.query(customer.Customer) cust_q = sess.query(customer.Customer)
c.customers = cust_q.all() c.customers = cust_q.all()
return render('/customer.mako') return render('/customer/index.mako')
def edit(self, id): def edit(self, id):
sess = meta.Session() sess = meta.Session()
cust_q = sess.query(customer.Customer) cust_q = sess.query(customer.Customer)
c.customer = cust_q.filter(customer.Customer.id==id).one() c.customer = cust_q.filter(customer.Customer.id==id).one()
return c.customer return render('/customer/edit.mako')

View file

@ -6,6 +6,7 @@ from pylons.middleware import error_document_template, media_path
from pyalchemybiz.lib.base import * from pyalchemybiz.lib.base import *
class ErrorController(BaseController): class ErrorController(BaseController):
"""Generates error documents as and when they are required. """Generates error documents as and when they are required.
@ -14,8 +15,9 @@ class ErrorController(BaseController):
This behaviour can be altered by changing the parameters to the This behaviour can be altered by changing the parameters to the
ErrorDocuments middleware in your config/middleware.py file. ErrorDocuments middleware in your config/middleware.py file.
""" """
def document(self): def document(self):
"""Render the error document""" """Render the error document"""
page = error_document_template % \ page = error_document_template % \

View file

@ -4,6 +4,7 @@ from pyalchemybiz.lib.base import *
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class IndexController(BaseController): class IndexController(BaseController):
def index(self): def index(self):

View file

@ -1,5 +1,6 @@
from pyalchemybiz.lib.base import * from pyalchemybiz.lib.base import *
class TemplateController(BaseController): class TemplateController(BaseController):
def view(self, url): def view(self, url):

View file

@ -1,6 +1,7 @@
"""The application's Globals object""" """The application's Globals object"""
from pylons import config from pylons import config
class Globals(object): class Globals(object):
"""Globals acts as a container for objects available throughout the """Globals acts as a container for objects available throughout the
life of the application life of the application

View file

@ -16,6 +16,7 @@ import pyalchemybiz.model as model
from pyalchemybiz.model import meta from pyalchemybiz.model import meta
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
class BaseController(WSGIController): class BaseController(WSGIController):
def __call__(self, environ, start_response): def __call__(self, environ, start_response):

View file

@ -1,3 +1,4 @@
"""Data model for pyalchemybiz."""
import logging import logging
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
@ -7,9 +8,10 @@ from pyalchemybiz.model import person, customer, product
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def init_model(engine): def init_model(engine):
"""Call me before using any of the tables or classes in the model.""" """Call me before using any of the tables or classes in the model."""
sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine) sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine)
meta.engine = engine meta.engine = engine
@ -24,9 +26,9 @@ def init_model(engine):
'product', meta.metadata, autoload=True, autoload_with=engine) 'product', meta.metadata, autoload=True, autoload_with=engine)
orm.mapper(person.Person, person.t_person) orm.mapper(person.Person, person.t_person)
orm.mapper(customer.Customer, customer.t_customer) orm.mapper(customer.Customer, customer.t_customer, properties={
customer.Customer.person = orm.relation(person.Person) 'person': orm.relation(person.Person, backref='customer')})
orm.mapper(product.ProductType, product.t_producttype) orm.mapper(product.ProductType, product.t_producttype)
orm.mapper(product.Product, product.t_product) orm.mapper(product.Product, product.t_product, properties={
product.Product.producttype = orm.relation( 'producttype': orm.relation(product.ProductType,
product.Product, backref=orm.backref('products', lazy='dynamic')) backref='products')})

View file

@ -1,5 +1,12 @@
"""Customer related model elements."""
t_customer = None t_customer = None
class Customer(object): class Customer(object):
def __str__(self): """Customer model class."""
return "%s %s" % (self.firstname, self.lastname)
def __repr__(self):
if self.id is None:
return "<Customer: new %s>" % (self.person)
else:
return "<Customer: %d %s>" % (self.id, self.person)

View file

@ -9,6 +9,6 @@ engine = None
# SQLAlchemy session manager. Updated by model.init_model(). # SQLAlchemy session manager. Updated by model.init_model().
Session = None Session = None
# Global metadata. If you have multiple databases with overlapping table # Global metadata. If you have multiple databases with overlapping table
# names, you'll need a metadata for each database. # names, you'll need a metadata for each database.
metadata = MetaData() metadata = MetaData()

View file

@ -1,5 +1,11 @@
t_person = None t_person = None
class Person(object): class Person(object):
def __str__(self):
return "%s %s" % (self.firstname, self.lastname) def __repr__(self):
if self.id is None:
return "<Person: new %s %s>" % (self.firstname, self.lastname)
else:
return "<Person: %d %s %s>" % (self.id, self.firstname,
self.lastname)

View file

@ -1,8 +1,21 @@
t_producttype = None t_producttype = None
t_product = None t_product = None
class ProductType(object): class ProductType(object):
pass
def __repr__(self):
if self.id is None:
return "<ProductType: new %s>" % (self.name)
else:
return "<ProductType: %d %s>" % (self.id, self.name)
class Product(object): class Product(object):
pass
def __repr__(self):
if self.id is None:
return "<Product: new %s %s>" % (self.name, self.producttype)
else:
return "<Product: %d %s %s>" % (self.id, self.name,
self.producttype)

View file

@ -1,9 +0,0 @@
<%inherit file="base.mako" />
<h1>Hallo</h1>
<ul id="customers">
% for customer in c.customers:
<li>${customer.firstname}&nbsp;${customer.lastname} [${h.link_to('edit', h.url_for(id=customer.id, action="edit"))}]</li>
% endfor
</ul>

View file

@ -0,0 +1,9 @@
<%inherit file="/base.mako" />
<h1>Hallo</h1>
<ul id="customers">
% for customer in c.customers:
<li>${h.escape_once(customer)} [${h.link_to('edit', h.url_for(id=customer.id, action="edit"))}]</li>
% endfor
</ul>

View file

@ -32,6 +32,7 @@ test_file = os.path.join(conf_dir, 'test.ini')
cmd = paste.script.appinstall.SetupCommand('setup-app') cmd = paste.script.appinstall.SetupCommand('setup-app')
cmd.run([test_file]) cmd.run([test_file])
class TestController(TestCase): class TestController(TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View file

@ -1,5 +1,6 @@
from pyalchemybiz.tests import * from pyalchemybiz.tests import *
class TestCustomerController(TestController): class TestCustomerController(TestController):
def test_index(self): def test_index(self):

View file

@ -1,5 +1,6 @@
from pyalchemybiz.tests import * from pyalchemybiz.tests import *
class TestIndexController(TestController): class TestIndexController(TestController):
def test_index(self): def test_index(self):

View file

@ -12,6 +12,7 @@ from pyalchemybiz.config.environment import load_environment
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def setup_config(command, filename, section, vars): def setup_config(command, filename, section, vars):
"""Place any commands to setup pyalchemybiz here""" """Place any commands to setup pyalchemybiz here"""
conf = appconfig('config:' + filename) conf = appconfig('config:' + filename)