From 62afd58c87318aa22970e86a8f80538befcadfc4 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Mon, 17 Oct 2011 00:56:32 +0000 Subject: [PATCH] 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 --- django/contrib/flatpages/forms.py | 8 +++++--- django/contrib/flatpages/tests/forms.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py index b2811ff618..3916c85a89 100644 --- a/django/contrib/flatpages/forms.py +++ b/django/contrib/flatpages/forms.py @@ -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))) diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py index 83437ba733..7f617d52a5 100644 --- a/django/contrib/flatpages/tests/forms.py +++ b/django/contrib/flatpages/tests/forms.py @@ -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")