mirror of https://github.com/django/django.git
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:
parent
d48510cc6b
commit
62afd58c87
|
@ -16,11 +16,13 @@ class FlatpageForm(forms.ModelForm):
|
||||||
url = self.cleaned_data.get('url', None)
|
url = self.cleaned_data.get('url', None)
|
||||||
sites = self.cleaned_data.get('sites', 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:
|
for site in sites:
|
||||||
if flatpages_with_same_url.filter(sites=site).exists():
|
if same_url.filter(sites=site).exists():
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
_('Flatpage with url %s already exists for site %s'
|
_('Flatpage with url %s already exists for site %s'
|
||||||
% (url, site)))
|
% (url, site)))
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.flatpages.forms import FlatpageForm
|
from django.contrib.flatpages.forms import FlatpageForm
|
||||||
|
from django.contrib.flatpages.models import FlatPage
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
class FlatpageAdminFormTests(TestCase):
|
class FlatpageAdminFormTests(TestCase):
|
||||||
|
@ -35,3 +36,23 @@ class FlatpageAdminFormTests(TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
f.errors,
|
f.errors,
|
||||||
{'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']})
|
{'__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")
|
||||||
|
|
Loading…
Reference in New Issue