From 2a8a4a25991bade36ab025535857e07ed6fe9cfc Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Fri, 23 Sep 2022 03:44:12 -0700 Subject: [PATCH 1/7] concept API for django tables --- CHANGELOG.md | 4 ++- requirements/test-env.txt | 1 + src/django_idom/components.py | 11 +++++- src/django_idom/types.py | 56 +++++++++++++++++++++++++++++- tests/test_app/components.py | 7 ++++ tests/test_app/templates/base.html | 1 + 6 files changed, 77 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 894a1390..837d0154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,9 @@ Using the following categories, list your changes in this order: ## [Unreleased] -- Nothing (Yet) +### Added + +- `django_table` component to generate HTML tables. ## [1.2.0] - 2022-09-19 diff --git a/requirements/test-env.txt b/requirements/test-env.txt index 32187f96..49e44476 100644 --- a/requirements/test-env.txt +++ b/requirements/test-env.txt @@ -1,3 +1,4 @@ django playwright twisted +django_filter diff --git a/src/django_idom/components.py b/src/django_idom/components.py index 34ce359c..64e10c38 100644 --- a/src/django_idom/components.py +++ b/src/django_idom/components.py @@ -14,7 +14,7 @@ from idom.types import VdomDict from django_idom.config import IDOM_CACHE, IDOM_VIEW_COMPONENT_IFRAMES -from django_idom.types import ViewComponentIframe +from django_idom.types import TableConfig, ViewComponentIframe # TODO: Might want to intercept href clicks and form submit events. @@ -119,6 +119,15 @@ async def async_renderer(): return rendered_view +def django_table(table_config: TableConfig): + @component + def new_component(): + print(table_config) + return None + + return new_component() + + @component def django_css(static_path: str): """Fetches a CSS static file for use within IDOM. This allows for deferred CSS loading. diff --git a/src/django_idom/types.py b/src/django_idom/types.py index 1f4bd406..f6d6274e 100644 --- a/src/django_idom/types.py +++ b/src/django_idom/types.py @@ -1,7 +1,17 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Any, Awaitable, Callable, Generic, Iterable, Optional, TypeVar, Union +from typing import ( + Any, + Awaitable, + Callable, + Generic, + Iterable, + Optional, + Tuple, + TypeVar, + Union, +) from django.db.models.base import Model from django.db.models.query import QuerySet @@ -9,6 +19,11 @@ from typing_extensions import ParamSpec +try: + from django_filters import FilterSet +except ImportError: + FilterSet = TypeVar("FilterSet") + __all__ = ["_Result", "_Params", "_Data", "IdomWebsocket", "Query", "Mutation"] _Result = TypeVar("_Result", bound=Union[Model, QuerySet[Any]]) @@ -51,3 +66,42 @@ class ViewComponentIframe: view: View | Callable args: Iterable kwargs: dict + + +@dataclass +class TableConfig: + # Will check if value exists in either the model or TableConfig class + fields: Iterable[str] + + model: Model | None = None + + # Allows for renaming columns + column_names: Iterable[Tuple[str, str]] | None = None + + # By default, all fields are sortable + sortable_fields: Iterable[str] | None = None + + # https://django-tables2.readthedocs.io/en/latest/pages/column-attributes.html#id1 + # Probably want a callable API similar to this `func(value:Any, node_type:str)`` + column_attrs: dict[Callable] | None = None + + # https://django-tables2.readthedocs.io/en/latest/pages/column-attributes.html#row-attributes + # Probably want a callable API similar to this `func(record:Model, node_type:str)`` + row_attrs: dict[Callable] | None = None + + # https://sparkbyexamples.com/pandas/pandas-sort-dataframe-by-multiple-columns/ + order_by: Iterable[str] | None = None + + # https://django-tables2.readthedocs.io/en/latest/pages/filtering.html + filterset: FilterSet | None = None + + # Zero means no pagination. + # https://docs.djangoproject.com/en/4.1/ref/paginator/#django.core.paginator.Paginator + pagination: int = 0 + + # Allows for a custom render function to change render layout + renderer: Callable | None = None + + +bs_table_column_attrs = {} +bs_table_row_attrs = {} diff --git a/tests/test_app/components.py b/tests/test_app/components.py index c103e01c..2be07114 100644 --- a/tests/test_app/components.py +++ b/tests/test_app/components.py @@ -85,6 +85,13 @@ def use_location(): ) +@component +def django_table(): + return django_idom.components.django_table( + table_config=django_idom.types.TableConfig(fields=["id", "text"]) + ) + + @component def django_css(): return html.div( diff --git a/tests/test_app/templates/base.html b/tests/test_app/templates/base.html index bea9893a..34b09eae 100644 --- a/tests/test_app/templates/base.html +++ b/tests/test_app/templates/base.html @@ -27,6 +27,7 @@