mirror of
https://github.com/inventree/InvenTree.git
synced 2026-09-02 10:18:42 +00:00
Refactor clean_string
This commit is contained in:
@@ -1,18 +1,12 @@
|
|||||||
"""Mixins for (API) views in the whole project."""
|
"""Mixins for (API) views in the whole project."""
|
||||||
|
|
||||||
from django.core.exceptions import FieldDoesNotExist
|
|
||||||
|
|
||||||
from rest_framework import generics, mixins, status
|
from rest_framework import generics, mixins, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
import data_exporter.mixins
|
import data_exporter.mixins
|
||||||
import importer.mixins
|
import importer.mixins
|
||||||
from InvenTree.fields import InvenTreeNotesField, OutputConfiguration
|
from InvenTree.fields import OutputConfiguration
|
||||||
from InvenTree.helpers import (
|
from InvenTree.helpers import remove_non_printable_characters, strip_html_tags
|
||||||
clean_markdown,
|
|
||||||
remove_non_printable_characters,
|
|
||||||
strip_html_tags,
|
|
||||||
)
|
|
||||||
from InvenTree.schema import schema_for_view_output_options
|
from InvenTree.schema import schema_for_view_output_options
|
||||||
from InvenTree.serializers import FilterableSerializerMixin
|
from InvenTree.serializers import FilterableSerializerMixin
|
||||||
|
|
||||||
@@ -54,38 +48,10 @@ class CleanMixin:
|
|||||||
"""Clean / sanitize a single input string."""
|
"""Clean / sanitize a single input string."""
|
||||||
cleaned = data
|
cleaned = data
|
||||||
|
|
||||||
# By default, newline characters are removed
|
cleaned = remove_non_printable_characters(cleaned, remove_newline=True)
|
||||||
remove_newline = True
|
|
||||||
is_markdown = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
if hasattr(self, 'serializer_class'):
|
|
||||||
model = self.serializer_class.Meta.model
|
|
||||||
field_base = model._meta.get_field(field)
|
|
||||||
|
|
||||||
# The following field types allow newline characters
|
|
||||||
allow_newline = [(InvenTreeNotesField, True)]
|
|
||||||
|
|
||||||
for field_type in allow_newline:
|
|
||||||
if issubclass(type(field_base), field_type[0]):
|
|
||||||
remove_newline = False
|
|
||||||
is_markdown = field_type[1]
|
|
||||||
break
|
|
||||||
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
except FieldDoesNotExist:
|
|
||||||
pass
|
|
||||||
|
|
||||||
cleaned = remove_non_printable_characters(
|
|
||||||
cleaned, remove_newline=remove_newline
|
|
||||||
)
|
|
||||||
|
|
||||||
cleaned = strip_html_tags(cleaned, field_name=field)
|
cleaned = strip_html_tags(cleaned, field_name=field)
|
||||||
|
|
||||||
if is_markdown:
|
|
||||||
cleaned = clean_markdown(cleaned)
|
|
||||||
|
|
||||||
return cleaned
|
return cleaned
|
||||||
|
|
||||||
def clean_data(self, data: dict) -> dict:
|
def clean_data(self, data: dict) -> dict:
|
||||||
|
|||||||
Reference in New Issue
Block a user