Add ValidationError for tags with more than 100 characters

- Co-Authored-By: Khanh Hoang <39248704+thoang43@users.noreply.github.com>
- Add max_tag_length field to avoid hard coding 100
- fixes #4564
pull/8256/merge
vuph 2022-03-26 03:50:03 -04:00 zatwierdzone przez LB (Ben Johnston)
rodzic 9007bda686
commit 5ac259f682
4 zmienionych plików z 33 dodań i 0 usunięć

Wyświetl plik

@ -53,6 +53,7 @@ Changelog
* Fix: Allow bulk publishing of pages without revisions (Andy Chosak)
* Fix: Stop skipping heading levels in Wagtail welcome page (Jesse Menn)
* Fix: Add missing `lang` attributes to `<html>` elements (James Ray)
* Fix: Avoid 503 server error when entering tags over 100chars and instead show a user facing validation error (Vu Pham, Khanh Hoang)
2.16.2 (xx.xx.xxxx) - IN DEVELOPMENT

Wyświetl plik

@ -88,6 +88,7 @@ class LandingPage(Page):
* Stop skipping heading levels in Wagtail welcome page (Jesse Menn)
* Add missing `lang` attributes to `<html>` elements (James Ray)
* Add missing translation usage in Workflow templates (Anuja Verma, Saurabh Kumar)
* Avoid 503 server error when entering tags over 100chars and instead show a user facing validation error (Vu Pham, Khanh Hoang)
## Upgrade considerations

Wyświetl plik

@ -1,3 +1,5 @@
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from taggit.forms import TagField as TaggitTagField
from taggit.models import Tag
@ -31,8 +33,27 @@ class TagField(TaggitTagField):
self.widget.free_tagging = self.free_tagging
def clean(self, value):
value = super().clean(value)
max_tag_length = self.tag_model.name.field.max_length
value_too_long = ""
for val in value:
if len(val) > max_tag_length:
if value_too_long:
value_too_long += ", "
value_too_long += val
if value_too_long:
raise ValidationError(
_(
"Tag(s) %(value_too_long)s are over %(max_tag_length)d characters"
% {
"value_too_long": value_too_long,
"max_tag_length": max_tag_length,
}
)
)
if not self.free_tagging:
# filter value to just the tags that already exist in tag_model
value = list(

Wyświetl plik

@ -485,6 +485,16 @@ class TestTagField(TestCase):
self.assertTrue(form.is_valid())
self.assertEqual(set(form.cleaned_data["tags"]), {"Italian", "delicious"})
def test_tag_over_one_hundred_characters(self):
class RestaurantTagForm(forms.Form):
tags = TagField(tag_model=RestaurantTag)
tag_name = ""
for _ in range(101):
tag_name += "a"
form = RestaurantTagForm({"tags": tag_name})
self.assertFalse(form.is_valid())
class TestFilteredSelect(TestCase):
def test_render(self):