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."))
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."))
27 url
= self
.cleaned_data
.get('url', None)
28 sites
= self
.cleaned_data
.get('sites', None)
30 same_url
= FlatPage
.objects
.filter(url
=url
)
32 same_url
= same_url
.exclude(pk
=self
.instance
.pk
)
34 if same_url
.filter(sites__in
=sites
).exists():
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()