123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
-
- from sqlalchemy import engine_from_config
- from sqlalchemy.event import listen
- from sqlalchemy.orm import sessionmaker
- from sqlalchemy.orm import configure_mappers
- import zope.sqlalchemy
- from .meta import DeclarativeBase
- from tracim_backend.models.revision_protection import prevent_content_revision_delete
-
-
- from tracim_backend.models.auth import User, Group, Permission
- from tracim_backend.models.data import Content, ContentRevisionRO
-
-
-
- configure_mappers()
-
-
- def get_engine(settings, prefix='sqlalchemy.'):
- return engine_from_config(settings, prefix)
-
-
- def get_session_factory(engine):
- factory = sessionmaker(expire_on_commit=False)
- factory.configure(bind=engine)
- return factory
-
-
- def get_tm_session(session_factory, transaction_manager):
- """
- Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
-
- This function will hook the _session to the transaction manager which
- will take care of committing any changes.
-
- - When using pyramid_tm it will automatically be committed or aborted
- depending on whether an exception is raised.
-
- - When using scripts you should wrap the _session in a manager yourself.
- For example::
-
- import transaction
-
- engine = get_engine(settings)
- session_factory = get_session_factory(engine)
- with transaction.manager:
- dbsession = get_tm_session(session_factory, transaction.manager)
-
- """
- dbsession = session_factory()
-
-
-
-
-
-
-
-
-
-
- zope.sqlalchemy.register(
- dbsession,
- transaction_manager=transaction_manager,
- keep_session=True,
- )
- listen(dbsession, 'before_flush', prevent_content_revision_delete)
- return dbsession
-
-
- def includeme(config):
- """
- Initialize the model for a Pyramid app.
-
- Activate this setup using ``config.include('tracim.models')``.
-
- """
- settings = config.get_settings()
- settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
-
-
- config.include('pyramid_tm')
-
-
- config.include('pyramid_retry')
-
- session_factory = get_session_factory(get_engine(settings))
- config.registry['dbsession_factory'] = session_factory
-
-
- config.add_request_method(
-
- lambda r: get_tm_session(session_factory, r.tm),
- 'dbsession',
- reify=True
- )
|