__init__.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. from sqlalchemy import engine_from_config
  3. from sqlalchemy.event import listen
  4. from sqlalchemy.orm import sessionmaker
  5. from sqlalchemy.orm import configure_mappers
  6. import zope.sqlalchemy
  7. from .meta import DeclarativeBase
  8. from .revision_protection import prevent_content_revision_delete
  9. # import or define all models here to ensure they are attached to the
  10. # Base.metadata prior to any initialization routines
  11. from tracim.models.auth import User, Group, Permission
  12. from tracim.models.data import Content, ContentRevisionRO
  13. # run configure_mappers after defining all of the models to ensure
  14. # all relationships can be setup
  15. configure_mappers()
  16. def get_engine(settings, prefix='sqlalchemy.'):
  17. return engine_from_config(settings, prefix)
  18. def get_session_factory(engine):
  19. factory = sessionmaker(expire_on_commit=False)
  20. factory.configure(bind=engine)
  21. return factory
  22. def get_tm_session(session_factory, transaction_manager):
  23. """
  24. Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
  25. This function will hook the _session to the transaction manager which
  26. will take care of committing any changes.
  27. - When using pyramid_tm it will automatically be committed or aborted
  28. depending on whether an exception is raised.
  29. - When using scripts you should wrap the _session in a manager yourself.
  30. For example::
  31. import transaction
  32. engine = get_engine(settings)
  33. session_factory = get_session_factory(engine)
  34. with transaction.manager:
  35. dbsession = get_tm_session(session_factory, transaction.manager)
  36. """
  37. dbsession = session_factory()
  38. # FIXME - G.M - 02-05-2018 - Check Zope/Sqlalchemy session conf.
  39. # We use both keep_session=True for zope and
  40. # expire_on_commit=False for sessionmaker to keep session alive after
  41. # commit ( in order to not have trouble like #52 or
  42. # detached objects problems).
  43. # These problem happened because we use "commit" in our current code.
  44. # Understand what those params really mean and check if it can cause
  45. # troubles somewhere else.
  46. zope.sqlalchemy.register(
  47. dbsession,
  48. transaction_manager=transaction_manager,
  49. keep_session=True,
  50. )
  51. listen(dbsession, 'before_flush', prevent_content_revision_delete)
  52. return dbsession
  53. def includeme(config):
  54. """
  55. Initialize the model for a Pyramid app.
  56. Activate this setup using ``config.include('tracim.models')``.
  57. """
  58. settings = config.get_settings()
  59. settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
  60. # use pyramid_tm to hook the transaction lifecycle to the request
  61. config.include('pyramid_tm')
  62. # use pyramid_retry to retry a request when transient exceptions occur
  63. config.include('pyramid_retry')
  64. session_factory = get_session_factory(get_engine(settings))
  65. config.registry['dbsession_factory'] = session_factory
  66. # make request.dbsession available for use in Pyramid
  67. config.add_request_method(
  68. # r.tm is the transaction manager used by pyramid_tm
  69. lambda r: get_tm_session(session_factory, r.tm),
  70. 'dbsession',
  71. reify=True
  72. )