Rocky Burt avatar Rocky Burt committed ce4c5bd

Trying to make stucco_auth easier to include

Comments (0)

Files changed (2)

     'cryptacular',
     'pyramid',
     'pyramid_beaker',
-    'pyramid_jinja2',
+    'pyramid_jinja2 >= 0.5',  # to get pyramid_jinja2.includeme support
     'stucco_evolution',
     'SQLAlchemy'
 ]

stucco_auth/__init__.py

 from stucco_auth.interfaces import IAuthRoot
 
 SESSION_FACTORY_KEY = __name__ + '.db_session_factory'
+AUTOCOMMIT_KEY = __name__ + '.autocommit_init'
 
 import logging
 log = logging.getLogger(__name__)
 
 def includeme(c):
     """Add stucco_auth views to Pyramid configurator instance."""
+
+    c.include(pyramid_jinja2.includeme)
+
     c.add_view(views.login, name='login', context=IAuthRoot,
         renderer='login.jinja2')
     c.add_view(views.login_post, name='login', context=IAuthRoot,
     c.add_view(views.logout, name='logout', context=IAuthRoot)
     c.add_static_view('static', 'stucco_auth:static')
 
+    c.add_subscriber(new_request_listener, NewRequest)
 
-def demo_app(global_config, **settings):
-    """Return the example application for stucco_auth."""
-    from stucco_auth.models import get_root
+    Session = c.registry.settings.get(SESSION_FACTORY_KEY, None)
+    if  Session is None:
+        engine = sqlalchemy.engine_from_config(c.registry.settings)
+        Session = sqlalchemy.orm.sessionmaker(bind=engine)
+        c.registry.settings[SESSION_FACTORY_KEY] = Session
 
-    engine = sqlalchemy.engine_from_config(settings)
-    Session = sqlalchemy.orm.sessionmaker(bind=engine)
-    settings[SESSION_FACTORY_KEY] = Session
-
+    autocommit = c.registry.settings.get(AUTOCOMMIT_KEY, True)
     session = Session()
     try:
         import stucco_evolution
 
         authentication_policy = AuthTktAuthenticationPolicy(
             tkt_secret.value, callback=security.lookup_groups)
-
+        c._set_authentication_policy(authentication_policy)
         authorization_policy = ACLAuthorizationPolicy()
-
-        config = Configurator(root_factory=get_root,
-                              settings=settings,
-                              authentication_policy=authentication_policy,
-                              authorization_policy=authorization_policy)
-
-        config.add_renderer('.jinja2', pyramid_jinja2.renderer_factory)
-
-        # Configure beaker session:
-        import pyramid_beaker
-        session_factory = \
-            pyramid_beaker.session_factory_from_settings(settings)
-        config.set_session_factory(session_factory)
-        config.include('stucco_auth.includeme')
-        config.add_view(context=IAuthRoot, renderer='welcome.jinja2')
-        # event handler will only work if stucco_auth.tm is being used
-        config.add_subscriber(new_request_listener, NewRequest)
-
-        app = config.make_wsgi_app()
-        tm = TM(app, Session)
-
-        # For pshell compatibility:
-        tm.registry, tm.threadlocal_manager, tm.root_factory = \
-                app.registry, app.threadlocal_manager, app.root_factory
+        c._set_authorization_policy(authorization_policy)
 
         # In case database work was done during init:
-        session.commit()
+        if autocommit:
+            session.commit()
     except:
+        if autocommit:
+            session.rollback()
         raise
-        session.rollback()
     finally:
-        session.close()
+        if autocommit:
+            session.close()
+
+
+def demo_app(global_config, **settings):
+    """Return the example application for stucco_auth."""
+    from stucco_auth.models import get_root
+
+    config = Configurator(root_factory=get_root,
+                          settings=settings)
+
+    # Configure beaker session:
+    import pyramid_beaker
+    session_factory = \
+        pyramid_beaker.session_factory_from_settings(settings)
+    config.set_session_factory(session_factory)
+    config.include(includeme)
+    config.add_view(context=IAuthRoot, renderer='welcome.jinja2')
+
+    app = config.make_wsgi_app()
+    tm = TM(app, config.registry.settings[SESSION_FACTORY_KEY])
+
+    # For pshell compatibility:
+    tm.registry, tm.threadlocal_manager, tm.root_factory = \
+        app.registry, app.threadlocal_manager, app.root_factory
 
     return tm
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.