vigiboard / vigiboard / model / __init__.py @ 20367931
History | View | Annotate | Download (2.33 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""The application's model objects"""
|
3 |
|
4 |
from zope.sqlalchemy import ZopeTransactionExtension |
5 |
from sqlalchemy.orm import scoped_session, sessionmaker |
6 |
#from sqlalchemy import MetaData
|
7 |
from sqlalchemy.ext.declarative import declarative_base |
8 |
|
9 |
# Global session manager: DBSession() returns the Thread-local
|
10 |
# session object appropriate for the current web request.
|
11 |
maker = sessionmaker(autoflush=True, autocommit=False, |
12 |
extension=ZopeTransactionExtension()) |
13 |
DBSession = scoped_session(maker) |
14 |
|
15 |
# Base class for all of our model classes: By default, the data model is
|
16 |
# defined with SQLAlchemy's declarative extension, but if you need more
|
17 |
# control, you can switch to the traditional method.
|
18 |
DeclarativeBase = declarative_base() |
19 |
|
20 |
# There are two convenient ways for you to spare some typing.
|
21 |
# You can have a query property on all your model classes by doing this:
|
22 |
# DeclarativeBase.query = DBSession.query_property()
|
23 |
# Or you can use a session-aware mapper as it was used in TurboGears 1:
|
24 |
# DeclarativeBase = declarative_base(mapper=DBSession.mapper)
|
25 |
|
26 |
# Global metadata.
|
27 |
# The default metadata is the one from the declarative base.
|
28 |
metadata = DeclarativeBase.metadata |
29 |
|
30 |
# If you have multiple databases with overlapping table names, you'll need a
|
31 |
# metadata for each database. Feel free to rename 'metadata2'.
|
32 |
#metadata2 = MetaData()
|
33 |
|
34 |
#####
|
35 |
# Generally you will not want to define your table's mappers, and data objects
|
36 |
# here in __init__ but will want to create modules them in the model directory
|
37 |
# and import them at the bottom of this file.
|
38 |
#
|
39 |
######
|
40 |
|
41 |
def init_model(engine): |
42 |
"""Call me before using any of the tables or classes in the model."""
|
43 |
|
44 |
DBSession.configure(bind=engine) |
45 |
# If you are using reflection to introspect your database and create
|
46 |
# table objects for you, your tables must be defined and mapped inside
|
47 |
# the init_model function, so that the engine is available if you
|
48 |
# use the model outside tg2, you need to make sure this is called before
|
49 |
# you use the model.
|
50 |
|
51 |
#
|
52 |
# See the following example:
|
53 |
|
54 |
#global t_reflected
|
55 |
|
56 |
#t_reflected = Table("Reflected", metadata,
|
57 |
# autoload=True, autoload_with=engine)
|
58 |
|
59 |
#mapper(Reflected, t_reflected)
|
60 |
|
61 |
# Import your model modules here.
|
62 |
from vigiboard.model.auth import User, Group, Permission |
63 |
from vigiboard.model.vigiboard_bdd import * |