2011-10-07 23:45:52 +08:00
|
|
|
from django import forms
|
2012-01-29 04:27:23 +08:00
|
|
|
from django.conf import settings
|
2011-10-07 23:45:52 +08:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
2012-01-29 04:27:23 +08:00
|
|
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
2011-10-07 23:45:52 +08:00
|
|
|
|
|
|
|
class FlatpageForm(forms.ModelForm):
|
|
|
|
url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
|
|
|
|
help_text = _("Example: '/about/contact/'. Make sure to have leading"
|
|
|
|
" and trailing slashes."),
|
|
|
|
error_message = _("This value must contain only letters, numbers,"
|
|
|
|
" dots, underscores, dashes, slashes or tildes."))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = FlatPage
|
|
|
|
|
2012-01-29 04:27:23 +08:00
|
|
|
def clean_url(self):
|
|
|
|
url = self.cleaned_data['url']
|
|
|
|
if not url.startswith('/'):
|
|
|
|
raise forms.ValidationError(ugettext("URL is missing a leading slash."))
|
|
|
|
if (settings.APPEND_SLASH and
|
|
|
|
'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES and
|
|
|
|
not url.endswith('/')):
|
|
|
|
raise forms.ValidationError(ugettext("URL is missing a trailing slash."))
|
|
|
|
return url
|
|
|
|
|
2011-10-07 23:45:52 +08:00
|
|
|
def clean(self):
|
|
|
|
url = self.cleaned_data.get('url', None)
|
|
|
|
sites = self.cleaned_data.get('sites', None)
|
|
|
|
|
2011-10-17 08:56:32 +08:00
|
|
|
same_url = FlatPage.objects.filter(url=url)
|
|
|
|
if self.instance.pk:
|
|
|
|
same_url = same_url.exclude(pk=self.instance.pk)
|
2011-10-07 23:45:52 +08:00
|
|
|
|
2012-04-29 07:44:14 +08:00
|
|
|
if sites and same_url.filter(sites__in=sites).exists():
|
2011-10-07 23:45:52 +08:00
|
|
|
for site in sites:
|
2011-10-17 08:56:32 +08:00
|
|
|
if same_url.filter(sites=site).exists():
|
2011-10-07 23:45:52 +08:00
|
|
|
raise forms.ValidationError(
|
2012-11-16 05:53:08 +08:00
|
|
|
_('Flatpage with url %(url)s already exists for site %(site)s') %
|
|
|
|
{'url': url, 'site': site})
|
2011-10-07 23:45:52 +08:00
|
|
|
|
|
|
|
return super(FlatpageForm, self).clean()
|