dnmgmt/whoisdb/validators.py

45 lines
1.3 KiB
Python
Raw Normal View History

2018-01-17 00:23:00 +01:00
# This file is part of dnmgmt, a number resource management system
# Licensed under GNU General Public License v3 or later
# Written by Sebastian Lohff (seba@someserver.de)
2017-02-15 02:35:46 +01:00
from django.core import validators
2017-03-21 02:36:07 +01:00
from django.core.exceptions import ValidationError
2017-02-15 02:35:46 +01:00
from django.utils import six
from django.utils.deconstruct import deconstructible
from django.utils.translation import ugettext_lazy as _
2017-02-23 22:28:06 +01:00
import re
2017-03-21 02:36:07 +01:00
import ipaddress
2017-02-23 22:28:06 +01:00
2017-02-24 03:43:59 +01:00
2017-02-15 02:35:46 +01:00
@deconstructible
class HandleValidator(validators.RegexValidator):
2019-05-30 22:10:55 +02:00
regex = r'^(?:[A-Z]+[0-9]*(-[A-Z]+)|AUTO)$'
message = _(
'Enter a valid handle (all uppercase)'
)
flags = re.ASCII if six.PY3 else 0
2017-02-24 03:43:59 +01:00
@deconstructible
class HandleValidatorWithSuffix(validators.RegexValidator):
2019-05-30 22:10:55 +02:00
flags = re.ASCII if six.PY3 else 0
2017-02-24 03:43:59 +01:00
2019-05-30 22:10:55 +02:00
def __init__(self, suffix):
self.regex = r'^(?:[A-Z]+[0-9]*-%s|AUTO)$' % re.escape(suffix)
self.message = _(
'Enter a valid handle with suffix %s (all uppercase), e.g. FOO3-%s' % (suffix, suffix)
)
2017-02-15 02:35:46 +01:00
2019-05-30 22:10:55 +02:00
super(HandleValidatorWithSuffix, self).__init__()
2017-03-21 02:36:07 +01:00
2017-03-22 04:06:24 +01:00
2017-03-21 02:36:07 +01:00
def IP46CIDRValidator(value):
2019-05-30 22:10:55 +02:00
if not re.match(r"[0-9a-fA-F:.]+/[0-9]+", value):
raise ValidationError("Address needs to be a subnet in the format of ip/prefix")
2017-03-21 02:36:07 +01:00
2019-05-30 22:10:55 +02:00
try:
ipaddress.ip_network(value)
except ValueError as e:
raise ValidationError(str(e))