Ticket #14678: flatpages-ticket14678.diff

File flatpages-ticket14678.diff, 4.4 KB (added by Joni, 14 years ago)
  • django/contrib/flatpages/admin.py

    diff --git a/django/contrib/flatpages/admin.py b/django/contrib/flatpages/admin.py
    index 1b377e9..74118fd 100644
    a b class FlatpageForm(forms.ModelForm):  
    1111        error_message = _("This value must contain only letters, numbers,"
    1212                          " dots, underscores, dashes, slashes or tildes."))
    1313
     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 belog to the same site." % (url, other_flatpage.title, site)))
     24                   
     25        return super(FlatpageForm, self).clean()
     26
    1427    class Meta:
    1528        model = FlatPage
    1629
  • django/contrib/flatpages/tests/forms.py

    diff --git a/django/contrib/flatpages/tests/forms.py b/django/contrib/flatpages/tests/forms.py
    index 969d347..f40ac0d 100644
    a b  
    11from django.contrib.flatpages.admin import FlatpageForm
     2from django.contrib.flatpages.models import FlatPage
     3from django.contrib.sites.models import Site
    24from django.test import TestCase
    35
    46class FlatpageAdminFormTests(TestCase):
    class FlatpageAdminFormTests(TestCase):  
    1012        }
    1113
    1214    def test_flatpage_admin_form_url_validation(self):
    13         "The flatpage admin form validates correctly validates urls"
     15        "The flatpage admin form correctly validates urls"
    1416        self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
    1517        self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
    1618        self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid())
    class FlatpageAdminFormTests(TestCase):  
    2022        self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
    2123        self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
    2224        self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid())
     25
     26    def test_url_must_be_unique_for_all_pages_in_the_same_site(self):
     27        "The flatpage admin form correctly enforces url uniqueness among all flatpages belonging to the same site"
     28       
     29        site1 = Site.objects.create(domain='site1.com', name='site1')
     30        site1.save()
     31       
     32        site2 = Site.objects.create(domain='site2.com', name='site2')
     33        site2.save()
     34       
     35        my_page = FlatPage.objects.create(
     36            url="/my-page/",
     37            title="This is my page",
     38            content="Isn't it special!",
     39            enable_comments=False,
     40            registration_required=False,
     41        )
     42        my_page.sites.add(site1) # add my_page to site1
     43        my_page.save()
     44       
     45        new_page_data = {
     46            'title': "A test page",
     47            'content': "This is a test",
     48        }
     49       
     50        # New page has the same url as my_page and is in the same site -> error
     51        self.assertFalse(FlatpageForm(data=dict(url='/my-page/', sites=[site1.pk], **new_page_data)).is_valid())
     52       
     53        # New page has the same url as my_page but is in another site -> ok
     54        self.assertTrue(FlatpageForm(data=dict(url='/my-page/', sites=[site2.pk], **new_page_data)).is_valid())
     55 No newline at end of file
  • docs/ref/contrib/flatpages.txt

    diff --git a/docs/ref/contrib/flatpages.txt b/docs/ref/contrib/flatpages.txt
    index ce6fdfc..ecb0c76 100644
    a b If you've activated the automatic Django admin interface, you should see a  
    116116"Flatpages" section on the admin index page. Edit flatpages as you edit any
    117117other object in the system.
    118118
     119Take 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.
     120
    119121Via the Python API
    120122------------------
    121123
Back to Top