Esempio n. 1
0
 def test_pre_save_sanitize(self):
     txt = self.random_string(add_chars="<>")
     sanitizer = get_sanitizer()
     TestModel = self._test_model()
     m = TestModel()
     meta = TestModel._meta
     html = meta.local_concrete_fields[1]
     m.html = txt
     html.pre_save(m, False)
     self.assertEqual(m.html, sanitizer.sanitize(txt))
    def __init__(self, sanitizer_type="default", **kwargs):
        """Builds the class's sanitizer.

        Args:
            sanitizer_type (str, optional): This is the sanitizer from
                django's settings that will be used.  If no sanitizer_type
                is provided, the default sanitizer will be used.

        """
        self.sanitizer_type = sanitizer_type
        self.sanitizer = get_sanitizer(name=sanitizer_type)
        self.default_validators = [self.validate_html]
        super().__init__(**kwargs)
Esempio n. 3
0
 def save(self, *args, **kwargs):
     self.text = get_sanitizer("tinyforum-post").sanitize(self.text)
     super().save(*args, **kwargs)
     self.thread.save()
Esempio n. 4
0
def cleanse_html(html):
    """
    Pass ugly HTML, get nice HTML back.
    """
    return get_sanitizer().sanitize(html)
Esempio n. 5
0
 def pre_save(self, model_instance, add):
     sanitizer = get_sanitizer()
     value = super().pre_save(model_instance, add)
     value = sanitizer.sanitize(value)
     setattr(model_instance, self.attname, value)
     return value