Permalink
Browse files

Setup `scopefunc` when initialising Flask-SQLAlchemy. Does not need t…

…o add another argument to `create_scoped_session`.
  • Loading branch information...
s0undt3ch committed Sep 4, 2011
1 parent ef2e2bf commit 6ec9d82d99a274a129d05c39669b414c801d79d6
Showing with 12 additions and 9 deletions.
  1. +12 −9 flaskext/sqlalchemy.py
View
@@ -586,13 +586,22 @@ class User(db.Model):
.. versionadded:: 0.10
The `session_options` parameter was added.
+
+ .. versionadded:: 0.16
+ `scopefunc` is now accepted on `session_options`. It allows specifying
+ a custom function which will define the SQLAlchemy session's scoping.
"""
def __init__(self, app=None, use_native_unicode=True,
session_extensions=None, session_options=None):
self.use_native_unicode = use_native_unicode
self.session_extensions = to_list(session_extensions, []) + \
[_SignallingSessionExtension()]
+
+ session_options.set_default(
+ 'scopefunc', _request_ctx_stack.__ident_func__
+ )
+
self.session = self.create_scoped_session(session_options)
self.Model = self.make_declarative_base()
self._engine_lock = Lock()
@@ -611,17 +620,11 @@ def metadata(self):
"""Returns the metadata"""
return self.Model.metadata
- def create_scoped_session(self, options=None, scopefunc=None):
- """Helper factory method that creates a scoped session.
-
- .. versionadded:: 0.16
- The `scopefunc` parameter was added.
-
- """
+ def create_scoped_session(self, options=None):
+ """Helper factory method that creates a scoped session."""
if options is None:
options = {}
- if scopefunc is None:
- scopefunc = _request_ctx_stack.__ident_func__
+ scopefunc=options.pop('scopefunc', None)
return orm.scoped_session(
partial(_SignallingSession, self, **options), scopefunc=scopefunc
)

0 comments on commit 6ec9d82

Please sign in to comment.