Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17057 -- Corrected flatpage url uniqueness validation to accou…

…nt for flatpage editing.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17000 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 62afd58c87318aa22970e86a8f80538befcadfc4 1 parent d48510c
@carljm carljm authored
View
8 django/contrib/flatpages/forms.py
@@ -16,11 +16,13 @@ def clean(self):
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
21 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 @@ def test_flatpage_admin_form_url_uniqueness_validation(self):
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")
Please sign in to comment.
Something went wrong with that request. Please try again.