From 00a7fe9734dd2ee9fe9248b74b3a2ca3c7154557 Mon Sep 17 00:00:00 2001 From: Adrienne Stilp Date: Thu, 21 Mar 2024 17:00:24 -0700 Subject: [PATCH] Add a constance setting to show a site announcement Add the ANNOUNCEMENT_TEXT setting to constance with the default as a blank string: "". If the setting is set to a non-blank string, then show an alert with the announcement. Otherwise, do not show any alert. Add tests. --- config/settings/base.py | 4 +++- primed/primed_anvil/tests/test_views.py | 19 +++++++++++++++++++ primed/templates/base.html | 25 ++++++++++++++++--------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 2f408147..61828a23 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -207,6 +207,7 @@ "django.template.context_processors.static", "django.template.context_processors.tz", "django.contrib.messages.context_processors.messages", + "constance.context_processors.config", "primed.utils.context_processors.settings_context", ], }, @@ -373,8 +374,9 @@ # django-constance # ------------------------------------------------------------------------------ CONSTANCE_CONFIG = { - "SITE_ANNOUNCEMENT": ("", "Site-wide announcement message", str), + "ANNOUNCEMENT_TEXT": ("", "Site-wide announcement message", str), } + CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend" CONSTANCE_IGNORE_ADMIN_VERSION_CHECK = True # CONSTANCE_DATABASE_CACHE_BACKEND = "default" diff --git a/primed/primed_anvil/tests/test_views.py b/primed/primed_anvil/tests/test_views.py index d7ff92fe..9e1b079f 100644 --- a/primed/primed_anvil/tests/test_views.py +++ b/primed/primed_anvil/tests/test_views.py @@ -3,6 +3,7 @@ from anvil_consortium_manager import models as acm_models from anvil_consortium_manager.tests.factories import AccountFactory from anvil_consortium_manager.views import AccountList +from constance.test import override_config from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission @@ -169,6 +170,24 @@ def test_view_links(self): response, '"{}"'.format(reverse("anvil_consortium_manager:index")) ) + def test_site_announcement_no_text(self): + self.client.force_login(self.user) + response = self.client.get(self.get_url()) + self.assertNotContains(response, """id="alert-announcement""") + + @override_config(ANNOUNCEMENT_TEXT="This is a test announcement") + def test_site_announcement_text(self): + self.client.force_login(self.user) + response = self.client.get(self.get_url()) + self.assertContains(response, """id="alert-announcement""") + self.assertContains(response, "This is a test announcement") + + @override_config(ANNOUNCEMENT_TEXT="This is a test announcement") + def test_site_announcement_text_unauthenticated_user(self): + response = self.client.get(self.get_url(), follow=True) + self.assertContains(response, """id="alert-announcement""") + self.assertContains(response, "This is a test announcement") + class StudyDetailTest(TestCase): """Tests for the StudyDetail view.""" diff --git a/primed/templates/base.html b/primed/templates/base.html index 8b2cb7a0..1789536c 100644 --- a/primed/templates/base.html +++ b/primed/templates/base.html @@ -111,18 +111,25 @@
- {% if request.user.is_authenticated and not request.user.account %} - + {% if config.ANNOUNCEMENT_TEXT %} + + {% endif %} + + {% if request.user.is_authenticated and not request.user.account %} + {% endif %} {% if messages %} - {% for message in messages %} - - {% endfor %} -{% endif %} + {% for message in messages %} + + {% endfor %} + {% endif %} {% block extra_navbar %} {% endblock %}