Upgraded sqlservice to 2.0.x
This commit is contained in:
parent
229ea6fb5b
commit
6c1eb903f7
8 changed files with 95 additions and 91 deletions
|
|
@ -11,7 +11,8 @@ from sqlalchemy.exc import OperationalError
|
|||
from sqlalchemy.schema import MetaData, Column
|
||||
from sqlalchemy.types import Integer
|
||||
from sqlalchemy.orm.collections import InstrumentedList
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy import event, orm
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from sqlservice import ModelBase, as_declarative
|
||||
|
||||
|
|
@ -26,16 +27,51 @@ logger.info("Reading Jeeves DB structure")
|
|||
|
||||
meta = MetaData()
|
||||
try:
|
||||
# TODO: Split raw.py and reflect tables on separate module loads?
|
||||
meta.reflect(bind=db.raw_session.connection(),
|
||||
only=['ar', 'ars', 'arsh', 'arean', 'xae', 'xare', 'fr', 'kus', 'x1k',
|
||||
'oh', 'orp', 'lp', 'vg', 'xp', 'xm', 'prh', 'prl',
|
||||
'kp', 'kpw', 'cr', 'X4', 'xw', 'X1',
|
||||
'kp', 'kpw', 'cr', 'X4', 'xw', 'X1', 'jfbs', 'lrfb',
|
||||
'JAPP_EWMS_Item_Replenishment_Levels'])
|
||||
except OperationalError as e:
|
||||
logger.error("Failed to read Jeeves DB structure")
|
||||
raise e
|
||||
|
||||
|
||||
|
||||
@event.listens_for(Session, "do_orm_execute")
|
||||
def _add_filtering_criteria(execute_state):
|
||||
"""Intercept all ORM queries. Add a with_loader_criteria option to all
|
||||
of them.
|
||||
|
||||
This option applies to SELECT queries and adds a global WHERE criteria
|
||||
(or as appropriate ON CLAUSE criteria for join targets)
|
||||
to all objects of a certain class or superclass.
|
||||
|
||||
"""
|
||||
|
||||
# the with_loader_criteria automatically applies itself to
|
||||
# relationship loads as well including lazy loads. So if this is
|
||||
# a relationship load, assume the option was set up from the top level
|
||||
# query.
|
||||
|
||||
# TODO: Make configurable if repo made pub
|
||||
company_code = execute_state.execution_options.get("company_code", 1)
|
||||
|
||||
if (
|
||||
not execute_state.is_column_load
|
||||
and not execute_state.is_relationship_load
|
||||
# and not execute_state.execution_options.get("include_private", False)
|
||||
):
|
||||
execute_state.statement = execute_state.statement.options(
|
||||
orm.with_loader_criteria(
|
||||
RawBaseModel,
|
||||
lambda cls: cls.ForetagKod == company_code,
|
||||
include_aliases=True,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@as_declarative(metadata=meta)
|
||||
class RawBaseModel(ModelBase):
|
||||
""" Generalize __init__, __repr__ and to_json
|
||||
|
|
@ -62,19 +98,7 @@ class RawBaseModel(ModelBase):
|
|||
def __init__(self, data=None, **kargs):
|
||||
if data:
|
||||
data = self._map_keys(data)
|
||||
self.update(data, **kargs)
|
||||
# super(RawBaseModel, self).__init__(data=None, **kargs)
|
||||
|
||||
@classmethod
|
||||
def _base_filters(self, obj, filters=and_()):
|
||||
# This method provides base filtering, additional filtering can be done in subclasses
|
||||
# Add this method to your model if you want more filtering, otherwise leave it out
|
||||
# import and_ from sqlalchemy package
|
||||
# this is a base filter for ALL queries
|
||||
return and_(
|
||||
obj.ForetagKod == 1,
|
||||
filters
|
||||
)
|
||||
self.set(**kargs)
|
||||
|
||||
@classmethod
|
||||
def _map_columns(cls, key):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue