From 97171f7865532c2985804851f7fdec21710fad59 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Aug 25 2017 08:52:48 +0000
Subject: [PATCH 1/2] Filter out widgets that have been disabled


---

diff --git a/check-cache-coverage.py b/check-cache-coverage.py
index 4e623cc..f79f4c1 100755
--- a/check-cache-coverage.py
+++ b/check-cache-coverage.py
@@ -29,6 +29,8 @@ session = hubs.app.session
 hubs.widgets.registry.register_list(hubs.app.app.config["WIDGETS"])
 
 for w_instance in session.query(hubs.models.Widget).all():
+    if not w_instance.enabled:
+        continue
     for fn_name, fn_class in w_instance.module.get_cached_functions().items():
         if fn_class(w_instance).is_cached():
             full += 1
diff --git a/hubs/backend/triage.py b/hubs/backend/triage.py
index f59769b..b4bb412 100755
--- a/hubs/backend/triage.py
+++ b/hubs/backend/triage.py
@@ -91,6 +91,8 @@ def get_widgets():
     # Get a list of all our widgets:
     log.debug("Querying for all widgets.")
     widgets = session.query(hubs.models.Widget).all()
+    # Filter out widgets that have been disabled.
+    widgets = [w for w in widgets if w.enabled]
     # Randomize so that all the triage daemons work on widgets in different
     # orders.  This should hopefully prevent cache thrashing.
     log.debug("Randomizing list of all widgets.")
diff --git a/hubs/models.py b/hubs/models.py
index f894c22..eebd178 100644
--- a/hubs/models.py
+++ b/hubs/models.py
@@ -456,6 +456,10 @@ class Widget(ObjectAuthzMixin, BASE):
     def edit_url(self):
         return self.module.get_edit_url(self.hub, self)
 
+    @property
+    def enabled(self):
+        return self.plugin in hubs.widgets.registry
+
 
 class User(BASE):
     __tablename__ = 'users'
diff --git a/smart_cache_invalidator.py b/smart_cache_invalidator.py
index 75b4487..b74fdad 100755
--- a/smart_cache_invalidator.py
+++ b/smart_cache_invalidator.py
@@ -27,6 +27,8 @@ hubs.widgets.registry.register_list(hubs.app.app.config["WIDGETS"])
 def do_list(args):
     ''' List the different widget for which there is data cached. '''
     for w_instance in session.query(hubs.models.Widget).all():
+        if not w_instance.enabled:
+            continue
         widget = w_instance.module
         for fn_name, fn_class in widget.get_cached_functions().items():
             if fn_class(w_instance).is_cached():

From 6aa5e8f3abbf1b926fbfab8f8795dc213391dd19 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Aug 25 2017 08:52:48 +0000
Subject: [PATCH 2/2] Add a couple unit tests


---

diff --git a/hubs/tests/backend/__init__.py b/hubs/tests/backend/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/hubs/tests/backend/__init__.py
diff --git a/hubs/tests/backend/test_triage.py b/hubs/tests/backend/test_triage.py
new file mode 100644
index 0000000..10d0794
--- /dev/null
+++ b/hubs/tests/backend/test_triage.py
@@ -0,0 +1,28 @@
+from __future__ import unicode_literals
+
+from hubs.app import app
+from hubs.backend import triage
+from hubs.models import Hub, Widget
+from hubs.tests import APPTest
+
+
+class TriageTest(APPTest):
+
+    def setUp(self):
+        super(TriageTest, self).setUp()
+        # Set the session
+        triage.session = self.session
+
+
+    def test_get_widgets(self):
+        # Get all widgets except those whose module isn't installed.
+        hub = Hub.by_name('ralph')
+        widget = Widget(
+            plugin='non-existant',
+            index=500,
+            _config="{}",
+            )
+        hub.widgets.append(widget)
+        module_names = [w.plugin for w in triage.get_widgets()]
+        self.assertNotIn("non-existant", module_names)
+        self.assertEqual(len(module_names), 59)
diff --git a/hubs/tests/test_models.py b/hubs/tests/test_models.py
index 175caa1..be7be21 100644
--- a/hubs/tests/test_models.py
+++ b/hubs/tests/test_models.py
@@ -229,3 +229,9 @@ class ModelTest(hubs.tests.APPTest):
         widget.visibility = "restricted"
         self.assertEqual(
             widget._get_auth_permission_name("view"), "widget.restricted.view")
+
+    def test_widget_enabled(self):
+        hub = hubs.models.Hub.get("ralph")
+        widget = hubs.models.Widget(hub=hub, plugin="does-not-exist")
+        self.session.add(widget)
+        self.assertFalse(widget.enabled)

