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

50 changes: 48 additions & 2 deletions 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 All @@ -7,7 +8,7 @@
from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.shortcuts import redirect
from django.shortcuts import HttpResponseRedirect, redirect
from django.template.response import TemplateResponse
from django.urls import resolve, reverse
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -84,6 +85,37 @@ def has_change_permission(self, request, obj=None):
return False
return True

def get_parent_object(self, request):
"""
method to get the instance from model' s parent class
in context with the ForeignKey relation
"""
resolved = resolve(request.path_info)
if resolved.kwargs:
return self.parent_model.objects.get(pk=resolved.kwargs["object_id"])
return None

def get_queryset(self, request):
"""
overriding queryset to remove any FirmwareImage instances,
where the image file has been manually deleted by the user
from the file system
"""
qs = super(FirmwareImageInline, self).get_queryset(request)
build = self.get_parent_object(request)
qs = qs.filter(build=build)
for imageObject in qs:
if imageObject.file is not None:
path = imageObject.file.path
if not os.path.isfile(path):
try:
type = imageObject.type
imageObject.delete()
logger.warning(f"Image object {type} was removed")
except Exception:
pass
return qs
Copy link
Member

Choose a reason for hiding this comment

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

Looping over individual FirmwareImage object will make system very slow. Instead of this, the issue recommended catching the exception when it occurs(on delete operation) and delete the related FirmwareImage.



class CategoryFilter(MultitenantOrgFilter):
multitenant_lookup = 'organization_id__in'
Expand Down Expand Up @@ -192,7 +224,21 @@ 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/exception
try:
return super().change_view(request, object_id, form_url, extra_context)
except Exception as e:
if type(e).__name__ == "FileNotFoundError":
self.message_user(
request, "Please reload the page", level=logging.ERROR
)
else:
self.message_user(
request,
"Image objects have been removed or form data didn't validate",
level=logging.ERROR,
)
return HttpResponseRedirect(request.path)
Copy link
Member

Choose a reason for hiding this comment

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

  1. Instead of showing this error to the user, log the error with logger.exception.
  2. Delete the FirmwareImage object for which the error was raised.
  3. Call the change_view method again (recursion)

We want to handle this case without making it obvious to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I understand



class UpgradeOperationForm(forms.ModelForm):
Expand Down