validators.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from __future__ import unicode_literals
  2. import re
  3. from django.core.validators import _lazy_re_compile, URLValidator
  4. class EnhancedURLValidator(URLValidator):
  5. """
  6. Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension.
  7. """
  8. class AnyURLScheme(object):
  9. """
  10. A fake URL list which "contains" all scheme names abiding by the syntax defined in RFC 3986 section 3.1
  11. """
  12. def __contains__(self, item):
  13. if not item or not re.match('^[a-z][0-9a-z+\-.]*$', item.lower()):
  14. return False
  15. return True
  16. fqdn_re = URLValidator.hostname_re + URLValidator.domain_re + URLValidator.tld_re
  17. host_res = [URLValidator.ipv4_re, URLValidator.ipv6_re, fqdn_re, URLValidator.hostname_re]
  18. regex = _lazy_re_compile(
  19. r'^(?:[a-z0-9\.\-\+]*)://' # Scheme (previously enforced by AnyURLScheme or schemes kwarg)
  20. r'(?:\S+(?::\S*)?@)?' # HTTP basic authentication
  21. r'(?:' + '|'.join(host_res) + ')' # IPv4, IPv6, FQDN, or hostname
  22. r'(?::\d{2,5})?' # Port number
  23. r'(?:[/?#][^\s]*)?' # Path
  24. r'\Z', re.IGNORECASE)
  25. schemes = AnyURLScheme()