Commit 9555d949 authored by Chris Routh's avatar Chris Routh 🐰

Merge branch 'dev-1.1.0' into 'master'

Release 1.1.0

See merge request open-source/python/django_tidyfields!7
parents 12ec01ad ebf42426
Pipeline #2534 passed with stage
in 12 minutes and 52 seconds
......@@ -8,7 +8,7 @@
Welcome to django_tidyfields's documentation!
=================================================
:Version: 1.0.1
:Version: 1.1.0
:Source: https://gitlab.routh.io/open-source/python/django_tidyfields
:Keywords: ``django`` ``lxml`` ``input`` ``html`` ``fields``
:PythonVersion: 3.6+
......
......@@ -5,7 +5,7 @@ import re
from lxml.html.clean import Cleaner
from lxml.html import fromstring, tostring
__version__ = '1.0.1'
__version__ = '1.1.0'
DEFAULT_APP_CONFIG = 'django_tidyfields.apps.DjangoTidyfieldsConfig'
......@@ -36,6 +36,7 @@ TIDYFIELDS_DEFAULT = {
'whitelist_tags': {}
}
STRIP_TIDYWRAP_REGEX = re.compile(r'^<tidywrap>(.*)</tidywrap>$', flags=re.DOTALL)
def tidy_input(field_contents="", **kwargs):
"""
......@@ -54,6 +55,6 @@ def tidy_input(field_contents="", **kwargs):
)
# Remove outer element added to prevent lxml adding unknown root element.
tidied = re.sub(r'^<tidywrap>(.*)</tidywrap>$', r'\1', tidied, flags=re.DOTALL)
tidied = STRIP_TIDYWRAP_REGEX.sub(r'\1', tidied)
return tidied
......@@ -27,17 +27,27 @@ class TidyCharField(CharField):
except AttributeError:
self.kwargs = {**TIDYFIELDS_DEFAULT, **(field_args or {})}
def pre_save(self, model_instance, add):
def clean(self, value, model_instance):
"""
Clean text, update model and return cleaned text.
:param model_instance: (obj) model instance
:param add: default textfield parameter, unused
:param value: raw input value
:return: clean text as unicode
"""
tidied = tidy_input(getattr(model_instance, self.attname), **self.kwargs)
tidied = tidy_input(value, **self.kwargs)
setattr(model_instance, self.attname, tidied)
return tidied
return super(TidyCharField, self).clean(tidied, model_instance)
def pre_save(self, model_instance, add):
"""
Call clean on pre_save to enforce html stripping before DB save.
:param model_instance: (obj) model instance, unused
:param add: default textfield parameter, unused
:return: clean text as unicode
"""
return self.clean(getattr(model_instance, self.attname), model_instance)
class TidyTextField(TextField):
......@@ -60,15 +70,25 @@ class TidyTextField(TextField):
except AttributeError:
self.kwargs = {**TIDYFIELDS_DEFAULT, **(field_args or {})}
def pre_save(self, model_instance, add):
def clean(self, value, model_instance):
"""
Clean text, update model and return cleaned text.
:param model_instance: (obj) model instance
:param add: default textfield parameter, unused
:param value: raw input value
:return: clean text as unicode
"""
tidied = tidy_input(getattr(model_instance, self.attname), **self.kwargs)
tidied = tidy_input(value, **self.kwargs)
setattr(model_instance, self.attname, tidied)
return tidied
return super(TidyTextField, self).clean(tidied, model_instance)
def pre_save(self, model_instance, add):
"""
Call clean on pre_save to enforce html stripping before DB save.
:param model_instance: (obj) model instance, unused
:param add: default textfield parameter, unused
:return: clean text as unicode
"""
return self.clean(getattr(model_instance, self.attname), model_instance)
......@@ -2,7 +2,7 @@
# see http://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files
[metadata]
name = django_tidyfields
version = 1.0.1
version = 1.1.0
description = Easily accept html input with Django models, templates and DRF serializers
long_description = file: README.rst
url = https://gitlab.routh.io/open-source/python/django_tidyfields
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment