Skip to content

Commit

Permalink
Setup scopefunc when initialising Flask-SQLAlchemy. Does not need t…
Browse files Browse the repository at this point in the history
…o add another argument to `create_scoped_session`.
  • Loading branch information
s0undt3ch committed Sep 4, 2011
1 parent ef2e2bf commit 6ec9d82
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions flaskext/sqlalchemy.py
Expand Up @@ -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()
Expand All @@ -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
)
Expand Down

0 comments on commit 6ec9d82

Please sign in to comment.