From a305902763ef484ba6396122929406ed56ca42db Mon Sep 17 00:00:00 2001 From: Jan Pieter Waagmeester Date: Wed, 6 Nov 2024 10:32:57 +0100 Subject: [PATCH] more --- django_tables2/columns/base.py | 3 ++- django_tables2/columns/booleancolumn.py | 17 +++++++++-------- django_tables2/columns/checkboxcolumn.py | 4 ++-- django_tables2/columns/datecolumn.py | 4 +++- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/django_tables2/columns/base.py b/django_tables2/columns/base.py index f5edf77f..8d37e629 100644 --- a/django_tables2/columns/base.py +++ b/django_tables2/columns/base.py @@ -1,5 +1,6 @@ from collections import OrderedDict from itertools import islice +from typing import Self from django.core.exceptions import ImproperlyConfigured from django.urls import reverse @@ -399,7 +400,7 @@ def order(self, queryset, is_descending): return (queryset, False) @classmethod - def from_field(cls, field, **kwargs): + def from_field(cls, field, **kwargs) -> Self | None: """ Return a specialized column for the model field or `None`. diff --git a/django_tables2/columns/booleancolumn.py b/django_tables2/columns/booleancolumn.py index d3bcbb28..36ea0fc5 100644 --- a/django_tables2/columns/booleancolumn.py +++ b/django_tables2/columns/booleancolumn.py @@ -1,5 +1,8 @@ +from typing import Self + from django.db import models from django.utils.html import escape, format_html +from django.utils.safestring import SafeString from ..utils import AttributeDict from .base import Column, library @@ -30,7 +33,7 @@ def __init__(self, null=False, yesno="✔,✘", **kwargs): kwargs["empty_values"] = () super().__init__(**kwargs) - def _get_bool_value(self, record, value, bound_column): + def _get_bool_value(self, record, value, bound_column) -> bool: # If record is a model, we need to check if it has choices defined. if hasattr(record, "_meta"): field = bound_column.accessor.get_field(record) @@ -40,10 +43,9 @@ def _get_bool_value(self, record, value, bound_column): if hasattr(field, "choices") and field.choices is not None and len(field.choices) > 0: value = next(val for val, name in field.choices if name == value) - value = bool(value) - return value + return bool(value) - def render(self, value, record, bound_column): + def render(self, value, record, bound_column) -> SafeString: value = self._get_bool_value(record, value, bound_column) text = self.yesno[int(not value)] attrs = {"class": str(value).lower()} @@ -51,15 +53,14 @@ def render(self, value, record, bound_column): return format_html("{}", AttributeDict(attrs).as_html(), escape(text)) - def value(self, record, value, bound_column): + def value(self, record, value, bound_column) -> str: """ Returns the content for a specific cell similarly to `.render` however without any html content. """ - value = self._get_bool_value(record, value, bound_column) - return str(value) + return str(self._get_bool_value(record, value, bound_column)) @classmethod - def from_field(cls, field, **kwargs): + def from_field(cls, field, **kwargs) -> Self | None: if isinstance(field, models.NullBooleanField): return cls(null=True, **kwargs) diff --git a/django_tables2/columns/checkboxcolumn.py b/django_tables2/columns/checkboxcolumn.py index caecb7f9..970107a3 100644 --- a/django_tables2/columns/checkboxcolumn.py +++ b/django_tables2/columns/checkboxcolumn.py @@ -1,4 +1,4 @@ -from django.utils.safestring import mark_safe +from django.utils.safestring import SafeString, mark_safe from django_tables2.utils import Accessor, AttributeDict, computed_values @@ -58,7 +58,7 @@ def header(self): attrs = AttributeDict(default, **(specific or general or {})) return mark_safe(f"") - def render(self, value, bound_column, record): + def render(self, value, bound_column, record) -> SafeString: default = {"type": "checkbox", "name": bound_column.name, "value": value} if self.is_checked(value, record): default.update({"checked": "checked"}) diff --git a/django_tables2/columns/datecolumn.py b/django_tables2/columns/datecolumn.py index ef500115..6a4a498c 100644 --- a/django_tables2/columns/datecolumn.py +++ b/django_tables2/columns/datecolumn.py @@ -1,3 +1,5 @@ +from typing import Self + from django.db import models from .base import library @@ -23,6 +25,6 @@ def __init__(self, format=None, short=True, *args, **kwargs): super().__init__(template_code=template, *args, **kwargs) @classmethod - def from_field(cls, field, **kwargs): + def from_field(cls, field, **kwargs) -> Self | None: if isinstance(field, models.DateField): return cls(**kwargs)