Skip to content

Commit ee88b78

Browse files
committed
[fix] Handle empty form submission in device add
Fixes #1057 Fixed MultiValueDictKeyError when submitting empty device form with configuration inline. Changed direct dictionary access to use .get() method with defaults to allow proper validation.
1 parent 9dd8f90 commit ee88b78

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

openwisp_controller/config/admin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@ def get_temp_model_instance(self, **options):
352352
config_model = self.Meta.model
353353
instance = config_model(**options)
354354
device_model = config_model.device.field.related_model
355-
org = Organization.objects.get(pk=self.data["organization"])
355+
org_id = self.data.get("organization")
356+
org = Organization.objects.get(pk=org_id) if org_id else None
356357
instance.device = device_model(
357-
name=self.data["name"],
358-
mac_address=self.data["mac_address"],
358+
name=self.data.get("name", ""),
359+
mac_address=self.data.get("mac_address", ""),
359360
organization=org,
360361
)
361362
return instance

openwisp_controller/config/tests/test_admin.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,43 @@ def test_templates_fetch_queries_10(self):
22792279
config = self._create_config(organization=self._get_org())
22802280
self._verify_template_queries(config, 10)
22812281

2282+
def test_empty_device_form_with_config_inline(self):
2283+
"""
2284+
Test for issue #1057: MultiValueDictKeyError when submitting
2285+
empty device form with configuration inline added
2286+
"""
2287+
org = self._get_org()
2288+
path = reverse(f"admin:{self.app_label}_device_add")
2289+
# Submit empty form with config inline
2290+
params = {
2291+
"name": "",
2292+
"mac_address": "",
2293+
"organization": "",
2294+
"config-0-backend": "netjsonconfig.OpenWrt",
2295+
"config-0-templates": "",
2296+
"config-0-config": json.dumps({}),
2297+
"config-0-context": "",
2298+
"config-TOTAL_FORMS": 1,
2299+
"config-INITIAL_FORMS": 0,
2300+
"config-MIN_NUM_FORMS": 0,
2301+
"config-MAX_NUM_FORMS": 1,
2302+
"deviceconnection_set-TOTAL_FORMS": 0,
2303+
"deviceconnection_set-INITIAL_FORMS": 0,
2304+
"deviceconnection_set-MIN_NUM_FORMS": 0,
2305+
"deviceconnection_set-MAX_NUM_FORMS": 1000,
2306+
"command_set-TOTAL_FORMS": 0,
2307+
"command_set-INITIAL_FORMS": 0,
2308+
"command_set-MIN_NUM_FORMS": 0,
2309+
"command_set-MAX_NUM_FORMS": 1000,
2310+
}
2311+
# Should not raise MultiValueDictKeyError
2312+
response = self.client.post(path, params)
2313+
# Should show validation errors instead
2314+
self.assertEqual(response.status_code, 200)
2315+
self.assertContains(response, "errorlist")
2316+
# Verify no device was created
2317+
self.assertEqual(Device.objects.count(), 0)
2318+
22822319

22832320
class TestTransactionAdmin(
22842321
CreateConfigTemplateMixin,

0 commit comments

Comments
 (0)