[fix] Fixed MultiValueDictKeyError on empty device form submission#1173
Conversation
ee88b78 to
76cb583
Compare
| config = self._create_config(organization=self._get_org()) | ||
| self._verify_template_queries(config, 10) | ||
|
|
||
| def test_empty_device_form_with_config_inline(self): |
There was a problem hiding this comment.
Can you please try running this test without the fix and check if it fails or not?
Also I don't think it is necessary to mention the issue in the docstring, a simple description of what it does is enough, you can take other tests as reference
There was a problem hiding this comment.
Hi @DragnEmperor , Thanks for the feedback! I've updated the test to properly reproduce the bug by omitting the device fields from the POST data, which matches the actual scenario from the issue.
I tried running it locally but hit a Twisted import error with my Python version. However, the test logic is clear: without the fix, accessing self.data["organization"] on a missing key raises MultiValueDictKeyError, and with the fix using .get(), it's handled gracefully.
CI should catch it if there's any issue. Let me know if you need anything else changed.
Also updated the docstring as you suggested.
There was a problem hiding this comment.
Hey @DragnEmperor , I've fixed the CI issues from the previous run. The workflow needs approval to run the tests again. Could you approve it when you get a chance?
19170b9 to
21c1fb1
Compare
|
Hey @DragnEmperor , really sorry for the inconvenience, I had to fix the commit message format to match the project guidelines (added the [fix] prefix). Could you approve the workflow one more time? |
.github/workflows/ci.yml
Outdated
| if: ${{ failure() && steps.deps.conclusion == 'success' }} | ||
| run: | | ||
| if [ -f geckodriver.log ]; then | ||
| cat geckodriver.log | ||
| else | ||
| echo "geckodriver.log not found" | ||
| fi |
There was a problem hiding this comment.
why is this change required? IMO any unrelated changes should be taken up if there is a dedicated issue for the same. Else, would be difficult to track the changes.
There was a problem hiding this comment.
You're right, my bad! I added this while debugging some test failures but it's not really needed for this PR. Removed it in the latest commit. Thank you for addressing
Regarding the checks - all of them should pass except Python 3.9. This seems to be an existing issue on the main branch and not related to my changes. I encountered the same Python 3.9 failure in my previous PR as well, and it was also discussed in the community group.
I've opened PR #1174 to drop Python 3.9 support from openwisp-controller, which should resolve this CI issue for all pending PRs once merged.
There was a problem hiding this comment.
Hi @DragnEmperor , PR #1174 has been merged which drops Python 3.9 support. Could you re-run the checks on this PR? All tests should pass now. Thanks!
Fixes openwisp#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.
e0f61e4 to
d152703
Compare
d152703 to
94f9cd2
Compare
|
Hi @pandafy , @DragnEmperor , all checks are passing now, This PR is ready for review. |
Hi @Srinath0916 , I would suggest to have a look once again through all the changes done to ensure any unrelated changes are not there. I see some changes in workflow yaml file. |
|
Thanks @DragnEmperor ! I've reviewed all changes in this PR, The MultiValueDictKeyError fix includes: - config/admin.py (the main fix) - config/base/config.py (related changes) - config/tests/test_admin.py (test for the fix) The workflow yaml changes include: - Geckodriver logging removal (as you said) and Python 3.9 related changes from updating with master (PR #1174) Should I clean up the workflow file to keep only the geckodriver removal you asked for and remove the Python 3.9 changes? I want to make sure the tests still pass." |
@Srinath0916 I meant to keep the |
|
Thanks @DragnEmperor ! I've reverted ci.yml to match master exactly. Now the PR contains only the MultiValueDictKeyError fix files with no workflow changes. Ready for review! |
|
LGTM |
|
Thank you @DragnEmperor , Really appreciate your patience and detailed feedback throughout this process. |
|
Hi @pandafy , This PR has been approved, and all checks are passing. Could you help merge it when you get a chance? Thanks. |
| if not config: | ||
| # The request does not contain vaild data to create a temporary | ||
| # Device instance. Thus, we cannot validate the templates. | ||
| # The Device validation will be handled by DeviceAdmin. | ||
| # Therefore, we don't need to raise any error here. | ||
| return |
There was a problem hiding this comment.
We shall return early if the data does not contain organization.
There was a problem hiding this comment.
Thanks @pandafy , for the review and the improvement,
Really appreciate the feedback and approval.
| # Should show validation errors instead | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertContains(response, "errorlist") | ||
| # Verify no device was created |
There was a problem hiding this comment.
Let's avoid adding too many comments, it's not useful, I am going to remove some.
97b9ddd to
c0096f2
Compare
Checklist
Reference to Existing Issue
Closes #1057
Description of Changes
Fixed the MultiValueDictKeyError that happened when you click "Add another Configuration" on a new device and hit save without filling anything in.
The problem was in the ConfigForm.get_temp_model_instance() method - it was trying to access organization, name, and mac_address from the form data without checking if they exist first. Changed it to use .get() so it handles empty fields properly and shows validation errors instead of crashing.
Added a test case to verify the fix works.
Screenshot
See issue #1057 for the error screenshot. After the fix, the form now shows proper validation errors instead of crashing.