Skip to content

Commit

Permalink
Added option for default settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
KozyrevIvan committed Oct 3, 2024
1 parent a99a8a6 commit c9c7b72
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# PyCharm
.idea/
24 changes: 13 additions & 11 deletions src/django_nh3/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django import forms
from django.utils.safestring import mark_safe

from src.django_nh3.utils import get_nh3_update_options


class Nh3Field(forms.CharField):
"""nh3 form field"""
Expand All @@ -15,27 +17,27 @@ class Nh3Field(forms.CharField):

def __init__(
self,
attributes: dict[str, set[str]] = {},
attributes: dict[str, set[str]] | None = None,
attribute_filter: Callable[[str, str, str], str] | None = None,
clean_content_tags: set[str] = set(),
clean_content_tags: set[str] | None = None,
empty_value: Any | None = None,
link_rel: str = "",
strip_comments: bool = False,
tags: set[str] = set(),
tags: set[str] | None = None,
*args: Any,
**kwargs: dict[Any, Any],
):
super().__init__(*args, **kwargs)

self.empty_value = empty_value
self.nh3_options = {
"attributes": attributes,
"attribute_filter": attribute_filter,
"clean_content_tags": clean_content_tags,
"link_rel": link_rel,
"strip_comments": strip_comments,
"tags": tags,
}
self.nh3_options = get_nh3_update_options(
attributes=attributes,
attribute_filter=attribute_filter,
clean_content_tags=clean_content_tags,
link_rel=link_rel,
strip_comments=strip_comments,
tags=tags,
)

def to_python(self, value: Any) -> Any:
"""
Expand Down
23 changes: 12 additions & 11 deletions src/django_nh3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@
from django.utils.safestring import mark_safe

from . import forms
from .utils import get_nh3_update_options


class Nh3Field(models.TextField):
def __init__(
self,
attributes: dict[str, set[str]] = {},
attributes: dict[str, set[str]] | None = None,
attribute_filter: Callable[[str, str, str], str] | None = None,
clean_content_tags: set[str] = set(),
clean_content_tags: set[str] | None = None,
link_rel: str = "",
strip_comments: bool = False,
tags: set[str] = set(),
tags: set[str] | None = None,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)

self.nh3_options = {
"attributes": attributes,
"attribute_filter": attribute_filter,
"clean_content_tags": clean_content_tags,
"link_rel": link_rel,
"strip_comments": strip_comments,
"tags": tags,
}
self.nh3_options = get_nh3_update_options(
attributes=attributes,
attribute_filter=attribute_filter,
clean_content_tags=clean_content_tags,
link_rel=link_rel,
strip_comments=strip_comments,
tags=tags,
)

def formfield(
self, form_class: FormField = forms.Nh3Field, **kwargs: Any
Expand Down
32 changes: 32 additions & 0 deletions src/django_nh3/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from collections.abc import Callable
from typing import Any

from django.conf import settings
Expand Down Expand Up @@ -48,3 +49,34 @@ def get_nh3_default_options() -> dict[str, Any]:
nh3_args[kwarg] = attr

return nh3_args


def get_nh3_update_options(
attributes: dict[str, set[str]] | None = None,
attribute_filter: Callable[[str, str, str], str] | None = None,
clean_content_tags: set[str] | None = None,
link_rel: str = "",
strip_comments: bool = False,
tags: set[str] | None = None,
) -> dict[str, Any]:
_attributes = attributes or getattr(settings, "NH3_ALLOWED_ATTRIBUTES", {})
_attribute_filter = attribute_filter or getattr(
settings, "NH3_ALLOWED_ATTRIBUTES_FILTER", None
)
_clean_content_tags = (
clean_content_tags or getattr(settings, "NH3_CLEAN_CONTENT_TAGS", None) or set()
)
_link_rel = link_rel or getattr(settings, "NH3_CLEAN_CONTENT_TAGS", "")
_strip_comments = strip_comments or getattr(settings, "NH3_STRIP_COMMENTS", False)
_tags = tags or getattr(settings, "NH3_ALLOWED_TAGS", None) or set()

nh3_args = {
"attributes": {tag: set(attributes) for tag, attributes in _attributes.items()},
"attribute_filter": _attribute_filter,
"clean_content_tags": set(_clean_content_tags),
"link_rel": _link_rel,
"strip_comments": _strip_comments,
"tags": set(_tags),
}

return nh3_args

0 comments on commit c9c7b72

Please sign in to comment.