2008-10-06 00:32:59 +02:00
|
|
|
"""Data model for pyalchemybiz."""
|
2008-10-05 23:02:10 +02:00
|
|
|
import logging
|
2008-10-05 17:23:54 +02:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
|
|
from pyalchemybiz.model import meta
|
2008-10-05 23:02:10 +02:00
|
|
|
from pyalchemybiz.model import person, customer, product
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2008-10-05 17:23:54 +02:00
|
|
|
|
2008-10-06 00:32:59 +02:00
|
|
|
|
2008-10-05 17:23:54 +02:00
|
|
|
def init_model(engine):
|
|
|
|
"""Call me before using any of the tables or classes in the model."""
|
2008-10-06 00:32:59 +02:00
|
|
|
|
2009-03-08 14:40:52 +01:00
|
|
|
sm = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
|
2008-10-05 17:23:54 +02:00
|
|
|
|
|
|
|
meta.engine = engine
|
|
|
|
meta.Session = orm.scoped_session(sm)
|
2009-03-07 12:45:24 +01:00
|
|
|
|
2008-10-05 23:02:10 +02:00
|
|
|
person.t_person = sa.Table(
|
|
|
|
'person', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
customer.t_customer = sa.Table(
|
|
|
|
'customer', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
product.t_producttype = sa.Table(
|
|
|
|
'producttype', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
product.t_product = sa.Table(
|
|
|
|
'product', meta.metadata, autoload=True, autoload_with=engine)
|
2008-10-05 17:23:54 +02:00
|
|
|
|
2008-10-05 23:02:10 +02:00
|
|
|
orm.mapper(person.Person, person.t_person)
|
2008-10-06 00:32:59 +02:00
|
|
|
orm.mapper(customer.Customer, customer.t_customer, properties={
|
|
|
|
'person': orm.relation(person.Person, backref='customer')})
|
2008-10-05 23:02:10 +02:00
|
|
|
orm.mapper(product.ProductType, product.t_producttype)
|
2008-10-06 00:32:59 +02:00
|
|
|
orm.mapper(product.Product, product.t_product, properties={
|
|
|
|
'producttype': orm.relation(product.ProductType,
|
|
|
|
backref='products')})
|