__init__.py 2.3KB

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