No Description
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.

validators.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # This file is part of dnmgmt, a number resource management system
  2. # Licensed under GNU General Public License v3 or later
  3. # Written by Sebastian Lohff (seba@someserver.de)
  4. from django.core import validators
  5. from django.core.exceptions import ValidationError
  6. from django.utils import six
  7. from django.utils.deconstruct import deconstructible
  8. from django.utils.translation import ugettext_lazy as _
  9. import re
  10. import ipaddress
  11. @deconstructible
  12. class HandleValidator(validators.RegexValidator):
  13. regex = r'^(?:[A-Z]+[0-9]*(-[A-Z]+)|AUTO)'
  14. message = _(
  15. 'Enter a valid handle (all uppercase)'
  16. )
  17. flags = re.ASCII if six.PY3 else 0
  18. @deconstructible
  19. class HandleValidatorWithSuffix(validators.RegexValidator):
  20. flags = re.ASCII if six.PY3 else 0
  21. def __init__(self, suffix):
  22. self.regex = r'^(?:[A-Z]+[0-9]*-%s|AUTO)' % re.escape(suffix)
  23. self.message = _(
  24. 'Enter a valid handle with suffix %s (all uppercase), e.g. FOO3-%s' % (suffix, suffix)
  25. )
  26. super(HandleValidatorWithSuffix, self).__init__()
  27. def IP46CIDRValidator(value):
  28. if not re.match(r"[0-9a-fA-F:.]+/[0-9]+", value):
  29. raise ValidationError("Address needs to be a subnet in the format of ip/prefix")
  30. try:
  31. ipaddress.ip_network(value)
  32. except ValueError as e:
  33. raise ValidationError(str(e))