Ticket #12972: 12972.diff

File 12972.diff, 3.3 KB (added by claudep, 4 years ago)

Updated patch

  • django/contrib/flatpages/forms.py

    diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py
    index 3916c85..1341d92 100644
    a b  
     1import re
     2
    13from django import forms
    2 from django.utils.translation import ugettext_lazy as _
     4from django.conf import settings
    35from django.contrib.flatpages.models import FlatPage
     6from django.utils.translation import ugettext_lazy as _
     7
     8URL_REGEX = r'^/[-\w/\.~]+/$'
    49
    510class FlatpageForm(forms.ModelForm):
    6     url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
     11    url = forms.RegexField(label=_("URL"), max_length=100, regex=URL_REGEX,
    712        help_text = _("Example: '/about/contact/'. Make sure to have leading"
    813                      " and trailing slashes."),
    914        error_message = _("This value must contain only letters, numbers,"
    class FlatpageForm(forms.ModelForm): 
    1217    class Meta:
    1318        model = FlatPage
    1419
     20    def __init__(self, *args, **kwargs):
     21        super(FlatpageForm, self).__init__(*args, **kwargs)
     22        if not settings.APPEND_SLASH or \
     23           not 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES:
     24            # Make trailing slash optional
     25            self.fields['url'].regex = re.compile(r'%s$' % URL_REGEX[:-2])
     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..55709bd 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        self.assertFalse(FlatpageForm(data=dict(url='no_leading_slash/', **self.form_data)).is_valid())
     27        if settings.APPEND_SLASH and \
     28           'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES:
     29            form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
     30            self.assertFalse(form.is_valid())
    2531
    26     def test_flatpage_admin_form_url_uniqueness_validation(self):
     32    @override_settings(APPEND_SLASH=False)
     33    def _test_flatpage_admin_form_url_uniqueness_validation(self):
    2734        "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
    2835        data = dict(url='/myflatpage1', **self.form_data)
    2936
    class FlatpageAdminFormTests(TestCase): 
    3744            f.errors,
    3845            {'__all__': [u'Flatpage with url /myflatpage1 already exists for site example.com']})
    3946
     47    @override_settings(APPEND_SLASH=False)
    4048    def test_flatpage_admin_form_edit(self):
    4149        """
    4250        Existing flatpages can be edited in the admin form without triggering
Back to Top