From 72a716743929afd613fb5242d2525265ec27b2c5 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 28 Aug 2010 12:25:54 +0000 Subject: [PATCH] Fixed #5192 -- Modified flatpage admin form to allow ~ and . characters in flatpage names. Thanks to marco.giusti@gmail.com for the report, Idan Gazit for summarizing the issue on the ticket, and ctrochalakis for the initial patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13655 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/flatpages/admin.py | 4 ++-- django/contrib/flatpages/tests/__init__.py | 1 + django/contrib/flatpages/tests/forms.py | 22 ++++++++++++++++++++++ django/contrib/flatpages/tests/views.py | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 django/contrib/flatpages/tests/forms.py diff --git a/django/contrib/flatpages/admin.py b/django/contrib/flatpages/admin.py index b6fdba3d53..1b377e967b 100644 --- a/django/contrib/flatpages/admin.py +++ b/django/contrib/flatpages/admin.py @@ -5,11 +5,11 @@ from django.utils.translation import ugettext_lazy as _ class FlatpageForm(forms.ModelForm): - url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/]+$', + 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," - " underscores, dashes or slashes.")) + " dots, underscores, dashes, slashes or tildes.")) class Meta: model = FlatPage diff --git a/django/contrib/flatpages/tests/__init__.py b/django/contrib/flatpages/tests/__init__.py index edbc108d4c..5dd5e89dca 100644 --- a/django/contrib/flatpages/tests/__init__.py +++ b/django/contrib/flatpages/tests/__init__.py @@ -1,4 +1,5 @@ from django.contrib.flatpages.tests.csrf import * +from django.contrib.flatpages.tests.forms import * from django.contrib.flatpages.tests.middleware import * from django.contrib.flatpages.tests.templatetags import * from django.contrib.flatpages.tests.views import * diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py new file mode 100644 index 0000000000..969d347b39 --- /dev/null +++ b/django/contrib/flatpages/tests/forms.py @@ -0,0 +1,22 @@ +from django.contrib.flatpages.admin import FlatpageForm +from django.test import TestCase + +class FlatpageAdminFormTests(TestCase): + def setUp(self): + self.form_data = { + 'title': "A test page", + 'content': "This is a test", + 'sites': [1], + } + + def test_flatpage_admin_form_url_validation(self): + "The flatpage admin form validates 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()) + + self.assertFalse(FlatpageForm(data=dict(url='/a space/', **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()) diff --git a/django/contrib/flatpages/tests/views.py b/django/contrib/flatpages/tests/views.py index 128669095e..89bdde2d92 100644 --- a/django/contrib/flatpages/tests/views.py +++ b/django/contrib/flatpages/tests/views.py @@ -1,6 +1,7 @@ import os from django.conf import settings from django.contrib.auth.models import User +from django.contrib.flatpages.models import FlatPage from django.test import TestCase class FlatpageViewTests(TestCase): @@ -54,3 +55,18 @@ class FlatpageViewTests(TestCase): "A non-existent flatpage won't be served if the fallback middlware is disabled" response = self.client.get('/no_such_flatpage/') self.assertEquals(response.status_code, 404) + + def test_view_flatpage_special_chars(self): + "A flatpage with special chars in the URL can be served through a view" + fp = FlatPage.objects.create( + url="/some.very_special~chars-here/", + title="A very special page", + content="Isn't it special!", + enable_comments=False, + registration_required=False, + ) + fp.sites.add(1) + + response = self.client.get('/flatpage_root/some.very_special~chars-here/') + self.assertEquals(response.status_code, 200) + self.assertContains(response, "

Isn't it special!

")