CRUDKit adjustments and bug fixes.
This commit is contained in:
parent
eef7428c2f
commit
b68dbfc7ae
5 changed files with 26 additions and 13 deletions
|
|
@ -54,6 +54,9 @@ class CRUDService(Generic[T]):
|
||||||
def list(self, params=None) -> list[T]:
|
def list(self, params=None) -> list[T]:
|
||||||
query, root_alias = self.get_query()
|
query, root_alias = self.get_query()
|
||||||
|
|
||||||
|
root_fields = []
|
||||||
|
rel_field_names = {}
|
||||||
|
|
||||||
if params:
|
if params:
|
||||||
if self.supports_soft_delete:
|
if self.supports_soft_delete:
|
||||||
include_deleted = _is_truthy(params.get('include_deleted'))
|
include_deleted = _is_truthy(params.get('include_deleted'))
|
||||||
|
|
@ -73,6 +76,7 @@ class CRUDService(Generic[T]):
|
||||||
isouter=True
|
isouter=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if params:
|
||||||
root_fields, rel_field_names = spec.parse_fields()
|
root_fields, rel_field_names = spec.parse_fields()
|
||||||
|
|
||||||
if root_fields:
|
if root_fields:
|
||||||
|
|
|
||||||
|
|
@ -157,17 +157,26 @@ class CRUDSpec:
|
||||||
for path in self.eager_paths:
|
for path in self.eager_paths:
|
||||||
current = root_alias
|
current = root_alias
|
||||||
loader = None
|
loader = None
|
||||||
|
|
||||||
for idx, name in enumerate(path):
|
for idx, name in enumerate(path):
|
||||||
rel_attr = getattr(current, name)
|
rel_attr = getattr(current, name)
|
||||||
loader = (selectinload(rel_attr) if loader is None else loader.selectinload(name))
|
|
||||||
|
if loader is None:
|
||||||
|
loader = selectinload(rel_attr)
|
||||||
|
else:
|
||||||
|
loader = loader.selectinload(rel_attr)
|
||||||
|
|
||||||
|
current = rel_attr.property.mapper.class_
|
||||||
|
|
||||||
if fields_map and idx == len(path) - 1 and path in fields_map:
|
if fields_map and idx == len(path) - 1 and path in fields_map:
|
||||||
target_cls = rel_attr.property.mapper.class_
|
target_cls = current
|
||||||
cols = [getattr(target_cls, n) for n in fields_map[path] if hasattr(target_cls, n)]
|
cols = [getattr(target_cls, n) for n in fields_map[path] if hasattr(target_cls, n)]
|
||||||
if cols:
|
if cols:
|
||||||
loader = loader.load_only(*cols)
|
loader = loader.load_only(*cols)
|
||||||
current = rel_attr.property.mapper.class_
|
|
||||||
if loader is not None:
|
if loader is not None:
|
||||||
loads.append(loader)
|
loads.append(loader)
|
||||||
|
|
||||||
return loads
|
return loads
|
||||||
|
|
||||||
def get_join_paths(self):
|
def get_join_paths(self):
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ def render_field(field, value):
|
||||||
options=field.get('options', None)
|
options=field.get('options', None)
|
||||||
)
|
)
|
||||||
|
|
||||||
def render_table(objects: List[Any], columns: Optional[List[Dict[str, Any]]] = None):
|
def render_table(objects: List[Any], columns: Optional[List[Dict[str, Any]]] = None, **opts):
|
||||||
env = get_env()
|
env = get_env()
|
||||||
template = get_crudkit_template(env, 'table.html')
|
template = get_crudkit_template(env, 'table.html')
|
||||||
|
|
||||||
|
|
@ -146,7 +146,7 @@ def render_table(objects: List[Any], columns: Optional[List[Dict[str, Any]]] = N
|
||||||
cells.append({"text": text, "href": href, "class": cls})
|
cells.append({"text": text, "href": href, "class": cls})
|
||||||
disp_rows.append({"id": rd.get("id"), "cells": cells})
|
disp_rows.append({"id": rd.get("id"), "cells": cells})
|
||||||
|
|
||||||
return template.render(columns=cols, rows=disp_rows)
|
return template.render(columns=cols, rows=disp_rows, kwargs=opts)
|
||||||
|
|
||||||
def render_form(model_cls, values, session=None):
|
def render_form(model_cls, values, session=None):
|
||||||
env = get_env()
|
env = get_env()
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,8 @@ def init_index_routes(app):
|
||||||
"start_time",
|
"start_time",
|
||||||
"contact.last_name",
|
"contact.last_name",
|
||||||
"contact.first_name",
|
"contact.first_name",
|
||||||
"work_item.name"
|
"work_item.name",
|
||||||
|
"work_item.device_type.description"
|
||||||
],
|
],
|
||||||
"sort": "start_time"
|
"sort": "start_time"
|
||||||
})
|
})
|
||||||
|
|
@ -33,11 +34,10 @@ def init_index_routes(app):
|
||||||
"link": {"endpoint": "user.get_item", "params": {"id": "{contact.id}"}}},
|
"link": {"endpoint": "user.get_item", "params": {"id": "{contact.id}"}}},
|
||||||
{"field": "work_item.name", "label": "Work Item",
|
{"field": "work_item.name", "label": "Work Item",
|
||||||
"link": {"endpoint": "inventory.get_item", "params": {"id": "{work_item.id}"}}},
|
"link": {"endpoint": "inventory.get_item", "params": {"id": "{work_item.id}"}}},
|
||||||
{"field": "complete", "label": "Status",
|
{"field": "work_item.device_type.description", "label": "Device Type"}
|
||||||
"format": "yesno", "classes": {"true":"badge bg-success","false":"badge bg-warning","none":"text-muted"}},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
logs = render_table(work_logs, columns=columns)
|
logs = render_table(work_logs, columns=columns, opts={"object_class": "worklog"})
|
||||||
|
|
||||||
return render_template("index.html", logs=logs, columns=columns)
|
return render_template("index.html", logs=logs, columns=columns)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<table>
|
<table class="table table-light table-striped table-hover table-bordered border-tertiary">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
{% for col in columns %}
|
{% for col in columns %}
|
||||||
|
|
@ -9,10 +9,10 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
{% if rows %}
|
{% if rows %}
|
||||||
{% for row in rows %}
|
{% for row in rows %}
|
||||||
<tr>
|
<tr onclick="location.href='{{ url_for(kwargs['opts']['object_class'] + '.get_item', id=row.id) }}'" style="cursor: pointer;">
|
||||||
{% for cell in row.cells %}
|
{% for cell in row.cells %}
|
||||||
{% if cell.href %}
|
{% if cell.href %}
|
||||||
<td class="{{ cell.class or '' }}"><a href="{{ cell.href }}">{{ cell.text if cell.text is not none else '-' }}</a></td>
|
<td class="{{ cell.class or '' }}"><a href="{{ cell.href }}" class="link-success link-underline link-underline-opacity-0 fw-semibold">{{ cell.text if cell.text is not none else '-' }}</a></td>
|
||||||
{% else %}
|
{% else %}
|
||||||
<td class="{{ cell.class or '' }}">{{ cell.text if cell.text is not none else '-' }}</td>
|
<td class="{{ cell.class or '' }}">{{ cell.text if cell.text is not none else '-' }}</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue