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.

fields.py 1.1KB

12345678910111213141516171819202122232425262728
  1. from django.forms import fields
  2. from django.core.validators import URL_VALIDATOR_USER_AGENT
  3. from oscar.core import validators
  4. class ExtendedURLField(fields.URLField):
  5. """
  6. Custom field similar to URLField type field, however also accepting and
  7. validating local relative URLs, ie. '/product/'
  8. """
  9. def __init__(self, max_length=None, min_length=None, verify_exists=False,
  10. validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
  11. # intentionally skip one step when calling super()
  12. super(fields.URLField, self).__init__(max_length, min_length, *args,
  13. **kwargs)
  14. validator = validators.ExtendedURLValidator(
  15. verify_exists=verify_exists,
  16. validator_user_agent=validator_user_agent)
  17. self.validators.append(validator)
  18. def to_python(self, value):
  19. # We need to avoid having 'http' inserted at the start of
  20. # every value
  21. if value and value.startswith('/'):
  22. return value
  23. return super(ExtendedURLField, self).to_python(value)