Ticket #12972: 12972-2.diff

File 12972-2.diff, 3.2 KB (added by Claude Paroz, 13 years ago)

Implement check in clean_url instead of regex

  • django/contrib/flatpages/forms.py

    diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py
    index 3916c85..347b5f8 100644
    a b  
    11from django import forms
    2 from django.utils.translation import ugettext_lazy as _
     2from django.conf import settings
    33from django.contrib.flatpages.models import FlatPage
     4from django.utils.translation import ugettext, ugettext_lazy as _
     5
    46
    57class FlatpageForm(forms.ModelForm):
    68    url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
    class FlatpageForm(forms.ModelForm):  
    1214    class Meta:
    1315        model = FlatPage
    1416
     17    def clean_url(self):
     18        url = self.cleaned_data['url']
     19        if not url.startswith("/"):
     20            raise forms.ValidationError(ugettext("Missing leading slash"))
     21        if settings.APPEND_SLASH and \
     22           'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES and \
     23           not url.endswith("/"):
     24            raise forms.ValidationError(ugettext("Missing trailing slash"))
     25        return url
     26
    1527    def clean(self):
    1628        url = self.cleaned_data.get('url', None)
    1729        sites = self.cleaned_data.get('sites', None)
  • django/contrib/flatpages/tests/forms.py

    diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py
    index 7f617d5..2112f50 100644
    a b from django.conf import settings  
    22from django.contrib.flatpages.forms import FlatpageForm
    33from django.contrib.flatpages.models import FlatPage
    44from django.test import TestCase
     5from django.test.utils import override_settings
    56
    67class FlatpageAdminFormTests(TestCase):
    78    def setUp(self):
    class FlatpageAdminFormTests(TestCase):  
    2223        self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
    2324        self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
    2425        self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid())
     26        form = FlatpageForm(data=dict(url='no_leading_slash/', **self.form_data))
     27        self.assertFalse(form.is_valid())
     28        self.assertEqual(form.errors['url'], ["Missing leading slash"])
     29        if settings.APPEND_SLASH and \
     30           'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES:
     31            form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
     32            self.assertFalse(form.is_valid())
     33            self.assertEqual(form.errors['url'], ["Missing trailing slash"])
    2534
     35    @override_settings(APPEND_SLASH=False)
    2636    def test_flatpage_admin_form_url_uniqueness_validation(self):
    2737        "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
    2838        data = dict(url='/myflatpage1', **self.form_data)
    class FlatpageAdminFormTests(TestCase):  
    3747            f.errors,
    3848            {'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']})
    3949
     50    @override_settings(APPEND_SLASH=False)
    4051    def test_flatpage_admin_form_edit(self):
    4152        """
    4253        Existing flatpages can be edited in the admin form without triggering
Back to Top