Jelajahi Sumber

Move clean_html() & foreground_color() to utilities.html

Jeremy Stretch 1 tahun lalu
induk
melakukan
2a3b85a32f

+ 1 - 1
netbox/dcim/svg/cables.py

@@ -6,7 +6,7 @@ from svgwrite.text import Text
 from django.conf import settings
 from django.conf import settings
 
 
 from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH
 from dcim.constants import CABLE_TRACE_SVG_DEFAULT_WIDTH
-from utilities.utils import foreground_color
+from utilities.html import foreground_color
 
 
 
 
 __all__ = (
 __all__ = (

+ 2 - 1
netbox/dcim/svg/racks.py

@@ -14,7 +14,8 @@ from django.urls import reverse
 from django.utils.http import urlencode
 from django.utils.http import urlencode
 
 
 from netbox.config import get_config
 from netbox.config import get_config
-from utilities.utils import foreground_color, array_to_ranges
+from utilities.html import foreground_color
+from utilities.utils import array_to_ranges
 from dcim.constants import RACK_ELEVATION_BORDER_WIDTH
 from dcim.constants import RACK_ELEVATION_BORDER_WIDTH
 
 
 
 

+ 2 - 1
netbox/extras/models/models.py

@@ -22,8 +22,9 @@ from netbox.models import ChangeLoggedModel
 from netbox.models.features import (
 from netbox.models.features import (
     CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin,
     CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin,
 )
 )
+from utilities.html import clean_html
 from utilities.querysets import RestrictedQuerySet
 from utilities.querysets import RestrictedQuerySet
-from utilities.utils import clean_html, dict_to_querydict, render_jinja2
+from utilities.utils import dict_to_querydict, render_jinja2
 
 
 __all__ = (
 __all__ = (
     'Bookmark',
     'Bookmark',

+ 34 - 0
netbox/utilities/html.py

@@ -1,12 +1,46 @@
 import re
 import re
 
 
+import nh3
 from django.utils.html import escape
 from django.utils.html import escape
 
 
+from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
+
 __all__ = (
 __all__ = (
+    'clean_html',
+    'foreground_color',
     'highlight',
     'highlight',
 )
 )
 
 
 
 
+def clean_html(html, schemes):
+    """
+    Sanitizes HTML based on a whitelist of allowed tags and attributes.
+    Also takes a list of allowed URI schemes.
+    """
+    return nh3.clean(
+        html,
+        tags=HTML_ALLOWED_TAGS,
+        attributes=HTML_ALLOWED_ATTRIBUTES,
+        url_schemes=set(schemes)
+    )
+
+
+def foreground_color(bg_color, dark='000000', light='ffffff'):
+    """
+    Return the ideal foreground color (dark or light) for a given background color in hexadecimal RGB format.
+
+    :param dark: RBG color code for dark text
+    :param light: RBG color code for light text
+    """
+    THRESHOLD = 150
+    bg_color = bg_color.strip('#')
+    r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
+    if r * 0.299 + g * 0.587 + b * 0.114 > THRESHOLD:
+        return dark
+    else:
+        return light
+
+
 def highlight(value, highlight, trim_pre=None, trim_post=None, trim_placeholder='...'):
 def highlight(value, highlight, trim_pre=None, trim_post=None, trim_placeholder='...'):
     """
     """
     Highlight a string within a string and optionally trim the pre/post portions of the original string.
     Highlight a string within a string and optionally trim the pre/post portions of the original string.

+ 1 - 1
netbox/utilities/templatetags/builtins/filters.py

@@ -11,9 +11,9 @@ from markdown import markdown
 from markdown.extensions.tables import TableExtension
 from markdown.extensions.tables import TableExtension
 
 
 from netbox.config import get_config
 from netbox.config import get_config
+from utilities.html import clean_html, foreground_color
 from utilities.markdown import StrikethroughExtension
 from utilities.markdown import StrikethroughExtension
 from utilities.string import title
 from utilities.string import title
-from utilities.utils import clean_html, foreground_color
 
 
 __all__ = (
 __all__ = (
     'bettertitle',
     'bettertitle',

+ 0 - 31
netbox/utilities/utils.py

@@ -3,7 +3,6 @@ import decimal
 from itertools import count, groupby
 from itertools import count, groupby
 from urllib.parse import urlencode
 from urllib.parse import urlencode
 
 
-import nh3
 from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
 from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
 from django.db.models.functions import Coalesce
 from django.db.models.functions import Coalesce
 from django.http import QueryDict
 from django.http import QueryDict
@@ -13,7 +12,6 @@ from django.utils.timezone import localtime
 from jinja2.sandbox import SandboxedEnvironment
 from jinja2.sandbox import SandboxedEnvironment
 
 
 from netbox.config import get_config
 from netbox.config import get_config
-from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
 from .string import title
 from .string import title
 
 
 
 
@@ -47,22 +45,6 @@ def csv_format(data):
     return ','.join(csv)
     return ','.join(csv)
 
 
 
 
-def foreground_color(bg_color, dark='000000', light='ffffff'):
-    """
-    Return the ideal foreground color (dark or light) for a given background color in hexadecimal RGB format.
-
-    :param dark: RBG color code for dark text
-    :param light: RBG color code for light text
-    """
-    THRESHOLD = 150
-    bg_color = bg_color.strip('#')
-    r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
-    if r * 0.299 + g * 0.587 + b * 0.114 > THRESHOLD:
-        return dark
-    else:
-        return light
-
-
 def dynamic_import(name):
 def dynamic_import(name):
     """
     """
     Dynamically import a class from an absolute path string
     Dynamically import a class from an absolute path string
@@ -301,19 +283,6 @@ def content_type_identifier(ct):
     return f'{ct.app_label}.{ct.model}'
     return f'{ct.app_label}.{ct.model}'
 
 
 
 
-def clean_html(html, schemes):
-    """
-    Sanitizes HTML based on a whitelist of allowed tags and attributes.
-    Also takes a list of allowed URI schemes.
-    """
-    return nh3.clean(
-        html,
-        tags=HTML_ALLOWED_TAGS,
-        attributes=HTML_ALLOWED_ATTRIBUTES,
-        url_schemes=set(schemes)
-    )
-
-
 def local_now():
 def local_now():
     """
     """
     Return the current date & time in the system timezone.
     Return the current date & time in the system timezone.