2020-11-05 09:34:31 +00:00
|
|
|
import re
|
2020-05-29 13:25:36 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-29 13:25:36 +00:00
|
|
|
|
2022-11-02 05:54:35 +00:00
|
|
|
have_symbol = re.compile("[^a-zA-Z0-9._-]+")
|
|
|
|
wrong_ip = re.compile("^0.|^255.")
|
|
|
|
wrong_name = re.compile("[^a-zA-Z0-9._-]+")
|
2020-05-29 13:25:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_hostname(value):
|
|
|
|
sym = have_symbol.match(value)
|
|
|
|
wip = wrong_ip.match(value)
|
|
|
|
|
|
|
|
if sym:
|
2022-11-02 05:54:35 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_('Hostname must contain only numbers, or the domain name separated by "."')
|
|
|
|
)
|
2020-05-29 13:25:36 +00:00
|
|
|
elif wip:
|
2022-11-02 05:54:35 +00:00
|
|
|
raise ValidationError(_("Wrong IP address"))
|
2020-05-29 13:25:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_name(value):
|
2022-11-02 05:54:35 +00:00
|
|
|
have_symbol = wrong_name.match("[^a-zA-Z0-9._-]+")
|
2020-05-29 13:25:36 +00:00
|
|
|
if have_symbol:
|
2022-11-02 05:54:35 +00:00
|
|
|
raise ValidationError(_("The hostname must not contain any special characters"))
|