1 # Copyright (C) 2010, Red Hat, Inc.
2 # Written by Darryl L. Pierce
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 """The application's model objects"""
20 from zope
.sqlalchemy
import ZopeTransactionExtension
21 from sqlalchemy
.orm
import scoped_session
, sessionmaker
22 #from sqlalchemy import MetaData
23 from sqlalchemy
.ext
.declarative
import declarative_base
25 # Global session manager: DBSession() returns the Thread-local
26 # session object appropriate for the current web request.
27 maker
= sessionmaker(autoflush
=True, autocommit
=False,
28 extension
=ZopeTransactionExtension())
29 DBSession
= scoped_session(maker
)
31 # Base class for all of our model classes: By default, the data model is
32 # defined with SQLAlchemy's declarative extension, but if you need more
33 # control, you can switch to the traditional method.
34 DeclarativeBase
= declarative_base()
36 # There are two convenient ways for you to spare some typing.
37 # You can have a query property on all your model classes by doing this:
38 # DeclarativeBase.query = DBSession.query_property()
39 # Or you can use a session-aware mapper as it was used in TurboGears 1:
40 # DeclarativeBase = declarative_base(mapper=DBSession.mapper)
43 # The default metadata is the one from the declarative base.
44 metadata
= DeclarativeBase
.metadata
46 # If you have multiple databases with overlapping table names, you'll need a
47 # metadata for each database. Feel free to rename 'metadata2'.
48 #metadata2 = MetaData()
51 # Generally you will not want to define your table's mappers, and data objects
52 # here in __init__ but will want to create modules them in the model directory
53 # and import them at the bottom of this file.
57 def init_model(engine
):
58 """Call me before using any of the tables or classes in the model."""
60 DBSession
.configure(bind
=engine
)
61 # If you are using reflection to introspect your database and create
62 # table objects for you, your tables must be defined and mapped inside
63 # the init_model function, so that the engine is available if you
64 # use the model outside tg2, you need to make sure this is called before
68 # See the following example:
72 #t_reflected = Table("Reflected", metadata,
73 # autoload=True, autoload_with=engine)
75 #mapper(Reflected, t_reflected)
77 # Import your model modules here.