Fixed #14678 -- Added validation to catch flatpages with the same URL on the same site. Thanks seler for the report, and joni, graham_king, and j4nu5 for work on the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
846fc7260a
commit
3f3010014b
|
@ -1,19 +1,7 @@
|
||||||
from django import forms
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.flatpages.models import FlatPage
|
from django.contrib.flatpages.models import FlatPage
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.contrib.flatpages.forms import FlatpageForm
|
||||||
|
|
||||||
class FlatpageForm(forms.ModelForm):
|
|
||||||
url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
|
|
||||||
help_text = _("Example: '/about/contact/'. Make sure to have leading"
|
|
||||||
" and trailing slashes."),
|
|
||||||
error_message = _("This value must contain only letters, numbers,"
|
|
||||||
" dots, underscores, dashes, slashes or tildes."))
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = FlatPage
|
|
||||||
|
|
||||||
|
|
||||||
class FlatPageAdmin(admin.ModelAdmin):
|
class FlatPageAdmin(admin.ModelAdmin):
|
||||||
form = FlatpageForm
|
form = FlatpageForm
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
from django import forms
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.contrib.flatpages.models import FlatPage
|
||||||
|
|
||||||
|
class FlatpageForm(forms.ModelForm):
|
||||||
|
url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
|
||||||
|
help_text = _("Example: '/about/contact/'. Make sure to have leading"
|
||||||
|
" and trailing slashes."),
|
||||||
|
error_message = _("This value must contain only letters, numbers,"
|
||||||
|
" dots, underscores, dashes, slashes or tildes."))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = FlatPage
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if flatpages_with_same_url.filter(sites__in=sites).exists():
|
||||||
|
for site in sites:
|
||||||
|
if flatpages_with_same_url.filter(sites=site).exists():
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_('Flatpage with url %s already exists for site %s'
|
||||||
|
% (url, site)))
|
||||||
|
|
||||||
|
return super(FlatpageForm, self).clean()
|
|
@ -1,5 +1,5 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.flatpages.admin import FlatpageForm
|
from django.contrib.flatpages.forms import FlatpageForm
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
class FlatpageAdminFormTests(TestCase):
|
class FlatpageAdminFormTests(TestCase):
|
||||||
|
@ -11,7 +11,7 @@ class FlatpageAdminFormTests(TestCase):
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_flatpage_admin_form_url_validation(self):
|
def test_flatpage_admin_form_url_validation(self):
|
||||||
"The flatpage admin form validates correctly validates urls"
|
"The flatpage admin form correctly validates urls"
|
||||||
self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
|
self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
|
||||||
self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
|
self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
|
||||||
self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid())
|
self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid())
|
||||||
|
@ -21,3 +21,17 @@ class FlatpageAdminFormTests(TestCase):
|
||||||
self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
|
self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
|
||||||
self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
|
self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
|
||||||
self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid())
|
self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid())
|
||||||
|
|
||||||
|
def test_flatpage_admin_form_url_uniqueness_validation(self):
|
||||||
|
"The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
|
||||||
|
data = dict(url='/myflatpage1', **self.form_data)
|
||||||
|
|
||||||
|
FlatpageForm(data=data).save()
|
||||||
|
|
||||||
|
f = FlatpageForm(data=data)
|
||||||
|
|
||||||
|
self.assertFalse(f.is_valid())
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
f.errors,
|
||||||
|
{'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']})
|
||||||
|
|
|
@ -148,6 +148,14 @@ Via the Python API
|
||||||
|
|
||||||
.. currentmodule:: django.contrib.flatpages
|
.. currentmodule:: django.contrib.flatpages
|
||||||
|
|
||||||
|
.. admonition:: Check for duplicate flatpage URLs.
|
||||||
|
|
||||||
|
If you add or modify flatpages via your own code, you will likely want to
|
||||||
|
check for duplicate flatpage URLs within the same site. The flatpage form
|
||||||
|
used in the admin performs this validation check, and can be imported from
|
||||||
|
:class:`django.contrib.flatpages.forms.FlatPageForm` and used in your own
|
||||||
|
views.
|
||||||
|
|
||||||
Flatpage templates
|
Flatpage templates
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue