forked from absent1706/sqlalchemy-mixins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.py
36 lines (29 loc) · 807 Bytes
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sqlalchemy.future import select
from sqlalchemy.orm import Session, scoped_session, Query
from .utils import classproperty
class NoSessionError(RuntimeError):
pass
class SessionMixin:
_session = None
@classmethod
def set_session(cls, session):
"""
:type session: scoped_session | Session
"""
cls._session = session
@classproperty
def session(cls):
"""
:rtype: scoped_session | Session
"""
if cls._session is not None:
return cls._session
else:
raise NoSessionError('Cant get session.'
'Please, call SaActiveRecord.set_session()')
@classproperty
def query(cls):
"""
:rtype: Query
"""
return select(cls)