Skip to content

Commit

Permalink
[chores] Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Sep 25, 2024
1 parent 0613299 commit 3dad010
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions openwisp_controller/subnet_division/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def rule_class(self):
def clean(self):
super().clean()
self._validate_label()
self._validate_master_subnet_validity()
self._validate_master_subnet_consistency()
self._validate_ip_address_consistency()
if not self._state.adding:
Expand All @@ -86,6 +87,10 @@ def _validate_label(self):
}
)

def _validate_master_subnet_validity(self):
if not self.master_subnet.subnet:
raise ValidationError({'master_subnet': _('Invalid master subnet.')})

def _validate_existing_fields(self):
db_instance = self._meta.model.objects.get(id=self.id)
# The size field should not be changed
Expand All @@ -103,15 +108,11 @@ def _validate_existing_fields(self):
)

def _validate_master_subnet_consistency(self):
# Try to retrieve and validate master_subnet
master_subnet = self.master_subnet.subnet
if not master_subnet:
raise ValidationError(
{'master_subnet': _('Master subnet is required and cannot be empty.')}
)
# Try to generate subnets and validate size
try:
next(master_subnet.subnets(new_prefix=self.size))
# if size is not consistent, raise error
except ValueError:
raise ValidationError(
{
Expand All @@ -122,7 +123,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

# Ensure that master subnet can accommodate
# the required number of generated subnets
available = 2 ** (self.size - master_subnet.prefixlen)
Expand All @@ -139,7 +139,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

# Validate organization of master subnet
if (
self.master_subnet.organization is not None
Expand Down
2 changes: 1 addition & 1 deletion openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def test_invalid_master_subnet(self):
rule.full_clean()
e = cm.exception
self.assertIn(
'Master subnet must be a valid subnet.',
'Invalid master subnet.',
e.message_dict.get('master_subnet', []),
)

Expand Down

0 comments on commit 3dad010

Please sign in to comment.