diff --git a/hc/api/transports.py b/hc/api/transports.py index 41e54ced..b08d52c4 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -3,12 +3,17 @@ from django.template.loader import render_to_string from django.utils import timezone import json import requests -import apprise from urllib.parse import quote, urlencode from hc.accounts.models import Profile from hc.lib import emails +try: + import apprise +except ImportError: + # Enforce + settings.APPRISE_ENABLED = False + def tmpl(template_name, **ctx): template_path = "integrations/%s" % template_name @@ -465,6 +470,11 @@ class Trello(HttpTransport): class Apprise(HttpTransport): def notify(self, check): + + if not settings.APPRISE_ENABLED: + # Not supported and/or enabled + return "Apprise is disabled and/or not installed." + a = apprise.Apprise() title = tmpl("apprise_title.html", check=check) body = tmpl("apprise_description.html", check=check) diff --git a/hc/front/tests/test_add_apprise.py b/hc/front/tests/test_add_apprise.py index cd1cb30b..cc46383a 100644 --- a/hc/front/tests/test_add_apprise.py +++ b/hc/front/tests/test_add_apprise.py @@ -1,8 +1,10 @@ from hc.api.models import Channel from hc.test import BaseTestCase +from django.test.utils import override_settings -class AddSlackTestCase(BaseTestCase): +@override_settings(APPRISE_ENABLED=True) +class AddAppriseTestCase(BaseTestCase): def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") r = self.client.get("/integrations/add_apprise/") @@ -19,3 +21,9 @@ class AddSlackTestCase(BaseTestCase): self.assertEqual(c.kind, "apprise") self.assertEqual(c.value, "json://example.org") self.assertEqual(c.project, self.project) + + @override_settings(APPRISE_ENABLED=False) + def test_it_requires_client_id(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/integrations/add_apprise/") + self.assertEqual(r.status_code, 404) diff --git a/hc/front/views.py b/hc/front/views.py index c02025d1..e391f875 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -237,6 +237,7 @@ def index(request): "enable_pd": settings.PD_VENDOR_KEY is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None, + "enable_apprise": settings.APPRISE_ENABLED is True, "registration_open": settings.REGISTRATION_OPEN, } @@ -611,6 +612,7 @@ def channels(request): "enable_pd": settings.PD_VENDOR_KEY is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None, + "enable_apprise": settings.APPRISE_ENABLED is True, "use_payments": settings.USE_PAYMENTS, } @@ -1328,6 +1330,9 @@ def add_matrix(request): @login_required def add_apprise(request): + if not settings.APPRISE_ENABLED: + raise Http404("apprise integration is not available") + if request.method == "POST": form = AddAppriseForm(request.POST) if form.is_valid(): diff --git a/hc/settings.py b/hc/settings.py index 6f31a3bf..6fcc5c99 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -204,6 +204,9 @@ MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER") MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") MATRIX_ACCESS_TOKEN = os.getenv("MATRIX_ACCESS_TOKEN") +# Apprise +APPRISE_ENABLED = envbool("APPRISE_ENABLED", "False") + if os.path.exists(os.path.join(BASE_DIR, "hc/local_settings.py")): from .local_settings import * diff --git a/requirements.txt b/requirements.txt index 027adb1a..88ba406c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,3 @@ django_compressor==2.2 psycopg2==2.7.5 pytz==2019.1 requests==2.22.0 -apprise==0.7.9 diff --git a/templates/front/channels.html b/templates/front/channels.html index 5f398e6f..27aebc91 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -213,6 +213,7 @@ Add Integration + {% if enable_apprise %}