Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[admin] Fixed FileNotFoundError #140 #180

20 changes: 19 additions & 1 deletion openwisp_firmware_upgrader/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
from datetime import timedelta

import reversion
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just delete the object here, add a message for the user with the message framework and redirect to the admin list page?

Keep in mind this is an exceptional case which happens only if someone messes with the filesystem.

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):
Expand Down
6 changes: 6 additions & 0 deletions openwisp_firmware_upgrader/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down