Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Nov 6, 2024
1 parent da79d65 commit a305902
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion django_tables2/columns/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`.
Expand Down
17 changes: 9 additions & 8 deletions django_tables2/columns/booleancolumn.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -40,26 +43,24 @@ 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()}
attrs.update(self.attrs.get("span", {}))

return format_html("<span {}>{}</span>", 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)

Expand Down
4 changes: 2 additions & 2 deletions django_tables2/columns/checkboxcolumn.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -58,7 +58,7 @@ def header(self):
attrs = AttributeDict(default, **(specific or general or {}))
return mark_safe(f"<input {attrs.as_html()} />")

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"})
Expand Down
4 changes: 3 additions & 1 deletion django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Self

from django.db import models

from .base import library
Expand All @@ -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)

0 comments on commit a305902

Please sign in to comment.