Ticket #14678: 14678.diff

File 14678.diff, 3.0 KB (added by Kushagra Sinha, 13 years ago)

Checks for duplicate flatpages and throws an exception

  • django/contrib/flatpages/tests/forms.py

     
    11from django.conf import settings
    22from django.contrib.flatpages.admin import FlatpageForm
     3from django.contrib.flatpages.models import FlatPage
     4from django.contrib.sites.models import Site
    35from django.test import TestCase
     6from django.db.utils import IntegrityError
    47
    58class FlatpageAdminFormTests(TestCase):
    69    def setUp(self):
     
    1114        }
    1215
    1316    def test_flatpage_admin_form_url_validation(self):
    14         "The flatpage admin form validates correctly validates urls"
     17        "The flatpage admin form correctly validates urls"
    1518        self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
    1619        self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
    1720        self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid())
     
    2124        self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
    2225        self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
    2326        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

     
    11from django.db import models
    22from django.contrib.sites.models import Site
    33from django.utils.translation import ugettext_lazy as _
     4from django.db.models.signals import m2m_changed
     5from django.dispatch import receiver
     6from django.db.utils import IntegrityError
    47
    58
    69class FlatPage(models.Model):
     
    2427
    2528    def get_absolute_url(self):
    2629        return self.url
     30
     31@receiver(m2m_changed, sender=FlatPage.sites.through)
     32def 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)))
Back to Top