Fixes on dot resolution not understanding root aliases.
This commit is contained in:
parent
60708164fa
commit
7cbe200801
2 changed files with 53 additions and 44 deletions
|
|
@ -18,37 +18,44 @@ class CRUDService(Generic[T]):
|
||||||
def get_query(self):
|
def get_query(self):
|
||||||
if self.polymorphic:
|
if self.polymorphic:
|
||||||
poly_model = with_polymorphic(self.model, '*')
|
poly_model = with_polymorphic(self.model, '*')
|
||||||
return self.session.query(poly_model)
|
return self.session.query(poly_model), poly_model
|
||||||
else:
|
else:
|
||||||
base_only = with_polymorphic(self.model, [], flat=True)
|
base_only = with_polymorphic(self.model, [], flat=True)
|
||||||
return self.session.query(base_only)
|
return self.session.query(base_only), base_only
|
||||||
|
|
||||||
def get(self, id: int, include_deleted: bool = False) -> T | None:
|
def get(self, id: int, include_deleted: bool = False) -> T | None:
|
||||||
obj = self.get_query().filter_by(id=id).first()
|
query, root_alias = self.get_query()
|
||||||
if obj is None:
|
|
||||||
return None
|
if self.supports_soft_delete and not include_deleted:
|
||||||
if self.supports_soft_delete and not include_deleted and obj.is_deleted:
|
query = query.filter(getattr(root_alias, "is_deleted") == False)
|
||||||
return None
|
|
||||||
return obj
|
query = query.filter(getattr(root_alias, "id") == id)
|
||||||
|
|
||||||
|
obj = query.first()
|
||||||
|
return obj or None
|
||||||
|
|
||||||
def list(self, params=None) -> list[T]:
|
def list(self, params=None) -> list[T]:
|
||||||
query = self.get_query()
|
query, root_alias = self.get_query()
|
||||||
|
|
||||||
if params:
|
if params:
|
||||||
if self.supports_soft_delete:
|
if self.supports_soft_delete:
|
||||||
include_deleted = False
|
|
||||||
include_deleted = _is_truthy(params.get('include_deleted'))
|
include_deleted = _is_truthy(params.get('include_deleted'))
|
||||||
if not include_deleted:
|
if not include_deleted:
|
||||||
query = query.filter(self.model.is_deleted == False)
|
query = query.filter(getattr(root_alias, "is_deleted") == False)
|
||||||
spec = CRUDSpec(self.model, params)
|
|
||||||
|
spec = CRUDSpec(self.model, params, root_alias)
|
||||||
filters = spec.parse_filters()
|
filters = spec.parse_filters()
|
||||||
order_by = spec.parse_sort()
|
order_by = spec.parse_sort()
|
||||||
limit, offset = spec.parse_pagination()
|
limit, offset = spec.parse_pagination()
|
||||||
|
|
||||||
for parent, relationship_attr, alias in spec.get_join_paths():
|
for parent_alias, relationship_attr, target_alias in spec.get_join_paths():
|
||||||
query = query.join(alias, relationship_attr.of_type(alias), isouter=True)
|
query = query.join(
|
||||||
|
target_alias,
|
||||||
|
relationship_attr.of_type(target_alias),
|
||||||
|
isouter=True
|
||||||
|
)
|
||||||
|
|
||||||
for eager in spec.get_eager_loads():
|
for eager in spec.get_eager_loads(root_alias):
|
||||||
query = query.options(eager)
|
query = query.options(eager)
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
|
|
@ -56,6 +63,7 @@ class CRUDService(Generic[T]):
|
||||||
if order_by:
|
if order_by:
|
||||||
query = query.order_by(*order_by)
|
query = query.order_by(*order_by)
|
||||||
query = query.offset(offset).limit(limit)
|
query = query.offset(offset).limit(limit)
|
||||||
|
|
||||||
return query.all()
|
return query.all()
|
||||||
|
|
||||||
def create(self, data: dict, actor=None) -> T:
|
def create(self, data: dict, actor=None) -> T:
|
||||||
|
|
|
||||||
|
|
@ -14,36 +14,40 @@ OPERATORS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
class CRUDSpec:
|
class CRUDSpec:
|
||||||
def __init__(self, model, params):
|
def __init__(self, model, params, root_alias):
|
||||||
self.model = model
|
self.model = model
|
||||||
self.params = params
|
self.params = params
|
||||||
|
self.root_alias = root_alias
|
||||||
self.eager_paths: Set[Tuple[str, ...]] = set()
|
self.eager_paths: Set[Tuple[str, ...]] = set()
|
||||||
self.join_paths: List[Tuple[object, InstrumentedAttribute, object]] = []
|
self.join_paths: List[Tuple[object, InstrumentedAttribute, object]] = []
|
||||||
self.alias_map: Dict[Tuple[str, ...], object] = {}
|
self.alias_map: Dict[Tuple[str, ...], object] = {}
|
||||||
|
|
||||||
def _resolve_column(self, path: str):
|
def _resolve_column(self, path: str):
|
||||||
current_model = self.model
|
current_alias = self.root_alias
|
||||||
current_alias = self.model
|
|
||||||
parts = path.split('.')
|
parts = path.split('.')
|
||||||
join_path = []
|
join_path: list[str] = []
|
||||||
|
|
||||||
for i, attr in enumerate(parts):
|
for i, attr in enumerate(parts):
|
||||||
if not hasattr(current_model, attr):
|
try:
|
||||||
|
attr_obj = getattr(current_alias, attr)
|
||||||
|
except AttributeError:
|
||||||
return None, None
|
return None, None
|
||||||
attr_obj = getattr(current_model, attr)
|
|
||||||
if isinstance(attr_obj, InstrumentedAttribute):
|
prop = getattr(attr_obj, "property", None)
|
||||||
if hasattr(attr_obj.property, 'direction'):
|
if prop is not None and hasattr(prop, "direction"):
|
||||||
join_path.append(attr)
|
join_path.append(attr)
|
||||||
path_key = tuple(join_path)
|
path_key = tuple(join_path)
|
||||||
alias = self.alias_map.get(path_key)
|
alias = self.alias_map.get(path_key)
|
||||||
if not alias:
|
if not alias:
|
||||||
alias = aliased(attr_obj.property.mapper.class_)
|
alias = aliased(prop.mapper.class_)
|
||||||
self.alias_map[path_key] = alias
|
self.alias_map[path_key] = alias
|
||||||
self.join_paths.append((current_alias, attr_obj, alias))
|
self.join_paths.append((current_alias, attr_obj, alias))
|
||||||
current_model = attr_obj.property.mapper.class_
|
current_alias = alias
|
||||||
current_alias = alias
|
continue
|
||||||
else:
|
|
||||||
return getattr(current_alias, attr), tuple(join_path) if join_path else None
|
if isinstance(attr_obj, InstrumentedAttribute) or hasattr(attr_obj, "clauses"):
|
||||||
|
return attr_obj, tuple(join_path) if join_path else None
|
||||||
|
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
def parse_filters(self):
|
def parse_filters(self):
|
||||||
|
|
@ -90,19 +94,16 @@ class CRUDSpec:
|
||||||
offset = int(self.params.get('offset', 0))
|
offset = int(self.params.get('offset', 0))
|
||||||
return limit, offset
|
return limit, offset
|
||||||
|
|
||||||
def get_eager_loads(self):
|
def get_eager_loads(self, root_alias):
|
||||||
loads = []
|
loads = []
|
||||||
for path in self.eager_paths:
|
for path in self.eager_paths:
|
||||||
current = self.model
|
current = root_alias
|
||||||
loader = None
|
loader = None
|
||||||
for attr in path:
|
for name in path:
|
||||||
attr_obj = getattr(current, attr)
|
rel_attr = getattr(current, name)
|
||||||
if loader is None:
|
loader = (joinedload(rel_attr) if loader is None else loader.joinedload(name))
|
||||||
loader = joinedload(attr_obj)
|
current = rel_attr.property.mapper.class_
|
||||||
else:
|
if loader is not None:
|
||||||
loader = loader.joinedload(attr_obj)
|
|
||||||
current = attr_obj.property.mapper.class_
|
|
||||||
if loader:
|
|
||||||
loads.append(loader)
|
loads.append(loader)
|
||||||
return loads
|
return loads
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue