Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 3f3010014b33451553c62b005c2ec09a16a9f9ce 1 parent 846fc72
@carljm carljm authored
View
14 django/contrib/flatpages/admin.py
@@ -1,19 +1,7 @@
-from django import forms
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
from django.utils.translation import ugettext_lazy as _
-
-
-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
-
+from django.contrib.flatpages.forms import FlatpageForm
class FlatPageAdmin(admin.ModelAdmin):
form = FlatpageForm
View
28 django/contrib/flatpages/forms.py
@@ -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()
View
18 django/contrib/flatpages/tests/forms.py
@@ -1,5 +1,5 @@
from django.conf import settings
-from django.contrib.flatpages.admin import FlatpageForm
+from django.contrib.flatpages.forms import FlatpageForm
from django.test import TestCase
class FlatpageAdminFormTests(TestCase):
@@ -11,7 +11,7 @@ def setUp(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='/some.special~chars/', **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 @@ def test_flatpage_admin_form_url_validation(self):
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']})
View
8 docs/ref/contrib/flatpages.txt
@@ -148,6 +148,14 @@ Via the Python API
.. 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
==================
Please sign in to comment.
Something went wrong with that request. Please try again.