Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334
  1. from django.forms import fields
  2. from oscar.core import validators
  3. class ExtendedURLField(fields.URLField):
  4. """
  5. Custom field similar to URLField type field, however also accepting and
  6. validating local relative URLs, ie. '/product/'
  7. """
  8. default_validators = []
  9. def __init__(self, max_length=None, min_length=None, verify_exists=None,
  10. *args, **kwargs):
  11. # We don't pass on verify_exists as it has been deprecated in Django
  12. # 1.4
  13. super(fields.URLField, self).__init__(
  14. max_length, min_length, *args, **kwargs)
  15. # Even though it is deprecated, we still pass on 'verify_exists' as
  16. # Oscar's ExtendedURLValidator uses it to determine whether to test
  17. # local URLs.
  18. if verify_exists is not None:
  19. validator = validators.ExtendedURLValidator(
  20. verify_exists=verify_exists)
  21. else:
  22. validator = validators.ExtendedURLValidator()
  23. self.validators.append(validator)
  24. def to_python(self, value):
  25. # We need to avoid having 'http' inserted at the start of
  26. # every value so that local URLs are valid.
  27. if value and value.startswith('/'):
  28. return value
  29. return super(ExtendedURLField, self).to_python(value)