Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

forms.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django import forms
  2. from django.db.models.loading import get_model
  3. from django.utils.translation import ugettext_lazy as _
  4. from oscar.core.validators import URLDoesNotExistValidator
  5. FlatPage = get_model('flatpages', 'FlatPage')
  6. class PageSearchForm(forms.Form):
  7. """
  8. Search form to filter pages by *title.
  9. """
  10. title = forms.CharField(required=False, label=_("Title"))
  11. class PageUpdateForm(forms.ModelForm):
  12. """
  13. Update form to create/update flatpages. It provides a *title*, *url*,
  14. and *content* field. The specified URL will be validated and check if
  15. the same URL already exists in the system.
  16. """
  17. url = forms.CharField(max_length=128, required=False, label=_("URL"),
  18. help_text=_("Example: '/about/contact/'. Make sure"
  19. " to have leading and trailing slashes."))
  20. def clean_url(self):
  21. """
  22. Validate the input for field *url* checking if the specified
  23. URL already exists. If it is an actual update and the URL has
  24. not been changed, validation will be skipped.
  25. Returns cleaned URL or raises an exception.
  26. """
  27. if 'url' in self.changed_data:
  28. if not self.cleaned_data['url'].endswith('/'):
  29. self.cleaned_data['url'] += '/'
  30. URLDoesNotExistValidator()(self.cleaned_data['url'])
  31. return self.cleaned_data['url']
  32. class Meta:
  33. model = FlatPage
  34. fields = ('title', 'url', 'content')