2023-01-12 17:31:34 +01:00
|
|
|
import contextlib
|
2023-01-03 21:36:34 +01:00
|
|
|
from dataclasses import dataclass
|
2023-09-30 08:14:02 +02:00
|
|
|
from typing import Any
|
2023-01-03 21:36:34 +01:00
|
|
|
|
2023-09-30 08:14:02 +02:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
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-09-18 21:14:47 +02:00
|
|
|
from zen_queries import queries_disabled
|
2023-09-30 08:14:02 +02:00
|
|
|
from zen_queries import render as zen_queries_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,
|
2023-10-02 20:50:39 +02:00
|
|
|
entity_name: str,
|
|
|
|
entity_name_plural: str,
|
2023-01-03 17:00:07 +01:00
|
|
|
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:
|
2023-09-18 21:14:47 +02:00
|
|
|
with queries_disabled():
|
|
|
|
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-10-02 20:50:39 +02:00
|
|
|
"entity_name": entity_name,
|
|
|
|
"entity_name_plural": entity_name_plural,
|
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,
|
|
|
|
)
|
2023-09-30 08:14:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def base_context(request: HttpRequest) -> dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Return a base context for all views.
|
|
|
|
"""
|
|
|
|
return {"site": get_current_site(request)}
|
|
|
|
|
|
|
|
|
|
|
|
def render(request, template_name, context=None):
|
|
|
|
"""
|
|
|
|
Render a template with a base context.
|
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
context = base_context(request) | context
|
2024-02-10 10:50:05 +01:00
|
|
|
|
|
|
|
# Make sure to fetch all permissions before rendering the template
|
|
|
|
# otherwise django-zen-queries will complain about database queries.
|
|
|
|
request.user.get_all_permissions()
|
|
|
|
|
2023-09-30 08:14:02 +02:00
|
|
|
return zen_queries_render(request, template_name, context)
|