2023-01-12 17:31:34 +01:00
|
|
|
import contextlib
|
2023-01-03 21:36:34 +01:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2023-01-12 17:31:34 +01:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.core.paginator import Paginator
|
2023-01-03 17:00:07 +01:00
|
|
|
from django.db.models import Model
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.http import HttpResponse
|
2023-01-03 21:36:34 +01:00
|
|
|
from django.urls import reverse
|
2023-01-03 17:00:07 +01:00
|
|
|
from zen_queries import render
|
2023-01-02 23:06:00 +01:00
|
|
|
|
|
|
|
|
2023-01-03 21:36:34 +01:00
|
|
|
@dataclass
|
|
|
|
class Row:
|
|
|
|
"""
|
|
|
|
A row in a table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
data: dict[str, str]
|
|
|
|
actions: list[dict[str, str]]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class RowAction:
|
|
|
|
"""
|
|
|
|
An action that can be performed on a row in a table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
label: str
|
|
|
|
url_name: str
|
|
|
|
url_kwargs: dict[str, str]
|
|
|
|
|
|
|
|
def render(self, obj) -> dict[str, str]:
|
|
|
|
"""
|
|
|
|
Render the action as a dictionary for the given object.
|
|
|
|
"""
|
|
|
|
url = reverse(
|
|
|
|
self.url_name,
|
|
|
|
kwargs={key: getattr(obj, value) for key, value in self.url_kwargs.items()},
|
|
|
|
)
|
|
|
|
return {"label": self.label, "url": url}
|
|
|
|
|
|
|
|
|
2023-01-03 17:00:07 +01:00
|
|
|
def render_list(
|
|
|
|
request: HttpRequest,
|
|
|
|
objects: list["Model"],
|
|
|
|
columns: list[tuple[str, str]],
|
2023-01-03 21:36:34 +01:00
|
|
|
row_actions: list[RowAction] = None,
|
|
|
|
list_actions: list[tuple[str, str]] = None,
|
2023-01-12 17:31:34 +01:00
|
|
|
paginate_by: int = None,
|
2023-01-03 17:00:07 +01:00
|
|
|
) -> HttpResponse:
|
2023-01-03 21:36:34 +01:00
|
|
|
"""
|
|
|
|
Render a list of objects with a table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: List actions
|
2023-01-03 17:00:07 +01:00
|
|
|
|
2023-01-12 17:31:34 +01:00
|
|
|
total_count = len(objects)
|
|
|
|
|
|
|
|
order_by = request.GET.get("order_by")
|
|
|
|
|
|
|
|
if order_by:
|
|
|
|
with contextlib.suppress(FieldError):
|
|
|
|
objects = objects.order_by(order_by)
|
|
|
|
|
|
|
|
if paginate_by:
|
|
|
|
paginator = Paginator(object_list=objects, per_page=paginate_by)
|
|
|
|
page = paginator.get_page(request.GET.get("page"))
|
|
|
|
objects = page.object_list
|
|
|
|
|
2023-01-03 21:36:34 +01:00
|
|
|
rows = []
|
|
|
|
for obj in objects:
|
|
|
|
row = Row(
|
|
|
|
data={column: getattr(obj, column[0]) for column in columns},
|
|
|
|
actions=[action.render(obj) for action in row_actions],
|
|
|
|
)
|
|
|
|
rows.append(row)
|
2023-01-03 17:00:07 +01:00
|
|
|
|
2023-01-21 16:20:50 +01:00
|
|
|
context = {
|
2023-01-03 21:36:34 +01:00
|
|
|
"rows": rows,
|
2023-01-12 17:31:34 +01:00
|
|
|
"columns": columns,
|
2023-01-03 21:36:34 +01:00
|
|
|
"row_actions": row_actions,
|
|
|
|
"list_actions": list_actions,
|
2023-01-12 17:31:34 +01:00
|
|
|
"total_count": total_count,
|
|
|
|
"order_by": order_by,
|
2023-01-03 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:31:34 +01:00
|
|
|
if paginate_by:
|
|
|
|
context |= {
|
|
|
|
"page": page,
|
|
|
|
"is_paginated": True,
|
|
|
|
}
|
|
|
|
|
2023-01-03 17:00:07 +01:00
|
|
|
return render(
|
|
|
|
request=request,
|
|
|
|
template_name="utils/list.html",
|
|
|
|
context=context,
|
|
|
|
)
|