Fixed #17057 -- Corrected flatpage url uniqueness validation to account for flatpage editing.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17000 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer 2011-10-17 00:56:32 +00:00
parent d48510cc6b
commit 62afd58c87
2 changed files with 26 additions and 3 deletions

View File

@ -16,11 +16,13 @@ class FlatpageForm(forms.ModelForm):
url = self.cleaned_data.get('url', None)
sites = self.cleaned_data.get('sites', None)
flatpages_with_same_url = FlatPage.objects.filter(url=url)
same_url = FlatPage.objects.filter(url=url)
if self.instance.pk:
same_url = same_url.exclude(pk=self.instance.pk)
if flatpages_with_same_url.filter(sites__in=sites).exists():
if same_url.filter(sites__in=sites).exists():
for site in sites:
if flatpages_with_same_url.filter(sites=site).exists():
if same_url.filter(sites=site).exists():
raise forms.ValidationError(
_('Flatpage with url %s already exists for site %s'
% (url, site)))

View File

@ -1,5 +1,6 @@
from django.conf import settings
from django.contrib.flatpages.forms import FlatpageForm
from django.contrib.flatpages.models import FlatPage
from django.test import TestCase
class FlatpageAdminFormTests(TestCase):
@ -35,3 +36,23 @@ class FlatpageAdminFormTests(TestCase):
self.assertEqual(
f.errors,
{'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']})
def test_flatpage_admin_form_edit(self):
"""
Existing flatpages can be edited in the admin form without triggering
the url-uniqueness validation.
"""
existing = FlatPage.objects.create(
url="/myflatpage1", title="Some page", content="The content")
existing.sites.add(settings.SITE_ID)
data = dict(url='/myflatpage1', **self.form_data)
f = FlatpageForm(data=data, instance=existing)
self.assertTrue(f.is_valid(), f.errors)
updated = f.save()
self.assertEqual(updated.title, "A test page")