Ticket #14678: 14678.diff
File 14678.diff, 3.0 KB (added by , 13 years ago) |
---|
-
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 6 from django.db.utils import IntegrityError 4 7 5 8 class FlatpageAdminFormTests(TestCase): 6 9 def setUp(self): … … 11 14 } 12 15 13 16 def test_flatpage_admin_form_url_validation(self): 14 "The flatpage admin form validatescorrectly validates urls"17 "The flatpage admin form correctly validates urls" 15 18 self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid()) 16 19 self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid()) 17 20 self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid()) … … 21 24 self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid()) 22 25 self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid()) 23 26 self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid()) 27 28 def test_url_must_be_unique_for_all_pages_in_the_same_site(self): 29 "The flatpage admin form correctly enforces url uniqueness among all flatpages belonging to the same site" 30 flat_page = FlatPage.objects.create(url='/flatpages/mypage/', title='My Page', content='My Page') 31 flat_page.sites.add(1) 32 flat_page.save() 33 34 duplicate = FlatPage.objects.create(url='/flatpages/mypage/', title='Duplicate', content='Duplicate') 35 self.assertRaises(IntegrityError, duplicate.sites.add, 1) -
django/contrib/flatpages/models.py
1 1 from django.db import models 2 2 from django.contrib.sites.models import Site 3 3 from django.utils.translation import ugettext_lazy as _ 4 from django.db.models.signals import m2m_changed 5 from django.dispatch import receiver 6 from django.db.utils import IntegrityError 4 7 5 8 6 9 class FlatPage(models.Model): … … 24 27 25 28 def get_absolute_url(self): 26 29 return self.url 30 31 @receiver(m2m_changed, sender=FlatPage.sites.through) 32 def verify_flatpage_uniqueness(sender, **kwargs): 33 flat_page = kwargs.get('instance', None) 34 action = kwargs.get('action', None) 35 sites = kwargs.get('pk_set', None) 36 37 if action == 'pre_add': 38 for site in sites: 39 if FlatPage.objects.filter(url=flat_page.url).filter(sites=site): 40 raise IntegrityError('Flatpage with url %s already exists for site %s' % (flat_page.url, Site.objects.get(pk=site)))