Ticket #14678: flatpages-ticket14678-2.diff

File flatpages-ticket14678-2.diff, 4.2 KB (added by Graham King, 13 years ago)

Fixed minor typo in warning message

  • docs/ref/contrib/flatpages.txt

     
    134134"Flatpages" section on the admin index page. Edit flatpages as you edit any
    135135other object in the system.
    136136
     137Take 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
    137139Via the Python API
    138140------------------
    139141
  • django/contrib/flatpages/admin.py

     
    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 belong 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

     
    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
    46
    57class FlatpageAdminFormTests(TestCase):
     
    1113        }
    1214
    1315    def test_flatpage_admin_form_url_validation(self):
    14         "The flatpage admin form validates correctly validates urls"
     16        "The flatpage admin form correctly validates urls"
    1517        self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
    1618        self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
    1719        self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **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())
    2325        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
Back to Top