diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py
index 3916c85..1341d92 100644
a
|
b
|
|
| 1 | import re |
| 2 | |
1 | 3 | from django import forms |
2 | | from django.utils.translation import ugettext_lazy as _ |
| 4 | from django.conf import settings |
3 | 5 | from django.contrib.flatpages.models import FlatPage |
| 6 | from django.utils.translation import ugettext_lazy as _ |
| 7 | |
| 8 | URL_REGEX = r'^/[-\w/\.~]+/$' |
4 | 9 | |
5 | 10 | class FlatpageForm(forms.ModelForm): |
6 | | url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$', |
| 11 | url = forms.RegexField(label=_("URL"), max_length=100, regex=URL_REGEX, |
7 | 12 | help_text = _("Example: '/about/contact/'. Make sure to have leading" |
8 | 13 | " and trailing slashes."), |
9 | 14 | error_message = _("This value must contain only letters, numbers," |
… |
… |
class FlatpageForm(forms.ModelForm):
|
12 | 17 | class Meta: |
13 | 18 | model = FlatPage |
14 | 19 | |
| 20 | def __init__(self, *args, **kwargs): |
| 21 | super(FlatpageForm, self).__init__(*args, **kwargs) |
| 22 | if not settings.APPEND_SLASH or \ |
| 23 | not 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES: |
| 24 | # Make trailing slash optional |
| 25 | self.fields['url'].regex = re.compile(r'%s$' % URL_REGEX[:-2]) |
| 26 | |
15 | 27 | def clean(self): |
16 | 28 | url = self.cleaned_data.get('url', None) |
17 | 29 | sites = self.cleaned_data.get('sites', None) |
diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py
index 7f617d5..55709bd 100644
a
|
b
|
from django.conf import settings
|
2 | 2 | from django.contrib.flatpages.forms import FlatpageForm |
3 | 3 | from django.contrib.flatpages.models import FlatPage |
4 | 4 | from django.test import TestCase |
| 5 | from django.test.utils import override_settings |
5 | 6 | |
6 | 7 | class FlatpageAdminFormTests(TestCase): |
7 | 8 | def setUp(self): |
… |
… |
class FlatpageAdminFormTests(TestCase):
|
22 | 23 | self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid()) |
23 | 24 | self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid()) |
24 | 25 | self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid()) |
| 26 | self.assertFalse(FlatpageForm(data=dict(url='no_leading_slash/', **self.form_data)).is_valid()) |
| 27 | if settings.APPEND_SLASH and \ |
| 28 | 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES: |
| 29 | form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data)) |
| 30 | self.assertFalse(form.is_valid()) |
25 | 31 | |
26 | | def test_flatpage_admin_form_url_uniqueness_validation(self): |
| 32 | @override_settings(APPEND_SLASH=False) |
| 33 | def _test_flatpage_admin_form_url_uniqueness_validation(self): |
27 | 34 | "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site" |
28 | 35 | data = dict(url='/myflatpage1', **self.form_data) |
29 | 36 | |
… |
… |
class FlatpageAdminFormTests(TestCase):
|
37 | 44 | f.errors, |
38 | 45 | {'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']}) |
39 | 46 | |
| 47 | @override_settings(APPEND_SLASH=False) |
40 | 48 | def test_flatpage_admin_form_edit(self): |
41 | 49 | """ |
42 | 50 | Existing flatpages can be edited in the admin form without triggering |