App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / contrib / flatpages / forms.py
blob8ffae833e5cc4fd9b43c85993f33db85c061871a
1 from django import forms
2 from django.conf import settings
3 from django.contrib.flatpages.models import FlatPage
4 from django.utils.translation import ugettext, ugettext_lazy as _
6 class FlatpageForm(forms.ModelForm):
7 url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
8 help_text = _("Example: '/about/contact/'. Make sure to have leading"
9 " and trailing slashes."),
10 error_message = _("This value must contain only letters, numbers,"
11 " dots, underscores, dashes, slashes or tildes."))
13 class Meta:
14 model = FlatPage
16 def clean_url(self):
17 url = self.cleaned_data['url']
18 if not url.startswith('/'):
19 raise forms.ValidationError(ugettext("URL is missing a leading slash."))
20 if (settings.APPEND_SLASH and
21 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES and
22 not url.endswith('/')):
23 raise forms.ValidationError(ugettext("URL is missing a trailing slash."))
24 return url
26 def clean(self):
27 url = self.cleaned_data.get('url', None)
28 sites = self.cleaned_data.get('sites', None)
30 same_url = FlatPage.objects.filter(url=url)
31 if self.instance.pk:
32 same_url = same_url.exclude(pk=self.instance.pk)
34 if same_url.filter(sites__in=sites).exists():
35 for site in sites:
36 if same_url.filter(sites=site).exists():
37 raise forms.ValidationError(
38 _('Flatpage with url %(url)s already exists for site %(site)s' %
39 {'url': url, 'site': site}))
41 return super(FlatpageForm, self).clean()