Created
June 21, 2017 07:43
-
-
Save khajvahmac/a1010bee0dc88d0fe9c992873cd48d6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import engine_from_config | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.orm import configure_mappers | |
from zope.component import getGlobalSiteManager | |
import zope.sqlalchemy | |
# import or define all models here to ensure they are attached to the | |
# Base.metadata prior to any initialization routines | |
from zope import interface | |
from zope.interface import implementer | |
from .main import User, Debate, Argument # noqa | |
# run configure_mappers after defining all of the models to ensure | |
# all relationships can be setup | |
configure_mappers() | |
class ISessionFactory(interface.Interface): | |
def get_session(self): | |
pass | |
@implementer(ISessionFactory) | |
class SQLAlchemySessionFactory: | |
def __init__(self, settings, prefix='sqlalchemy.'): | |
engine = engine_from_config(settings, prefix) | |
self.factory = sessionmaker() | |
self.factory.configure(bind=engine) | |
def get_session(self): | |
return self.factory() | |
def includeme(config): | |
""" | |
Initialize the model for a Pyramid app. | |
Activate this setup using ``config.include('orimsa.models')``. | |
""" | |
settings = config.get_settings() | |
# use pyramid_tm to hook the transaction lifecycle to the request | |
config.include('pyramid_tm') | |
session_factory = get_session_factory(get_engine(settings)) | |
#config.registry['dbsession_factory'] = session_factory | |
config.registry.registerUtility(SQLAlchemySessionFactory(settings), ISessionFactory, name='dbsession_factory') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment