Ticket #14678: 14678_v2.diff

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

Uses ModelForm's overridden clean method. Has tests. Has docs.

  • 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
     137.. admonition:: Manually check for duplicate flatpage URLs, if you are writing custom views for flatpages
     138
     139    In case you want to write custom views for manipulating flatpages, you
     140    will need to check for duplicate flatpage URLs. You can add
     141    ``from django.contrib.flatpages.forms import FlatPageForm`` in your
     142    custom view to use a FlatPage ModelForm that throws a ValidationError
     143    on finding flatpages with the same URL within a site.
     144
    137145Via the Python API
    138146------------------
    139147
     
    148156
    149157.. currentmodule:: django.contrib.flatpages
    150158
     159.. admonition:: Manually check for duplicate flatpage URLs.
     160
     161    You will need to manually check for duplicate flatpage URLs within a site,
     162    if you are adding flatpages via the Python API.
     163
    151164Flatpage templates
    152165==================
    153166
  • django/contrib/flatpages/admin.py

     
    1 from django import forms
    21from django.contrib import admin
    32from django.contrib.flatpages.models import FlatPage
    43from django.utils.translation import ugettext_lazy as _
     4from django.contrib.flatpages.forms import FlatpageForm
    55
    6 
    7 class FlatpageForm(forms.ModelForm):
    8     url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
    9         help_text = _("Example: '/about/contact/'. Make sure to have leading"
    10                       " and trailing slashes."),
    11         error_message = _("This value must contain only letters, numbers,"
    12                           " dots, underscores, dashes, slashes or tildes."))
    13 
    14     class Meta:
    15         model = FlatPage
    16 
    17 
    186class FlatPageAdmin(admin.ModelAdmin):
    197    form = FlatpageForm
    208    fieldsets = (
  • django/contrib/flatpages/tests/forms.py

     
    11from django.conf import settings
    2 from django.contrib.flatpages.admin import FlatpageForm
     2from django.contrib.flatpages.forms import FlatpageForm
    33from django.test import TestCase
    44
    55class FlatpageAdminFormTests(TestCase):
     
    1111        }
    1212
    1313    def test_flatpage_admin_form_url_validation(self):
    14         "The flatpage admin form validates correctly validates urls"
     14        "The flatpage admin form correctly validates urls"
    1515        self.assertTrue(FlatpageForm(data=dict(url='/new_flatpage/', **self.form_data)).is_valid())
    1616        self.assertTrue(FlatpageForm(data=dict(url='/some.special~chars/', **self.form_data)).is_valid())
    1717        self.assertTrue(FlatpageForm(data=dict(url='/some.very_special~chars-here/', **self.form_data)).is_valid())
     
    2121        self.assertFalse(FlatpageForm(data=dict(url='/a ! char/', **self.form_data)).is_valid())
    2222        self.assertFalse(FlatpageForm(data=dict(url='/a & char/', **self.form_data)).is_valid())
    2323        self.assertFalse(FlatpageForm(data=dict(url='/a ? char/', **self.form_data)).is_valid())
     24
     25    def test_flatpage_admin_form_url_uniqueness_validation(self):
     26        "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
     27        FlatpageForm(data=dict(url='/myflatpage1', **self.form_data)).save()
     28
     29        self.assertFalse(FlatpageForm(data=dict(url='/myflatpage1', **self.form_data)).is_valid())
  • django/contrib/flatpages/forms.py

     
     1from django import forms
     2from django.utils.translation import ugettext_lazy as _
     3from django.contrib.flatpages.models import FlatPage
     4
     5class FlatpageForm(forms.ModelForm):
     6    url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
     7        help_text = _("Example: '/about/contact/'. Make sure to have leading"
     8                      " and trailing slashes."),
     9        error_message = _("This value must contain only letters, numbers,"
     10                          " dots, underscores, dashes, slashes or tildes."))
     11
     12    class Meta:
     13        model = FlatPage
     14
     15    def clean(self):
     16        url = self.cleaned_data.get('url', None)
     17        sites = self.cleaned_data.get('sites', None)
     18
     19        flatpages_with_same_url = FlatPage.objects.filter(url=url)
     20
     21        if flatpages_with_same_url.filter(sites__in=sites).exists():
     22            for site in sites:
     23                if flatpages_with_same_url.filter(sites=site).exists():
     24                    raise forms.ValidationError(_('Flatpage with url %s already exists for site %s' % (url, site)))
     25
     26        return super(FlatpageForm, self).clean()
Back to Top