diff --git a/src/open_inwoner/accounts/tests/test_contact_views.py b/src/open_inwoner/accounts/tests/test_contact_views.py index 7d39a46d3c..c99141749b 100644 --- a/src/open_inwoner/accounts/tests/test_contact_views.py +++ b/src/open_inwoner/accounts/tests/test_contact_views.py @@ -6,8 +6,8 @@ from django.urls import reverse from django.utils.translation import ugettext_lazy as _ +from cms import api from django_webtest import WebTest -from PIL import Image from open_inwoner.accounts.models import User from open_inwoner.utils.tests.helpers import create_image_bytes @@ -85,10 +85,40 @@ def test_contact_filter_without_any_contacts(self): ), ) - def test_contact_list_show_link_to_messages(self): - message_link = reverse("inbox:index", kwargs={"uuid": self.contact.uuid}) + def test_messages_enabled_disabled(self): + """Assert that `Stuur Bericht` is displayed if and only if the message page is published""" + + # case 1: no message page response = self.app.get(self.list_url, user=self.user) - self.assertContains(response, message_link) + + # self.assertNotIn(_("Stuur bericht"), response) + self.assertNotContains(response, _("Stuur bericht")) + + # case 2: unpublished message page + page = api.create_page( + "Mijn Berichten", + "cms/fullwidth.html", + "nl", + slug="berichten", + ) + page.application_namespace = "inbox" + page.save() + + response = self.app.get(self.list_url, user=self.user) + + self.assertNotContains(response, _("Stuur bericht")) + + # case 3: published message page + page.publish("nl") + page.save() + + response = self.app.get(self.list_url, user=self.user) + + icons = response.pyquery(".material-icons-outlined") + message_icon = next((icon for icon in icons if icon.text == "message"), None) + message_button_text = message_icon.tail.strip() + + self.assertEqual(_("Stuur bericht"), message_button_text) def test_contact_list_show_reversed(self): other_contact = UserFactory(first_name="reverse_contact_user_should_be_found") @@ -366,19 +396,12 @@ def test_accepted_contact_appears_in_both_contact_lists(self): self.assertContains(response, self.user.first_name) self.assertContains(response, self.user.last_name) - self.assertContains( - response, reverse("inbox:index", kwargs={"uuid": self.user.uuid}) - ) # Sender contact list page response = self.app.get(self.list_url, user=self.user) self.assertContains(response, existing_user.first_name) self.assertContains(response, existing_user.last_name) - self.assertContains( - response, - reverse("inbox:index", kwargs={"uuid": existing_user.uuid}), - ) def test_post_with_no_params_in_contact_approval_returns_bad_request(self): existing_user = UserFactory(email="ex@example.com") diff --git a/src/open_inwoner/accounts/tests/test_profile_views.py b/src/open_inwoner/accounts/tests/test_profile_views.py index c13f662501..bffa27c023 100644 --- a/src/open_inwoner/accounts/tests/test_profile_views.py +++ b/src/open_inwoner/accounts/tests/test_profile_views.py @@ -1,12 +1,10 @@ -import io - from django.test import override_settings from django.urls import reverse from django.utils.translation import ugettext_lazy as _ import requests_mock +from cms import api from django_webtest import WebTest -from PIL import Image from timeline_logger.models import TimelineLog from webtest import Upload @@ -191,6 +189,42 @@ def test_expected_message_is_shown_when_all_notifications_disabled(self): response = self.app.get(self.url, user=self.user) self.assertContains(response, _("You do not have any notifications enabled.")) + def test_messages_enabled_disabled(self): + """Assert that `Stuur een bericht` is displayed if and only if the message page is published""" + + begeleider = UserFactory(contact_type=ContactTypeChoices.begeleider) + self.user.user_contacts.add(begeleider) + + # case 1: no message page + response = self.app.get(self.url, user=self.user) + + self.assertNotContains(response, _("Stuur een bericht")) + + # case 2: unpublished message page + page = api.create_page( + "Mijn Berichten", + "cms/fullwidth.html", + "nl", + slug="berichten", + ) + page.application_namespace = "inbox" + page.save() + + response = self.app.get(self.url, user=self.user) + + self.assertNotContains(response, _("Stuur een bericht")) + + # case 3: published message page + page.publish("nl") + page.save() + + response = self.app.get(self.url, user=self.user) + + message_link = response.pyquery("[title='Stuur een bericht']") + link_text = message_link.find(".link__text").text + + self.assertEqual(link_text(), _("Stuur een bericht")) + @override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls") class EditProfileTests(WebTest): diff --git a/src/open_inwoner/accounts/views/contacts.py b/src/open_inwoner/accounts/views/contacts.py index 1aefd684b7..2741879f1c 100644 --- a/src/open_inwoner/accounts/views/contacts.py +++ b/src/open_inwoner/accounts/views/contacts.py @@ -11,6 +11,7 @@ from mail_editor.helpers import find_template from view_breadcrumbs import BaseBreadcrumbMixin +from open_inwoner.cms.utils.page_display import inbox_page_is_published from open_inwoner.utils.views import CommonPageMixin, LogMixin from ..forms import ContactCreateForm, ContactFilterForm @@ -48,6 +49,7 @@ def get_context_data(self, **kwargs): context["contacts_for_approval"] = user.get_contacts_for_approval() context["pending_invitations"] = user.get_pending_invitations() context["form"] = ContactFilterForm(data=self.request.GET) + context["inbox_page_is_published"] = inbox_page_is_published() return context diff --git a/src/open_inwoner/accounts/views/profile.py b/src/open_inwoner/accounts/views/profile.py index 4403827160..3c18dd829c 100644 --- a/src/open_inwoner/accounts/views/profile.py +++ b/src/open_inwoner/accounts/views/profile.py @@ -20,6 +20,7 @@ LoginTypeChoices, StatusChoices, ) +from open_inwoner.cms.utils.page_display import inbox_page_is_published from open_inwoner.haalcentraal.utils import fetch_brp_data from open_inwoner.questionnaire.models import QuestionnaireStep from open_inwoner.utils.mixins import ExportMixin @@ -97,6 +98,7 @@ def get_context_data(self, **kwargs): published=True ).exists() context["can_change_password"] = user.login_type != LoginTypeChoices.digid + context["inbox_page_is_published"] = inbox_page_is_published() return context diff --git a/src/open_inwoner/cms/utils/page_display.py b/src/open_inwoner/cms/utils/page_display.py new file mode 100644 index 0000000000..2a18b3abc5 --- /dev/null +++ b/src/open_inwoner/cms/utils/page_display.py @@ -0,0 +1,45 @@ +"""Utilities for determining whether CMS pages are published""" + + +from cms.models import Page + +from open_inwoner.cms.cases.cms_apps import CasesApphook +from open_inwoner.cms.collaborate.cms_apps import CollaborateApphook +from open_inwoner.cms.inbox.cms_apps import InboxApphook + +cms_apps = { + "inbox": InboxApphook, + "collaborate": CollaborateApphook, + "cases": CasesApphook, +} + + +def _is_published(page_name: str) -> bool: + """ + Determine whether the page associated with a specific CMS app is published + """ + page = Page.objects.filter( + application_namespace=cms_apps[page_name].app_name, publisher_is_draft=False + ).first() + return page and page.is_published(page.languages) + + +def inbox_page_is_published() -> bool: + """ + :returns: True if the inbox/message page is published, False otherwise + """ + return _is_published("inbox") + + +def case_page_is_published() -> bool: + """ + :returns: True if the case page is published, False otherwise + """ + return _is_published("cases") + + +def collaborate_page_is_published() -> bool: + """ + :returns: True if the collaborate page published, False otherwise + """ + return _is_published("collaborate") diff --git a/src/open_inwoner/templates/pages/profile/contacts/list.html b/src/open_inwoner/templates/pages/profile/contacts/list.html index 77325da0af..1fa1f9fe65 100644 --- a/src/open_inwoner/templates/pages/profile/contacts/list.html +++ b/src/open_inwoner/templates/pages/profile/contacts/list.html @@ -84,7 +84,7 @@

