def test_validator_with_correct_tags(self): tag = Tag(title='a test') tag.save() validator = TagValidator() self.assertEqual(validator.validate_raw_string(None), True) self.assertEqual(validator.validate_raw_string(tag.title), True) self.assertEqual(validator.errors, [])
def test_validator_with_correct_tags(self): tag = Tag(title='a test') tag.save() validator = TagValidator() self.assertEqual(validator.validate_raw_string(None), True) self.assertEqual(validator.validate_raw_string(tag.title), True) self.assertEqual(validator.errors, [])
def clean(self): cleaned_data = super(TopicForm, self).clean() title = cleaned_data.get('title') text = cleaned_data.get('text') tags = cleaned_data.get('tags') if title is not None: if not title.strip(): self._errors['title'] = self.error_class( [_(u'Le champ titre ne peut être vide')]) if 'title' in cleaned_data: del cleaned_data['title'] if text is not None and not text.strip(): self._errors['text'] = self.error_class( [_(u'Le champ text ne peut être vide')]) if 'text' in cleaned_data: del cleaned_data['text'] if text is not None and len(text) > settings.ZDS_APP['forum']['max_post_length']: self._errors['text'] = self.error_class( [_(u'Ce message est trop long, il ne doit pas dépasser {0} ' u'caractères').format(settings.ZDS_APP['forum']['max_post_length'])]) validator = TagValidator() if not validator.validate_raw_string(tags): self._errors['tags'] = self.error_class(validator.errors) return cleaned_data
def clean(self): cleaned_data = super(ContentForm, self).clean() image = cleaned_data.get('image') if image is not None and image.size > settings.ZDS_APP['gallery']['image_max_size']: self._errors['image'] = self.error_class( [_(u'Votre logo est trop lourd, la limite autorisée est de {} Ko') .format(settings.ZDS_APP['gallery']['image_max_size'] / 1024)]) validator = TagValidator() if not validator.validate_raw_string(cleaned_data.get('tags')): self._errors['tags'] = self.error_class(validator.errors) return cleaned_data
def clean(self): cleaned_data = super(TopicForm, self).clean() self.get_non_empty_field_or_error(cleaned_data, 'title', lambda: _('Le champ titre ne peut être vide')) text = self.get_non_empty_field_or_error(cleaned_data, 'text', lambda: _('Le champ text ne peut être vide')) if text: self.check_text_length_limit(text, settings.ZDS_APP['forum']['max_post_length'], lambda: _('Ce message est trop long, ' 'il ne doit pas dépasser {0} caractères')) tags = cleaned_data.get('tags') validator = TagValidator() if not validator.validate_raw_string(tags): self._errors['tags'] = self.error_class(validator.errors) return cleaned_data
def clean(self): cleaned_data = super().clean() self.get_non_empty_field_or_error( cleaned_data, "title", lambda: _("Le champ titre ne peut être vide")) text = self.get_non_empty_field_or_error( cleaned_data, "text", lambda: _("Le champ text ne peut être vide")) if text: self.check_text_length_limit( text, settings.ZDS_APP["forum"]["max_post_length"], lambda: _("Ce message est trop long, " "il ne doit pas dépasser {0} caractères"), ) tags = cleaned_data.get("tags") validator = TagValidator() if not validator.validate_raw_string(tags): self._errors["tags"] = self.error_class(validator.errors) return cleaned_data
class TopicForm(forms.Form): title = forms.CharField( label=_('Titre'), max_length=Topic._meta.get_field('title').max_length, widget=forms.TextInput(attrs={ 'placeholder': _(u'Titre de mon sujet'), 'required': 'required', })) subtitle = forms.CharField( label=_('Sous-titre'), max_length=Topic._meta.get_field('subtitle').max_length, required=False, ) forum = forms.ModelChoiceField(label=_(u'Catégorie'), queryset=None, widget=forms.Select(attrs={ 'required': 'required', })) tags = forms.CharField( label=_( u'Tag(s) séparés par une virgule (exemple: python,django,web)'), max_length=64, required=False, widget=forms.TextInput( attrs={ 'data-autocomplete': '{ "type": "multiple", "fieldname": "title", "url": "/api/tags/?search=%s" }' })) text = forms.CharField( label='Texte', widget=forms.Textarea( attrs={ 'placeholder': _('Votre message au format Markdown.'), 'required': 'required', })) def __init__(self, *args, **kwargs): self.user = kwargs.get('user') super(TopicForm, self).__init__(*args, **kwargs) self.fields['forum'].queryset = Forum.objects.exclude( group__in=Group.objects.filter(user=self.user).all()) self.helper = FormHelper() self.helper.form_class = 'content-wrapper' self.helper.form_method = 'post' self.helper.layout = Layout( Field('title', autocomplete='off'), Field('subtitle', autocomplete='off'), Field('tags'), Field('forum'), CommonLayoutEditor(), ) def clean(self): cleaned_data = super(TopicForm, self).clean() title = cleaned_data.get('title') text = cleaned_data.get('text') if title is not None: if title.strip() == '': self._errors['title'] = self.error_class( [_(u'Le champ titre ne peut être vide')]) if 'title' in cleaned_data: del cleaned_data['title'] elif re.sub(ur"(?P<start>)(\[.*?\])(?P<end>)", sub_tag, title) \ .strip() == '': self._errors['title'] = self.error_class( [_(u'Le titre ne peux pas contenir uniquement des tags')]) else: tags = re.findall(ur"((.*?)\[(.*?)\](.*?))", title) for tag in tags: if tag[2].strip() == "": if 'title' in cleaned_data: self._errors['title'] = self.error_class( [_(u'Un tag ne peut être vide')]) elif len(tag[2]) > Tag._meta.get_field('title').max_length: if 'title' in cleaned_data: self._errors['title'] = self.error_class([ _(u'Un tag doit faire moins de {0} caractères' ).format( Tag._meta.get_field('title').max_length) ]) if text is not None and text.strip() == '': self._errors['text'] = self.error_class( [_(u'Le champ text ne peut être vide')]) if 'text' in cleaned_data: del cleaned_data['text'] if text is not None and len( text) > settings.ZDS_APP['forum']['max_post_length']: self._errors['text'] = self.error_class([ _(u'Ce message est trop long, il ne doit pas dépasser {0} ' u'caractères').format( settings.ZDS_APP['forum']['max_post_length']) ]) if not TagValidator.validate_raw_string(cleaned_data.get("tags")): self._errors['tags'] = self.error_class( [_(u'Vous avez entré un tag trop long.')]) return cleaned_data
def clean_tags(self): validator = TagValidator() cleaned_tags = self.cleaned_data.get("tags") if not validator.validate_raw_string(cleaned_tags): self.add_error("tags", self.error_class(validator.errors)) return cleaned_tags
def test_validator_with_utf8mb4(self): raw_string = '🐙☢,bla' validator = TagValidator() self.assertFalse(validator.validate_raw_string(raw_string)) self.assertEqual(1, len(validator.errors))
def test_validator_with_special_char_only(self): validator = TagValidator() self.assertFalse(validator.validate_raw_string('^')) self.assertEqual(len(validator.errors), 1)
def test_validator_with_utf8mb4(self): raw_string = '🐙☢,bla' validator = TagValidator() self.assertFalse(validator.validate_raw_string(raw_string)) self.assertEqual(1, len(validator.errors))
def test_validator_with_special_char_only(self): validator = TagValidator() self.assertFalse(validator.validate_raw_string("^")) self.assertEqual(len(validator.errors), 1)