diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py
index 3916c85..347b5f8 100644
a
|
b
|
|
1 | 1 | from django import forms |
2 | | from django.utils.translation import ugettext_lazy as _ |
| 2 | from django.conf import settings |
3 | 3 | from django.contrib.flatpages.models import FlatPage |
| 4 | from django.utils.translation import ugettext, ugettext_lazy as _ |
| 5 | |
4 | 6 | |
5 | 7 | class FlatpageForm(forms.ModelForm): |
6 | 8 | url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$', |
… |
… |
class FlatpageForm(forms.ModelForm):
|
12 | 14 | class Meta: |
13 | 15 | model = FlatPage |
14 | 16 | |
| 17 | def clean_url(self): |
| 18 | url = self.cleaned_data['url'] |
| 19 | if not url.startswith("/"): |
| 20 | raise forms.ValidationError(ugettext("Missing leading slash")) |
| 21 | if settings.APPEND_SLASH and \ |
| 22 | 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES and \ |
| 23 | not url.endswith("/"): |
| 24 | raise forms.ValidationError(ugettext("Missing trailing slash")) |
| 25 | return url |
| 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..2112f50 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 | form = FlatpageForm(data=dict(url='no_leading_slash/', **self.form_data)) |
| 27 | self.assertFalse(form.is_valid()) |
| 28 | self.assertEqual(form.errors['url'], ["Missing leading slash"]) |
| 29 | if settings.APPEND_SLASH and \ |
| 30 | 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES: |
| 31 | form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data)) |
| 32 | self.assertFalse(form.is_valid()) |
| 33 | self.assertEqual(form.errors['url'], ["Missing trailing slash"]) |
25 | 34 | |
| 35 | @override_settings(APPEND_SLASH=False) |
26 | 36 | def test_flatpage_admin_form_url_uniqueness_validation(self): |
27 | 37 | "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site" |
28 | 38 | data = dict(url='/myflatpage1', **self.form_data) |
… |
… |
class FlatpageAdminFormTests(TestCase):
|
37 | 47 | f.errors, |
38 | 48 | {'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']}) |
39 | 49 | |
| 50 | @override_settings(APPEND_SLASH=False) |
40 | 51 | def test_flatpage_admin_form_edit(self): |
41 | 52 | """ |
42 | 53 | Existing flatpages can be edited in the admin form without triggering |