{% trans "U bent toegevoegd als contactpersoon" %}

{% trans "Soort contact" %} {% trans "E-mailadres" %} {% trans "Telefoonnummer" %} - {% trans "Actief" %}  +   @@ -103,8 +103,10 @@

{% trans "U bent toegevoegd als contactpersoon" %}

{{invite.invitee_email}} {% icon "close" extra_classes="icon icon--danger" %} - {% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %} - {% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %} + {% if inbox_page_is_published %} + {% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %} + {% endif %} + {% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %} {% endfor %} @@ -116,7 +118,9 @@

{% trans "U bent toegevoegd als contactpersoon" %}

{{contact.email}} {% icon "close" extra_classes="icon icon--danger" %} - {% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %} + {% if inbox_page_is_published %} + {% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %} + {% endif %} {% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %} {% endfor %} @@ -126,10 +130,13 @@

{% trans "U bent toegevoegd als contactpersoon" %}

{{ contact.get_full_name }} {% if contact.contact_type == "contact" %}Contactpersoon{% elif contact.contact_type == "begeleider" %}Begeleider{% elif contact.contact_type == "organization" %}Organisatie{% endif %} + {{ contact.get_contact_email|default:"" }} {{ contact.phonenumber }} {% if contact.is_not_active %}{% icon "close" extra_classes="icon icon--danger" %}{% else %}{% icon "check" %}{% endif %} - {% button text=_('Stuur bericht') icon="message" icon_position="before" href=contact.get_contact_message_url icon_outlined=True transparent=True disabled=contact.is_not_active %} + {% if inbox_page_is_published %} + {% button text=_('Stuur bericht') icon="message" icon_position="before" href=contact.get_contact_message_url icon_outlined=True transparent=True disabled=contact.is_not_active %} + {% endif %} {% dropdown icon="settings" disabled=contact.is_not_active %}