CRUDKit update.
This commit is contained in:
parent
de29d45106
commit
f1fa1f2407
8 changed files with 391 additions and 44 deletions
|
|
@ -14,36 +14,40 @@ OPERATORS = {
|
|||
}
|
||||
|
||||
class CRUDSpec:
|
||||
def __init__(self, model, params):
|
||||
def __init__(self, model, params, root_alias):
|
||||
self.model = model
|
||||
self.params = params
|
||||
self.root_alias = root_alias
|
||||
self.eager_paths: Set[Tuple[str, ...]] = set()
|
||||
self.join_paths: List[Tuple[object, InstrumentedAttribute, object]] = []
|
||||
self.alias_map: Dict[Tuple[str, ...], object] = {}
|
||||
|
||||
def _resolve_column(self, path: str):
|
||||
current_model = self.model
|
||||
current_alias = self.model
|
||||
current_alias = self.root_alias
|
||||
parts = path.split('.')
|
||||
join_path = []
|
||||
join_path: list[str] = []
|
||||
|
||||
for i, attr in enumerate(parts):
|
||||
if not hasattr(current_model, attr):
|
||||
try:
|
||||
attr_obj = getattr(current_alias, attr)
|
||||
except AttributeError:
|
||||
return None, None
|
||||
attr_obj = getattr(current_model, attr)
|
||||
if isinstance(attr_obj, InstrumentedAttribute):
|
||||
if hasattr(attr_obj.property, 'direction'):
|
||||
join_path.append(attr)
|
||||
path_key = tuple(join_path)
|
||||
alias = self.alias_map.get(path_key)
|
||||
if not alias:
|
||||
alias = aliased(attr_obj.property.mapper.class_)
|
||||
self.alias_map[path_key] = alias
|
||||
self.join_paths.append((current_alias, attr_obj, alias))
|
||||
current_model = attr_obj.property.mapper.class_
|
||||
current_alias = alias
|
||||
else:
|
||||
return getattr(current_alias, attr), tuple(join_path) if join_path else None
|
||||
|
||||
prop = getattr(attr_obj, "property", None)
|
||||
if prop is not None and hasattr(prop, "direction"):
|
||||
join_path.append(attr)
|
||||
path_key = tuple(join_path)
|
||||
alias = self.alias_map.get(path_key)
|
||||
if not alias:
|
||||
alias = aliased(prop.mapper.class_)
|
||||
self.alias_map[path_key] = alias
|
||||
self.join_paths.append((current_alias, attr_obj, alias))
|
||||
current_alias = alias
|
||||
continue
|
||||
|
||||
if isinstance(attr_obj, InstrumentedAttribute) or hasattr(attr_obj, "clauses"):
|
||||
return attr_obj, tuple(join_path) if join_path else None
|
||||
|
||||
return None, None
|
||||
|
||||
def parse_filters(self):
|
||||
|
|
@ -90,19 +94,16 @@ class CRUDSpec:
|
|||
offset = int(self.params.get('offset', 0))
|
||||
return limit, offset
|
||||
|
||||
def get_eager_loads(self):
|
||||
def get_eager_loads(self, root_alias):
|
||||
loads = []
|
||||
for path in self.eager_paths:
|
||||
current = self.model
|
||||
current = root_alias
|
||||
loader = None
|
||||
for attr in path:
|
||||
attr_obj = getattr(current, attr)
|
||||
if loader is None:
|
||||
loader = joinedload(attr_obj)
|
||||
else:
|
||||
loader = loader.joinedload(attr_obj)
|
||||
current = attr_obj.property.mapper.class_
|
||||
if loader:
|
||||
for name in path:
|
||||
rel_attr = getattr(current, name)
|
||||
loader = (joinedload(rel_attr) if loader is None else loader.joinedload(name))
|
||||
current = rel_attr.property.mapper.class_
|
||||
if loader is not None:
|
||||
loads.append(loader)
|
||||
return loads
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue