def clean_custom_css(self, *args, **kwargs): if self.cleaned_data.get('custom_css') or self.files.get('custom_css'): css = self.cleaned_data['custom_css'] or self.files['custom_css'] try: validate_css(css.read()) return css except IsADirectoryError: self.instance.custom_css = None self.instance.save(update_fields=['custom_css']) else: self.instance.custom_css = None self.instance.save(update_fields=['custom_css'])
def clean_custom_css(self): if self.cleaned_data.get("custom_css") or self.files.get("custom_css"): css = self.cleaned_data["custom_css"] or self.files["custom_css"] if self.is_administrator: return css try: validate_css(css.read()) return css except IsADirectoryError: self.instance.custom_css = None self.instance.save(update_fields=["custom_css"]) else: self.instance.custom_css = None self.instance.save(update_fields=["custom_css"]) return None
def clean_custom_css_text(self): css = self.cleaned_data.get("custom_css_text").strip() if not css or self.is_administrator: return css validate_css(css) return css
def test_invalid_css(invalid_css): with pytest.raises(ValidationError): validate_css(invalid_css)
def test_valid_css(valid_css): assert validate_css(valid_css) == valid_css