Commits

Daniel Holth committed 278de17 Draft

less reliance on DBSession()

  • Participants
  • Parent commits 5affbd7

Comments (0)

Files changed (3)

sharder/__init__.py

     config.add_view('.views.slideshow',
                     renderer='slideshow.jinja2',
                     context=tables.Slideshow)
+    
+def augment_settings(settings):
+    """Add rasterize, jquery locations to settings"""
+    settings['rasterize'] = resource_filename('sharder', 
+                                              'support/rasterize.js')
+    settings['jquery'] = resource_filename('sharder', 
+                                           'support/jquery-1.6.4.min.js')
         
 def main(global_config, **settings):
     """ This function returns a Pyramid WSGI application.
     """
     engine = engine_from_config(settings, 'sqlalchemy.')
     tables.DBSession.configure(bind=engine)
-
-    # bw compat
-    settings['rasterize'] = resource_filename('sharder', 
-                                              'support/rasterize.js')
-    settings['jquery'] = resource_filename('sharder', 
-                                           'support/jquery-1.6.4.min.js')
+    
+    augment_settings(settings)
   
     with engine.begin() as connection:
         stucco_evolution.initialize(connection)

sharder/models.py

 
 class IntKeyTraverser(object):
     def __getitem__(self, key):
-        session = tables.DBSession
+        item = self
+        session = None
+        while session is None:
+            try:
+                session = item.request.db
+            except AttributeError:
+                item = item.__parent__
         try:
             key = int(key)
         except (ValueError, TypeError):
                 (Allow, 'group:admin', 'post') ]
     __name__ = ''
     __parent__ = None
-    __children__ = {'shards':Shards, 'shows':Shows} 
+    __children__ = {'shards':Shards, 'shows':Shows}
+    
+    def __init__(self, request):
+        self.request = request 
     
     def __getitem__(self, key):
         return self.__children__[key](name=key, parent=self)
     
 def make():
-    root = Sharder()
     def get_root(request):
-        return root
+        return Sharder(request)
     return get_root

sharder/schema.py

 
 import colander
 from . import tables
+from pyramid import threadlocal
 
 class Shard(colander.MappingSchema):
     name = colander.SchemaNode(colander.String())
        missing=0)
 
 def shard_exists(node, value):
-    if not tables.DBSession.query(tables.Shard.id).filter_by(id=value).all():
+    request = threadlocal.get_current_request()
+    if not request.db.query(tables.Shard.id).filter_by(id=value).all():
         raise colander.Invalid(node, "Shard %r does not exist" % value)
 
 class Slide(colander.MappingSchema):