diff --git a/django/contrib/flatpages/admin.py b/django/contrib/flatpages/admin.py
index 1b377e9..74118fd 100644
a
|
b
|
class FlatpageForm(forms.ModelForm):
|
11 | 11 | error_message = _("This value must contain only letters, numbers," |
12 | 12 | " dots, underscores, dashes, slashes or tildes.")) |
13 | 13 | |
| 14 | def clean(self): |
| 15 | url = self.cleaned_data.get('url') |
| 16 | sites = self.cleaned_data.get('sites') |
| 17 | if url and sites: |
| 18 | other_flatpages_with_same_url = FlatPage.objects.exclude(pk=self.instance.pk).filter(url=url) |
| 19 | |
| 20 | for other_flatpage in other_flatpages_with_same_url: |
| 21 | for site in sites: |
| 22 | if site in other_flatpage.sites.all(): |
| 23 | raise forms.ValidationError(_("URL '%s' is already assigned to another FlatPage '%s' that also belongs to the site '%s'. Two FlatPages can't have the same URL if they belog to the same site." % (url, other_flatpage.title, site))) |
| 24 | |
| 25 | return super(FlatpageForm, self).clean() |
| 26 | |
14 | 27 | class Meta: |
15 | 28 | model = FlatPage |
16 | 29 | |
diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py
index 969d347..f40ac0d 100644
a
|
b
|
|
1 | 1 | from django.contrib.flatpages.admin import FlatpageForm |
| 2 | from django.contrib.flatpages.models import FlatPage |
| 3 | from django.contrib.sites.models import Site |
2 | 4 | from django.test import TestCase |
3 | 5 | |
4 | 6 | class FlatpageAdminFormTests(TestCase): |
… |
… |
class FlatpageAdminFormTests(TestCase):
|
10 | 12 | } |
11 | 13 | |
12 | 14 | def test_flatpage_admin_form_url_validation(self): |
13 | | "The flatpage admin form validates correctly validates urls" |
| 15 | "The flatpage admin form correctly validates urls" |
14 | 16 | self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid()) |
15 | 17 | self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid()) |
16 | 18 | self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid()) |
… |
… |
class FlatpageAdminFormTests(TestCase):
|
20 | 22 | self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid()) |
21 | 23 | self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid()) |
22 | 24 | self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid()) |
| 25 | |
| 26 | def test_url_must_be_unique_for_all_pages_in_the_same_site(self): |
| 27 | "The flatpage admin form correctly enforces url uniqueness among all flatpages belonging to the same site" |
| 28 | |
| 29 | site1 = Site.objects.create(domain='site1.com', name='site1') |
| 30 | site1.save() |
| 31 | |
| 32 | site2 = Site.objects.create(domain='site2.com', name='site2') |
| 33 | site2.save() |
| 34 | |
| 35 | my_page = FlatPage.objects.create( |
| 36 | url="/my-page/", |
| 37 | title="This is my page", |
| 38 | content="Isn't it special!", |
| 39 | enable_comments=False, |
| 40 | registration_required=False, |
| 41 | ) |
| 42 | my_page.sites.add(site1) # add my_page to site1 |
| 43 | my_page.save() |
| 44 | |
| 45 | new_page_data = { |
| 46 | 'title': "A test page", |
| 47 | 'content': "This is a test", |
| 48 | } |
| 49 | |
| 50 | # New page has the same url as my_page and is in the same site -> error |
| 51 | self.assertFalse(FlatpageForm(data=dict(url='/my-page/', sites=[site1.pk], **new_page_data)).is_valid()) |
| 52 | |
| 53 | # New page has the same url as my_page but is in another site -> ok |
| 54 | self.assertTrue(FlatpageForm(data=dict(url='/my-page/', sites=[site2.pk], **new_page_data)).is_valid()) |
| 55 | No newline at end of file |
diff --git a/docs/ref/contrib/flatpages.txt b/docs/ref/contrib/flatpages.txt
index ce6fdfc..ecb0c76 100644
a
|
b
|
If you've activated the automatic Django admin interface, you should see a
|
116 | 116 | "Flatpages" section on the admin index page. Edit flatpages as you edit any |
117 | 117 | other object in the system. |
118 | 118 | |
| 119 | Take into consideration that the admin interface won't allow you to create two flatpages with the same url if they both belong to the same Site. |
| 120 | |
119 | 121 | Via the Python API |
120 | 122 | ------------------ |
121 | 123 | |