From 6ed81a49d1705610e13985e9ebc9a1ce7bd4800e Mon Sep 17 00:00:00 2001 From: Lova Andriarimalala <43842786+Xpirix@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:35:48 +0300 Subject: [PATCH] Handle missing resource files to prevent FileNotFoundError (#427) --- qgis-app/base/views/processing_view.py | 5 ++++- qgis-app/templates/base/detail.html | 8 +++++++- qgis-app/templates/base/list.html | 4 +++- qgis-app/templates/base/list_galery.html | 9 ++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/qgis-app/base/views/processing_view.py b/qgis-app/base/views/processing_view.py index 3b1352ff..442d9553 100644 --- a/qgis-app/base/views/processing_view.py +++ b/qgis-app/base/views/processing_view.py @@ -1,5 +1,5 @@ import logging - +from os.path import exists from base.forms.processing_forms import ResourceBaseReviewForm from base.license import zipped_with_license from django.conf import settings @@ -29,6 +29,7 @@ ) from django.views.generic.base import ContextMixin from django.utils.encoding import escape_uri_path +from django.http import Http404 GROUP_NAME = "Style Managers" @@ -468,6 +469,8 @@ class ResourceBaseDownload(ResourceBaseContextMixin, View): def get(self, request, *args, **kwargs): object = get_object_or_404(self.model, pk=self.kwargs["pk"]) + if not exists(object.file.path): + raise Http404 if not object.approved: if not check_resources_access(self.request.user, object): context = super(ResourceBaseDownload, self).get_context_data() diff --git a/qgis-app/templates/base/detail.html b/qgis-app/templates/base/detail.html index 745c58dd..d9571c23 100644 --- a/qgis-app/templates/base/detail.html +++ b/qgis-app/templates/base/detail.html @@ -19,9 +19,11 @@

{{ object_detail.name }}

+ {% if object_detail.thumbnail_image and object_detail.thumbnail_image.file %} {% thumbnail object_detail.thumbnail_image "420x420" format="PNG" as im %} {% trans {% endthumbnail %} + {% endif %} {% if is_3d_model %} {% include "base/includes/wavefront/detail_3dviewer.html" %} {% else %} @@ -34,7 +36,11 @@

{{ object_detail.name }}

{{ resource_name }} File
- Download + {% if object_detail.file and object_detail.file.file %} + Download + {% else %} + File missing: The resource file couldn't be located. + {% endif %}
diff --git a/qgis-app/templates/base/list.html b/qgis-app/templates/base/list.html index 529ac62d..c8fcd977 100644 --- a/qgis-app/templates/base/list.html +++ b/qgis-app/templates/base/list.html @@ -112,7 +112,9 @@

{% if title %}{{title}}{% else %}{% trans "All" %} {{ resource_name }}s{% en     {% endif %} - + {% if object.file and object.file.file %} + + {% endif %} {% endfor %} diff --git a/qgis-app/templates/base/list_galery.html b/qgis-app/templates/base/list_galery.html index 5f230973..9833b0d0 100644 --- a/qgis-app/templates/base/list_galery.html +++ b/qgis-app/templates/base/list_galery.html @@ -100,7 +100,14 @@

{% if title %}{{title}}{% else %}{% trans "All" %} {{ resource_name }}s{% en

- + {% if object.file and object.file.file %} + + + + {% endif %}

{{ object.name}}