Let subclass of MasterView override the database Session it uses.

This commit is contained in:
Lance Edgar 2015-11-20 20:11:15 -06:00
parent 89b443c442
commit a33b48dde3

View file

@ -35,7 +35,6 @@ import formalchemy
from pyramid.renderers import get_renderer, render_to_response
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from tailbone.db import Session
from tailbone.views import View
from tailbone.newgrids import filters, AlchemyGrid, GridAction
from tailbone.forms import AlchemyForm
@ -58,6 +57,15 @@ class MasterView(View):
row_attrs = {}
cell_attrs = {}
@property
def Session(self):
"""
SQLAlchemy scoped session to use when querying the database. Defaults
to ``tailbone.db.Session``.
"""
from tailbone.db import Session
return Session
##############################
# Available Views
##############################
@ -413,7 +421,7 @@ class MasterView(View):
override this method; override :meth:`query()` instead.
"""
if session is None:
session = Session()
session = self.Session()
return self.query(session)
def query(self, session):
@ -453,7 +461,7 @@ class MasterView(View):
doing a database lookup. If the instance cannot be found, raises 404.
"""
key = self.request.matchdict[self.get_model_key()]
instance = Session.query(self.model_class).get(key)
instance = self.Session.query(self.model_class).get(key)
if not instance:
raise HTTPNotFound()
return instance
@ -484,7 +492,7 @@ class MasterView(View):
"""
Make a FormAlchemy fieldset for the given model instance.
"""
kwargs.setdefault('session', Session())
kwargs.setdefault('session', self.Session())
kwargs.setdefault('request', self.request)
fieldset = formalchemy.FieldSet(instance, **kwargs)
fieldset.prettify = prettify
@ -525,8 +533,8 @@ class MasterView(View):
"""
# Flush immediately to force any pending integrity errors etc.; that
# way we don't set flash message until we know we have success.
Session.delete(instance)
Session.flush()
self.Session.delete(instance)
self.Session.flush()
def get_after_delete_url(self, instance):
"""