Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

55 rader
2.0 KiB

  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 _
  5. class 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_messages={
  10. "invalid": _("This value must contain only letters, numbers,"
  11. " dots, underscores, dashes, slashes or tildes."),
  12. },
  13. )
  14. class Meta:
  15. model = FlatPage
  16. fields = '__all__'
  17. def clean_url(self):
  18. url = self.cleaned_data['url']
  19. if not url.startswith('/'):
  20. raise forms.ValidationError(
  21. ugettext("URL is missing a leading slash."),
  22. code='missing_leading_slash',
  23. )
  24. if (settings.APPEND_SLASH and
  25. 'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE_CLASSES and
  26. not url.endswith('/')):
  27. raise forms.ValidationError(
  28. ugettext("URL is missing a trailing slash."),
  29. code='missing_trailing_slash',
  30. )
  31. return url
  32. def clean(self):
  33. url = self.cleaned_data.get('url')
  34. sites = self.cleaned_data.get('sites')
  35. same_url = FlatPage.objects.filter(url=url)
  36. if self.instance.pk:
  37. same_url = same_url.exclude(pk=self.instance.pk)
  38. if sites and same_url.filter(sites__in=sites).exists():
  39. for site in sites:
  40. if same_url.filter(sites=site).exists():
  41. raise forms.ValidationError(
  42. _('Flatpage with url %(url)s already exists for site %(site)s'),
  43. code='duplicate_url',
  44. params={'url': url, 'site': site},
  45. )
  46. return super(FlatpageForm, self).clean()