openwisp / openwisp-controller

Network and WiFi controller: provisioning, configuration management and updates, (pull via openwisp-config or push via SSH), x509 PKI management and more. Mainly OpenWRT, but designed to work also on other systems.
https://openwisp.io/docs/dev/controller/
Other
550 stars 173 forks source link

[bug] Subnet division rule validation fails with uncaught exception if subnet is left empty #866

Open nemesifier opened 3 months ago

nemesifier commented 3 months ago

Try creating a subnet and a subnet division rule in /admin/openwisp_ipam/subnet/add/. Leave the mandatory "subnet" field empty but try creating the subnet division rule anyway.

Expected result: a validation error is shown in the UI.

Actual result: 500 internal server error.

Stacktrace:

AttributeError: 'NoneType' object has no attribute 'subnets'
  File "django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/contrib/admin/options.py", line 688, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "django/utils/decorators.py", line 134, in _wrapper_view
    response = view_func(request, *args, **kwargs)
  File "django/views/decorators/cache.py", line 62, in _wrapper_view_func
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "reversion/admin.py", line 158, in add_view
    return super().add_view(request, form_url, extra_context)
  File "django/contrib/admin/options.py", line 1886, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "django/utils/decorators.py", line 134, in _wrapper_view
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/options.py", line 1747, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "django/contrib/admin/options.py", line 1797, in _changeform_view
    if all_valid(formsets) and form_validated:
  File "__init__.py", line 89, in wrapper
    return func(original_callable, *args, **kwargs)
  File "__init__.py", line 155, in all_valid
    if not original_all_valid(formsets):
  File "django/forms/formsets.py", line 579, in all_valid
    return all([formset.is_valid() for formset in formsets])
  File "django/forms/formsets.py", line 579, in <listcomp>
    return all([formset.is_valid() for formset in formsets])
  File "django/forms/formsets.py", line 384, in is_valid
    self.errors
  File "django/forms/formsets.py", line 366, in errors
    self.full_clean()
  File "django/forms/formsets.py", line 429, in full_clean
    form_errors = form.errors
  File "django/forms/forms.py", line 196, in errors
    self.full_clean()
  File "django/forms/forms.py", line 435, in full_clean
    self._post_clean()
  File "django/forms/models.py", line 486, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "django/db/models/base.py", line 1477, in full_clean
    self.clean()
  File "openwisp_controller/subnet_division/base/models.py", line 74, in clean
    self._validate_master_subnet_consistency()
  File "openwisp_controller/subnet_division/base/models.py", line 109, in _validate_master_subnet_consistency
    next(master_subnet.subnets(new_prefix=self.size))