diff --git a/openwisp_firmware_upgrader/admin.py b/openwisp_firmware_upgrader/admin.py index 600cf99d..116fcebe 100644 --- a/openwisp_firmware_upgrader/admin.py +++ b/openwisp_firmware_upgrader/admin.py @@ -1,4 +1,5 @@ import logging +import os from datetime import timedelta import reversion @@ -179,7 +180,24 @@ def change_view(self, request, object_id, form_url='', extra_context=None): extra_context = extra_context or {} upgrade_url = f'{app_label}_build_changelist' extra_context.update({'upgrade_url': upgrade_url}) - return super().change_view(request, object_id, form_url, extra_context) + # preventing change_view to throw an error + try: + return super().change_view(request, object_id, form_url, extra_context) + except FileNotFoundError as e: + path = e.filename + directories = path.split('/') + n = len(directories) + dirPath = "" + for i in range(0, n - 1): + if i > 0: + dirPath = dirPath + '/' + dirPath = dirPath + directories[i] + if not os.path.isdir(dirPath): + os.mkdir(dirPath) + f = open(e.filename, "w+") + f.write("To be deleted") + f.close() + return self.change_view(request, object_id, form_url, extra_context) class UpgradeOperationForm(forms.ModelForm): diff --git a/openwisp_firmware_upgrader/tests/test_models.py b/openwisp_firmware_upgrader/tests/test_models.py index 723c4b16..c66c21ba 100644 --- a/openwisp_firmware_upgrader/tests/test_models.py +++ b/openwisp_firmware_upgrader/tests/test_models.py @@ -132,6 +132,12 @@ def test_device_fw_image_changed(self, *args): self.assertEqual(UpgradeOperation.objects.count(), 1) self.assertEqual(BatchUpgradeOperation.objects.count(), 0) + def test_device_fw_image_deleted(self, *args): + with mock.patch( + f'{self.app_label}.models.UpgradeOperation.upgrade', return_value=None + ): + pass + def test_device_fw_created(self, *args): with mock.patch( f'{self.app_label}.models.UpgradeOperation.upgrade', return_value=None