From 81d21a9b64349efc8c453055faf6531091ef08dc Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Mon, 30 Oct 2023 14:40:27 +0100
Subject: [PATCH] Handle empty list of available models

---
 spatial/templates/spatial/index.html |  9 +++++++--
 spatial/views.py                     | 15 ++++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/spatial/templates/spatial/index.html b/spatial/templates/spatial/index.html
index 0919a042..28b0ff43 100644
--- a/spatial/templates/spatial/index.html
+++ b/spatial/templates/spatial/index.html
@@ -28,14 +28,19 @@
 <h2>VIPS grid map</h2>
 <p>
     View results from VIPS models run on gridded weather data.
+    {% if available_grid_models|length > 0 %}
     <ul>
-    {% for model in available_grid_models %}
+        {% for model in available_grid_models %}
         <li><a href="gridmap/{{model.model_id}}">{{ model.model_name }}</a></li>
-    {% endfor %}
+        {% endfor %}
     </ul>
+    {% else %}
+    <p><em>No GRID models available</em></p>
+    {% endif %}
 </p>
 <h2>Other resources</h2>
 <ul>
     <li><a href="test/PSILARTEMP">Carrot rust fly map [TEST]</a></li>
 </ul>
 {% endblock %}
+
diff --git a/spatial/views.py b/spatial/views.py
index 5bfd2d33..ef0a2dec 100644
--- a/spatial/views.py
+++ b/spatial/views.py
@@ -4,7 +4,9 @@ from django.conf import settings
 
 
 def index(request):
-    context = {"available_grid_models": settings.AVAILABLE_GRID_MODELS}
+    context = {
+        "available_grid_models": settings.AVAILABLE_GRID_MODELS if hasattr(settings, "AVAILABLE_GRID_MODELS") else []
+        }
     return render(request, 'spatial/index.html', context)
 
 def psilartemp(request):
@@ -12,9 +14,12 @@ def psilartemp(request):
     return render(request, 'spatial/psilartemp.html', context)
 
 def gridmap(request, model_id):
-    for model in settings.AVAILABLE_GRID_MODELS:
-        if model_id == model["model_id"]:
-            context = {"model_id": model_id}
-            return render(request, 'spatial/gridmap.html', context)
+    try:
+        for model in settings.AVAILABLE_GRID_MODELS:
+            if model_id == model["model_id"]:
+                context = {"model_id": model_id}
+                return render(request, 'spatial/gridmap.html', context)
+    except AttributeError:
+        pass
     return index(request)
     
-- 
GitLab