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 012f68e commit be61e9d
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 26 deletions.
3 changes: 1 addition & 2 deletions django_tables2/columns/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -400,7 +399,7 @@ def order(self, queryset, is_descending):
return (queryset, False)

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "Column | None":
"""
Return a specialized column for the model field or `None`.
Expand Down
4 changes: 1 addition & 3 deletions django_tables2/columns/booleancolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Self

from django.db import models
from django.utils.html import escape, format_html
from django.utils.safestring import SafeString
Expand Down Expand Up @@ -60,7 +58,7 @@ def value(self, record, value, bound_column) -> str:
return str(self._get_bool_value(record, value, bound_column))

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "BooleanColumn | None":
if isinstance(field, models.NullBooleanField):
return cls(null=True, **kwargs)

Expand Down
4 changes: 1 addition & 3 deletions django_tables2/columns/datecolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Self

from django.db import models

from .base import library
Expand All @@ -25,6 +23,6 @@ def __init__(self, format=None, short=True, *args, **kwargs):
super().__init__(template_code=template, *args, **kwargs)

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "DateColumn | None":
if isinstance(field, models.DateField):
return cls(**kwargs)
4 changes: 1 addition & 3 deletions django_tables2/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Self

from django.db import models

from .base import library
Expand All @@ -25,6 +23,6 @@ def __init__(self, format=None, short=True, *args, **kwargs):
super().__init__(template_code=template, *args, **kwargs)

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "DateTimeColumn | None":
if isinstance(field, models.DateTimeField):
return cls(**kwargs)
4 changes: 1 addition & 3 deletions django_tables2/columns/emailcolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Self

from django.db import models

from .base import library
Expand Down Expand Up @@ -37,6 +35,6 @@ def get_url(self, value) -> str:
return f"mailto:{value}"

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "EmailColumn | None":
if isinstance(field, models.EmailField):
return cls(**kwargs)
3 changes: 1 addition & 2 deletions django_tables2/columns/filecolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from typing import Self

from django.db import models
from django.utils.html import format_html
Expand Down Expand Up @@ -82,6 +81,6 @@ def render(self, record, value) -> SafeString:
)

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "FileColumn | None":
if isinstance(field, models.FileField):
return cls(**kwargs)
3 changes: 1 addition & 2 deletions django_tables2/columns/jsoncolumn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from typing import Self

from django.db.models import JSONField
from django.utils.html import format_html
Expand Down Expand Up @@ -48,6 +47,6 @@ def render(self, record, value) -> SafeString:
)

@classmethod
def from_field(cls, field, **kwargs) -> Self | None:
def from_field(cls, field, **kwargs) -> "JSONColumn | None":
if isinstance(field, (JSONField, HStoreField)):
return cls(**kwargs)
6 changes: 3 additions & 3 deletions django_tables2/columns/manytomanycolumn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models
from django.utils.encoding import force_str
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from django.utils.safestring import SafeString, mark_safe

from .base import Column, LinkTransform, library

Expand Down Expand Up @@ -84,7 +84,7 @@ def filter(self, qs):
"""
return qs.all()

def render(self, value):
def render(self, value) -> SafeString:
items = []
for item in self.filter(value):
content = conditional_escape(self.transform(item))
Expand All @@ -96,6 +96,6 @@ def render(self, value):
return mark_safe(conditional_escape(self.separator).join(items))

@classmethod
def from_field(cls, field, **kwargs):
def from_field(cls, field, **kwargs) -> "ManyToManyColumn | None":
if isinstance(field, models.ManyToManyField):
return cls(**kwargs)
5 changes: 3 additions & 2 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.template import Context, Template
from django.template.loader import get_template
from django.utils.html import strip_tags
from django.utils.safestring import SafeString

from .base import Column, library

Expand Down Expand Up @@ -49,7 +50,7 @@ def __init__(self, template_code=None, template_name=None, extra_context=None, *
if not self.template_code and not self.template_name:
raise ValueError("A template must be provided")

def render(self, record, table, value, bound_column, **kwargs):
def render(self, record, table, value, bound_column, **kwargs) -> SafeString:
# If the table is being rendered using `render_table`, it hackily
# attaches the context to the table as a gift to `TemplateColumn`.
context = getattr(table, "context", Context())
Expand All @@ -67,7 +68,7 @@ def render(self, record, table, value, bound_column, **kwargs):
else:
return get_template(self.template_name).render(context.flatten())

def value(self, **kwargs):
def value(self, **kwargs) -> str:
"""
The value returned from a call to `value()` on a `TemplateColumn` is
the rendered template with `django.utils.html.strip_tags` applied.
Expand Down
2 changes: 1 addition & 1 deletion django_tables2/columns/timecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def __init__(self, format=None, *args, **kwargs):
super().__init__(template_code=template, *args, **kwargs)

@classmethod
def from_field(cls, field, **kwargs):
def from_field(cls, field, **kwargs) -> "TimeColumn | None":
if isinstance(field, models.TimeField):
return cls(**kwargs)
4 changes: 2 additions & 2 deletions django_tables2/columns/urlcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class URLColumn(BaseLinkColumn):
'<a href="http://google.com">http://google.com</a>'
"""

def get_url(self, value):
def get_url(self, value: str) -> str:
return value

@classmethod
def from_field(cls, field, **kwargs):
def from_field(cls, field, **kwargs) -> "URLColumn | None":
if isinstance(field, models.URLField):
return cls(**kwargs)

0 comments on commit be61e9d

Please sign in to comment.