Fixes #3: validation error in reverse zone

This commit is contained in:
Sander Steffann 2020-06-24 12:52:04 +02:00
parent adfa056939
commit e83a3aa6ee
2 changed files with 9 additions and 9 deletions

View file

@ -1,4 +1,4 @@
VERSION = '1.0.2' VERSION = '1.0.3'
try: try:
from extras.plugins import PluginConfig from extras.plugins import PluginConfig

View file

@ -1,9 +1,7 @@
import logging
import socket
from typing import Optional
import dns.tsigkeyring import dns.tsigkeyring
import dns.update import dns.update
import logging
import socket
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.db.models.functions import Length from django.db.models.functions import Length
@ -11,7 +9,8 @@ from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from dns import rcode from dns import rcode
from dns.tsig import HMAC_MD5, HMAC_SHA1, HMAC_SHA224, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512 from dns.tsig import HMAC_MD5, HMAC_SHA1, HMAC_SHA224, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512
from netaddr import ip from netaddr import IPNetwork, ip
from typing import Optional
from ipam.fields import IPNetworkField from ipam.fields import IPNetworkField
from ipam.models import IPAddress from ipam.models import IPAddress
@ -210,7 +209,7 @@ class ReverseZone(models.Model):
def record_name(self, address: ip.IPAddress): def record_name(self, address: ip.IPAddress):
record_name = self.name record_name = self.name
if self.prefix.version == 4: if IPNetwork(self.prefix).version == 4:
for pos, octet in enumerate(address.words): for pos, octet in enumerate(address.words):
if (pos + 1) * 8 <= self.prefix.prefixlen: if (pos + 1) * 8 <= self.prefix.prefixlen:
continue continue
@ -227,7 +226,7 @@ class ReverseZone(models.Model):
return record_name return record_name
def clean(self): def clean(self):
if self.prefix.version == 4: if isinstance(self.prefix, IPNetwork) and self.prefix.version == 4:
if self.prefix.prefixlen not in [0, 8, 16, 24] and not self.name: if self.prefix.prefixlen not in [0, 8, 16, 24] and not self.name:
raise ValidationError({ raise ValidationError({
'name': _('Required when prefix length is not 0, 8, 16 or 24'), 'name': _('Required when prefix length is not 0, 8, 16 or 24'),
@ -240,7 +239,8 @@ class ReverseZone(models.Model):
break break
self.name = f'{octet}.{self.name}' self.name = f'{octet}.{self.name}'
else:
elif isinstance(self.prefix, IPNetwork) and self.prefix.version == 6:
if self.prefix.prefixlen % 4 != 0 and not self.name: if self.prefix.prefixlen % 4 != 0 and not self.name:
raise ValidationError({ raise ValidationError({
'name': _('Required when prefix length is not a nibble boundary'), 'name': _('Required when prefix length is not a nibble boundary'),