From c9c7b7232b2ff1df86f520b2ad03a15c1b02c950 Mon Sep 17 00:00:00 2001 From: ivan <79514623510@yandex.ru> Date: Thu, 3 Oct 2024 22:57:26 +0500 Subject: [PATCH] Added option for default settings. --- .gitignore | 3 +++ src/django_nh3/forms.py | 24 +++++++++++++----------- src/django_nh3/models.py | 23 ++++++++++++----------- src/django_nh3/utils.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..bd6ad26 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# PyCharm +.idea/ diff --git a/src/django_nh3/forms.py b/src/django_nh3/forms.py index ef114a4..0823cbc 100644 --- a/src/django_nh3/forms.py +++ b/src/django_nh3/forms.py @@ -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""" @@ -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: """ diff --git a/src/django_nh3/models.py b/src/django_nh3/models.py index 7ca0b20..3ffdb68 100644 --- a/src/django_nh3/models.py +++ b/src/django_nh3/models.py @@ -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 diff --git a/src/django_nh3/utils.py b/src/django_nh3/utils.py index b667fd0..589606d 100644 --- a/src/django_nh3/utils.py +++ b/src/django_nh3/utils.py @@ -1,4 +1,5 @@ import logging +from collections.abc import Callable from typing import Any from django.conf import settings @@ -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