Skip to content

Commit

Permalink
Add a constance setting to show a site announcement
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amstilp committed Mar 22, 2024
1 parent 0258914 commit 00a7fe9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
Expand Down Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions primed/primed_anvil/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
25 changes: 16 additions & 9 deletions primed/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,25 @@
<main class="flex-shrink-0">
<div class="container">

{% if request.user.is_authenticated and not request.user.account %}
<div class="alert alert-warning" role="alert">
<i class="bi bi-exclamation-triangle-fill me-1"></i>
You must <a href="{% url 'anvil_consortium_manager:accounts:link' %}">link your AnVIL account</a> before you can access any data on AnVIL.
</div>
{% if config.ANNOUNCEMENT_TEXT %}
<div class="alert alert-info" role="alert" id="alert-announcement">
<i class="bi bi-megaphone-fill me-1"></i>
{{ config.ANNOUNCEMENT_TEXT }}
</div>
{% endif %}

{% if request.user.is_authenticated and not request.user.account %}
<div class="alert alert-warning" role="alert">
<i class="bi bi-exclamation-triangle-fill me-1"></i>
You must <a href="{% url 'anvil_consortium_manager:accounts:link' %}">link your AnVIL account</a> before you can access any data on AnVIL.
</div>
{% endif %}

{% if messages %}
{% for message in messages %}
<div class="alert {% if message.tags %}alert-{{ message.tags }}{% endif %} alert-dismissible fade show" role="alert">{{ message }}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>
{% endfor %}
{% endif %}
{% for message in messages %}
<div class="alert {% if message.tags %}alert-{{ message.tags }}{% endif %} alert-dismissible fade show" role="alert">{{ message }}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>
{% endfor %}
{% endif %}

{% block extra_navbar %}
{% endblock %}
Expand Down

0 comments on commit 00a7fe9

Please sign in to comment.