Skip to content

Commit

Permalink
[#1508] fix behavior of 'my messages'
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Jun 13, 2023
1 parent 6be1f1c commit cd42290
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 19 deletions.
45 changes: 34 additions & 11 deletions src/open_inwoner/accounts/tests/test_contact_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
40 changes: 37 additions & 3 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions src/open_inwoner/accounts/views/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
2 changes: 2 additions & 0 deletions src/open_inwoner/accounts/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,6 +94,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

Expand Down
45 changes: 45 additions & 0 deletions src/open_inwoner/cms/utils/page_display.py
Original file line number Diff line number Diff line change
@@ -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")
15 changes: 11 additions & 4 deletions src/open_inwoner/templates/pages/profile/contacts/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ <h2 class="h2">{% trans "U bent toegevoegd als contactpersoon" %}</h2>
<td class="table__item">{{invite.invitee_email}}</td>
<td class="table__item"></td>
<td class="table__item">{% icon "close" extra_classes="icon icon--danger" %}</td>
<td class="table__item">{% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %}</td>
<td class="table__item">{% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %}<td>
{% if inbox_page_is_published %}
<td class="table__item">{% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %}</td>
{% endif %}
<td class="table__item">{% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %}<td>
</tr>
{% endfor %}

Expand All @@ -116,7 +118,9 @@ <h2 class="h2">{% trans "U bent toegevoegd als contactpersoon" %}</h2>
<td class="table__item">{{contact.email}}</td>
<td class="table__item"></td>
<td class="table__item">{% icon "close" extra_classes="icon icon--danger" %}</td>
<td class="table__item">{% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %}</td>
{% if inbox_page_is_published %}
<td class="table__item">{% button text=_('Stuur bericht') icon="message" icon_position="before" icon_outlined=True transparent=True disabled=True %}</td>
{% endif %}
<td class="table__item">{% button icon="settings" icon_position="before" text="" hide_text=True transparent=True disabled=True %}<td>
</tr>
{% endfor %}
Expand All @@ -126,10 +130,13 @@ <h2 class="h2">{% trans "U bent toegevoegd als contactpersoon" %}</h2>
<tr>
<th class="table__header">{{ contact.get_full_name }}</th>
<td class="table__item">{% if contact.contact_type == "contact" %}Contactpersoon{% elif contact.contact_type == "begeleider" %}Begeleider{% elif contact.contact_type == "organization" %}Organisatie{% endif %}</td>

<td class="table__item">{{ contact.get_contact_email|default:"" }}</td>
<td class="table__item">{{ contact.phonenumber }}</td>
<td class="table__item">{% if contact.is_not_active %}{% icon "close" extra_classes="icon icon--danger" %}{% else %}{% icon "check" %}{% endif %}</td>
<td class="table__item">{% 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 %}</td>
{% if inbox_page_is_published %}
<td class="table__item">{% 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 %}</td>
{% endif %}
<td class="table__item">
{% dropdown icon="settings" disabled=contact.is_not_active %}
<div class="dropdown__item">
Expand Down
4 changes: 3 additions & 1 deletion src/open_inwoner/templates/pages/profile/me.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ <h1 class="h1" id="overview">{% trans "Persoonlijk overzicht" %}</h1>
{% if mentor_contacts %}
<div class="tabled__item">{{ mentor_contacts|join:", " }}</div>
{% url 'profile:contact_list' as mentor_url %}
<div class="tabled__item tabled__item--force-right tabled__item--mobile-big">{% link href=mentor_url|add:"?type=begeleider" text="Stuur een bericht" icon="arrow_forward" icon_position="after" primary=True %}</div>
{% if inbox_page_is_published %}
<div class="tabled__item tabled__item--force-right tabled__item--mobile-big">{% link href=mentor_url|add:"?type=begeleider" text="Stuur een bericht" icon="arrow_forward" icon_position="after" primary=True %}</div>
{% endif %}
{% else %}
<div class="tabled__item">{% trans "U heeft (nog) geen gemeentelijke begeleider." %}</div>
<div class="tabled__item tabled__item--force-right tabled__item--mobile-big"></div>
Expand Down

0 comments on commit cd42290

Please sign in to comment.