You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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