From 765111fee884c9e3f4beecf57f183230b84dc9c1 Mon Sep 17 00:00:00 2001
From: Thorge Petersen <petersen@rz.uni-kiel.de>
Date: Mon, 11 Apr 2022 11:09:54 +0200
Subject: [PATCH] Return 404 for missing collections

---
 ckanext/odsh/collection/controller.py |  4 +++-
 ckanext/odsh/collection/helpers.py    | 17 ++++++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/ckanext/odsh/collection/controller.py b/ckanext/odsh/collection/controller.py
index 79b669af..86141f3c 100644
--- a/ckanext/odsh/collection/controller.py
+++ b/ckanext/odsh/collection/controller.py
@@ -9,6 +9,8 @@ class LatestDatasetController(PackageController):
     
     def latest_dataset(self, id):
         latest_dataset= get_latest_dataset(id)
+        if latest_dataset is None:
+            toolkit.abort(404)
         toolkit.redirect_to(controller='package', action='read', id=latest_dataset)
 
 class LatestRecourcesController(PackageController):
@@ -35,4 +37,4 @@ class LatestRecourcesController(PackageController):
                                     filename=pre_resource_url,
                                     qualified = True)
             toolkit.redirect_to(url_resource)
-        toolkit.abort(404)
\ No newline at end of file
+        toolkit.abort(404)
diff --git a/ckanext/odsh/collection/helpers.py b/ckanext/odsh/collection/helpers.py
index f4daaafe..82edf228 100644
--- a/ckanext/odsh/collection/helpers.py
+++ b/ckanext/odsh/collection/helpers.py
@@ -10,13 +10,16 @@ def get_collection(dataset_dict):
     collection_id = get_collection_id(dataset_dict)
     if collection_id:
         return get_collection_info(collection_id, dataset_dict)
-    
     return None
 
 
 def get_collection_info(collection_id, dataset_dict=None):
     collection_dict = get_package_dict(collection_id)
+    if not collection_dict:
+        return None
     dataset_names = get_dataset_names(collection_dict)
+    if not dataset_names:
+        return None
     datasets_in_collection = get_datasets_from_solr(dataset_names)
     collection_info = gather_collection_info(collection_dict, datasets_in_collection, dataset_dict)
     return collection_info
@@ -41,13 +44,15 @@ def get_package_dict(name):
 
 
 def get_dataset_names(collection_dict):
+    if not collection_dict:
+        return []
     collection_dict = get_package_dict(collection_dict.get('id')) # needed to get full package_dict
     if collection_dict:
-       relationships_collection = collection_dict.get('relationships')
-       names_collection_members = [relationship.get('object') for relationship in relationships_collection]
-       return names_collection_members
+        relationships_collection = collection_dict.get('relationships')
+        names_collection_members = [relationship.get('object') for relationship in relationships_collection]
+        return names_collection_members
     else:
-       return []
+        return []
 
 
 def get_datasets_from_solr(dataset_names):
@@ -159,6 +164,8 @@ def url_last_member(name_collection):
 
 def get_latest_dataset(collection_name):
     collection_info = get_collection_info(collection_name)
+    if not collection_info:
+        return None
     latest_name = collection_info['last_member']['name']
     return latest_name
 
-- 
GitLab