Ticket #14678: flatpages-ticket14678-2.diff
File flatpages-ticket14678-2.diff, 4.2 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 Take into consideration that the admin interface won't allow you to create two flatpages with the same url if they both belong to the same Site. 138 137 139 Via the Python API 138 140 ------------------ 139 141 -
django/contrib/flatpages/admin.py
11 11 error_message = _("This value must contain only letters, numbers," 12 12 " dots, underscores, dashes, slashes or tildes.")) 13 13 14 def clean(self): 15 url = self.cleaned_data.get('url') 16 sites = self.cleaned_data.get('sites') 17 if url and sites: 18 other_flatpages_with_same_url = FlatPage.objects.exclude(pk=self.instance.pk).filter(url=url) 19 20 for other_flatpage in other_flatpages_with_same_url: 21 for site in sites: 22 if site in other_flatpage.sites.all(): 23 raise forms.ValidationError(_("URL '%s' is already assigned to another FlatPage '%s' that also belongs to the site '%s'. Two FlatPages can't have the same URL if they belong to the same site." % (url, other_flatpage.title, site))) 24 25 return super(FlatpageForm, self).clean() 26 14 27 class Meta: 15 28 model = FlatPage 16 29 -
django/contrib/flatpages/tests/forms.py
1 1 from django.conf import settings 2 2 from django.contrib.flatpages.admin import FlatpageForm 3 from django.contrib.flatpages.models import FlatPage 4 from django.contrib.sites.models import Site 3 5 from django.test import TestCase 4 6 5 7 class FlatpageAdminFormTests(TestCase): … … 11 13 } 12 14 13 15 def test_flatpage_admin_form_url_validation(self): 14 "The flatpage admin form validatescorrectly validates urls"16 "The flatpage admin form correctly validates urls" 15 17 self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid()) 16 18 self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid()) 17 19 self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid()) … … 21 23 self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid()) 22 24 self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid()) 23 25 self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid()) 26 27 def test_url_must_be_unique_for_all_pages_in_the_same_site(self): 28 "The flatpage admin form correctly enforces url uniqueness among all flatpages belonging to the same site" 29 30 site1 = Site.objects.create(domain='site1.com', name='site1') 31 site1.save() 32 33 site2 = Site.objects.create(domain='site2.com', name='site2') 34 site2.save() 35 36 my_page = FlatPage.objects.create( 37 url="/my-page/", 38 title="This is my page", 39 content="Isn't it special!", 40 enable_comments=False, 41 registration_required=False, 42 ) 43 my_page.sites.add(site1) # add my_page to site1 44 my_page.save() 45 46 new_page_data = { 47 'title': "A test page", 48 'content': "This is a test", 49 } 50 51 # New page has the same url as my_page and is in the same site -> error 52 self.assertFalse(FlatpageForm(data=dict(url='/my-page/', sites=[site1.pk], **new_page_data)).is_valid()) 53 54 # New page has the same url as my_page but is in another site -> ok 55 self.assertTrue(FlatpageForm(data=dict(url='/my-page/', sites=[site2.pk], **new_page_data)).is_valid()) 56 No newline at end of file