From 40dfa6d36fae873bd8a49e8a0943be64ef4d2196 Mon Sep 17 00:00:00 2001
From: Frederick Muriuki Muriithi
Date: Mon, 7 Oct 2024 16:43:53 -0500
Subject: Handle "No linked resource" error cleanly.

Display the page even if there is no linked resource, but keep the
sensitive information hidden in such cases.
---
 uploader/phenotypes/views.py | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/uploader/phenotypes/views.py b/uploader/phenotypes/views.py
index 312179e..ddf6908 100644
--- a/uploader/phenotypes/views.py
+++ b/uploader/phenotypes/views.py
@@ -1,6 +1,7 @@
 """Views handling ('classical') phenotypes."""
 from functools import wraps
 
+from requests.models import Response
 from MySQLdb.cursors import DictCursor
 from flask import (flash,
                    request,
@@ -197,6 +198,31 @@ def view_phenotype(# pylint: disable=[unused-argument]
         **kwargs
 ):
     """View an individual phenotype from the dataset."""
+    def __render__(privileges):
+        return render_template(
+            "phenotypes/view-phenotype.html",
+            species=species,
+            population=population,
+            dataset=dataset,
+            phenotype=phenotype_by_id(conn,
+                                      species["SpeciesId"],
+                                      population["Id"],
+                                      dataset["Id"],
+                                      xref_id),
+            privileges=(privileges
+                        ### For demo! Do not commit this part
+                            + ("group:resource:edit-resource",
+                               "group:resource:delete-resource",)
+                        ### END: For demo! Do not commit this part
+                            ),
+            activelink="view-phenotype")
+
+    def __fail__(error):
+        if isinstance(error, Response) and error.json() == "No linked resource!":
+            return __render__(tuple())
+        return make_either_error_handler(
+            "There was an error fetching the roles and privileges.")(error)
+
     with database_connection(app.config["SQL_URI"]) as conn:
         return oauth2_post(
             "/auth/resource/phenotypes/individual/linked-resource",
@@ -210,23 +236,7 @@ def view_phenotype(# pylint: disable=[unused-argument]
             lambda resource: tuple(
                 privilege["privilege_id"] for role in resource["roles"]
                 for privilege in role["privileges"])
-        ).then(
-            lambda privileges: render_template(
-                "phenotypes/view-phenotype.html",
-                species=species,
-                population=population,
-                dataset=dataset,
-                phenotype=phenotype_by_id(conn,
-                                          species["SpeciesId"],
-                                          population["Id"],
-                                          dataset["Id"],
-                                          xref_id),
-                privileges=privileges,
-                activelink="view-phenotype")
-        ).either(
-            make_either_error_handler(
-                "There was an error fetching the roles and privileges."),
-            lambda resp: resp)
+        ).then(__render__).either(__fail__, lambda resp: resp)
 
 
 @phenotypesbp.route(
-- 
cgit v1.2.3