Ticket #14678: 14678_v2.diff
File 14678_v2.diff, 5.1 KB (added by , 13 years ago) |
---|
-
docs/ref/contrib/flatpages.txt
134 134 "Flatpages" section on the admin index page. Edit flatpages as you edit any 135 135 other object in the system. 136 136 137 .. admonition:: Manually check for duplicate flatpage URLs, if you are writing custom views for flatpages 138 139 In case you want to write custom views for manipulating flatpages, you 140 will need to check for duplicate flatpage URLs. You can add 141 ``from django.contrib.flatpages.forms import FlatPageForm`` in your 142 custom view to use a FlatPage ModelForm that throws a ValidationError 143 on finding flatpages with the same URL within a site. 144 137 145 Via the Python API 138 146 ------------------ 139 147 … … 148 156 149 157 .. currentmodule:: django.contrib.flatpages 150 158 159 .. admonition:: Manually check for duplicate flatpage URLs. 160 161 You will need to manually check for duplicate flatpage URLs within a site, 162 if you are adding flatpages via the Python API. 163 151 164 Flatpage templates 152 165 ================== 153 166 -
django/contrib/flatpages/admin.py
1 from django import forms2 1 from django.contrib import admin 3 2 from django.contrib.flatpages.models import FlatPage 4 3 from django.utils.translation import ugettext_lazy as _ 4 from django.contrib.flatpages.forms import FlatpageForm 5 5 6 7 class FlatpageForm(forms.ModelForm):8 url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',9 help_text = _("Example: '/about/contact/'. Make sure to have leading"10 " and trailing slashes."),11 error_message = _("This value must contain only letters, numbers,"12 " dots, underscores, dashes, slashes or tildes."))13 14 class Meta:15 model = FlatPage16 17 18 6 class FlatPageAdmin(admin.ModelAdmin): 19 7 form = FlatpageForm 20 8 fieldsets = ( -
django/contrib/flatpages/tests/forms.py
1 1 from django.conf import settings 2 from django.contrib.flatpages. adminimport FlatpageForm2 from django.contrib.flatpages.forms import FlatpageForm 3 3 from django.test import TestCase 4 4 5 5 class FlatpageAdminFormTests(TestCase): … … 11 11 } 12 12 13 13 def test_flatpage_admin_form_url_validation(self): 14 "The flatpage admin form validatescorrectly validates urls"14 "The flatpage admin form correctly validates urls" 15 15 self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid()) 16 16 self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid()) 17 17 self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid()) … … 21 21 self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid()) 22 22 self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid()) 23 23 self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid()) 24 25 def test_flatpage_admin_form_url_uniqueness_validation(self): 26 "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site" 27 FlatpageForm(data=dict(url='/myflatpage1', **self.form_data)).save() 28 29 self.assertFalse(FlatpageForm(data=dict(url='/myflatpage1', **self.form_data)).is_valid()) -
django/contrib/flatpages/forms.py
1 from django import forms 2 from django.utils.translation import ugettext_lazy as _ 3 from django.contrib.flatpages.models import FlatPage 4 5 class FlatpageForm(forms.ModelForm): 6 url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$', 7 help_text = _("Example: '/about/contact/'. Make sure to have leading" 8 " and trailing slashes."), 9 error_message = _("This value must contain only letters, numbers," 10 " dots, underscores, dashes, slashes or tildes.")) 11 12 class Meta: 13 model = FlatPage 14 15 def clean(self): 16 url = self.cleaned_data.get('url', None) 17 sites = self.cleaned_data.get('sites', None) 18 19 flatpages_with_same_url = FlatPage.objects.filter(url=url) 20 21 if flatpages_with_same_url.filter(sites__in=sites).exists(): 22 for site in sites: 23 if flatpages_with_same_url.filter(sites=site).exists(): 24 raise forms.ValidationError(_('Flatpage with url %s already exists for site %s' % (url, site))) 25 26 return super(FlatpageForm, self).clean()