Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1760] Implemented user feed #933

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
ZaakTypeStatusTypeConfig,
)
from open_inwoner.openzaak.utils import get_role_name_display, is_info_object_visible
from open_inwoner.userfeed import hooks
from open_inwoner.utils.time import has_new_elements
from open_inwoner.utils.translate import TranslationLookup
from open_inwoner.utils.views import CommonPageMixin, LogMixin
Expand Down Expand Up @@ -218,6 +219,9 @@ def get_context_data(self, **kwargs):
self.case, self.resulttype_config_mapping
)

hooks.case_status_seen(self.request.user, self.case)
hooks.case_documents_seen(self.request.user, self.case)

context["case"] = {
"id": str(self.case.uuid),
"identification": self.case.identification,
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/cms/plugins/cms_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .userfeed import UserFeedPlugin
from .videoplayer import VideoPlayerPlugin
26 changes: 26 additions & 0 deletions src/open_inwoner/cms/plugins/cms_plugins/userfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.utils.translation import gettext as _

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from open_inwoner.cms.plugins.models.userfeed import UserFeed
from open_inwoner.userfeed.feed import get_feed


@plugin_pool.register_plugin
class UserFeedPlugin(CMSPluginBase):
model = UserFeed
module = _("General")
name = _("User Feed")
render_template = "cms/plugins/userfeed/userfeed.html"

def render(self, context, instance, placeholder):
request = context["request"]
feed = get_feed(request.user, with_history=True)
context.update(
{
"instance": instance,
"userfeed": feed,
}
)
return context
36 changes: 36 additions & 0 deletions src/open_inwoner/cms/plugins/migrations/0002_userfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.2.23 on 2024-01-05 08:26

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("cms", "0022_auto_20180620_1551"),
("plugins", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="UserFeed",
fields=[
(
"cmsplugin_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
related_name="plugins_userfeed",
serialize=False,
to="cms.cmsplugin",
),
),
],
options={
"abstract": False,
},
bases=("cms.cmsplugin",),
),
]
1 change: 1 addition & 0 deletions src/open_inwoner/cms/plugins/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .userfeed import UserFeed
from .videoplayer import VideoPlayer
6 changes: 6 additions & 0 deletions src/open_inwoner/cms/plugins/models/userfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cms.models import CMSPlugin


class UserFeed(CMSPlugin):
# TODO add options
Bartvaderkin marked this conversation as resolved.
Show resolved Hide resolved
pass
50 changes: 50 additions & 0 deletions src/open_inwoner/cms/plugins/tests/test_userfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.test import TestCase
from django.utils.html import strip_tags
from django.utils.translation import ugettext as _

from pyquery import PyQuery as PQ

from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.cms.tests import cms_tools
from open_inwoner.userfeed.hooks.common import simple_message

from ..cms_plugins import UserFeedPlugin


class TestUserFeedPlugin(TestCase):
def test_plugin(self):
user = UserFactory()
simple_message(user, "Hello", title="Test message", url="http://foo.bar")

html, context = cms_tools.render_plugin(
UserFeedPlugin, plugin_data={}, user=user
)

feed = context["userfeed"]
self.assertEqual(feed.total_items, 1)

self.assertIn("Test message", html)
self.assertIn("Hello", html)

pyquery = PQ(html)

# test summary
summaries = pyquery.find(".userfeed__summary .userfeed__list-item")
self.assertEqual(len(summaries), 1)

summary = summaries.text()
expected = _("There is {count} message").format(count=1)
self.assertEqual(strip_tags(summary), expected)

# test item
items = pyquery.find(".card-container .card")
self.assertEqual(len(items), 1)

title = items.find("p.tabled__value").text()
self.assertEqual(title, "Test message")

message = items.find(".userfeed__heading").text()
self.assertEqual(message, "Hello")

action_url = items[0].attrib["href"]
self.assertEqual(action_url, "http://foo.bar")
14 changes: 14 additions & 0 deletions src/open_inwoner/cms/utils/page_display.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utilities for determining whether CMS pages are published"""


from django.db.models import Q

from cms.models import Page

from open_inwoner.cms.benefits.cms_apps import SSDApphook
Expand Down Expand Up @@ -52,3 +54,15 @@ def benefits_page_is_published() -> bool:
:returns: True if the social benefits page published, False otherwise
"""
return _is_published("ssd")


def get_active_app_names() -> list[str]:
return list(
Page.objects.published()
.exclude(
Q(application_urls="")
| Q(application_urls__isnull=True)
| Q(application_namespace="")
)
.values_list("application_namespace", flat=True)
)

This file was deleted.

2 changes: 2 additions & 0 deletions src/open_inwoner/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
"open_inwoner.extended_sessions",
"open_inwoner.custom_csp",
"open_inwoner.media",
"open_inwoner.userfeed",
"open_inwoner.cms.profile",
"open_inwoner.cms.cases",
"open_inwoner.cms.inbox",
Expand Down Expand Up @@ -553,6 +554,7 @@
"QuestionnairePlugin",
"ProductFinderPlugin",
"ProductLocationPlugin",
"UserFeedPlugin",
],
"text_only_plugins": ["LinkPlugin"],
"name": _("Content"),
Expand Down
42 changes: 28 additions & 14 deletions src/open_inwoner/openzaak/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
is_info_object_visible,
is_zaak_visible,
)
from open_inwoner.userfeed import hooks
from open_inwoner.utils.logentry import system_action as log_system_action
from open_inwoner.utils.url import build_absolute_url

Expand Down Expand Up @@ -93,10 +94,10 @@ def handle_zaken_notification(notification: Notification):
)
return

inform_users = get_emailable_initiator_users_from_roles(roles)
inform_users = get_initiator_users_from_roles(roles)
if not inform_users:
log_system_action(
f"ignored {r} notification: no users with bsn, valid email or with enabled notifications as (mede)initiators in case {case_url}",
f"ignored {r} notification: no users with bsn/nnp_id as (mede)initiators in case {case_url}",
log_level=logging.INFO,
)
return
Expand Down Expand Up @@ -215,6 +216,16 @@ def _handle_zaakinformatieobject_notification(
def handle_zaakinformatieobject_update(
user: User, case: Zaak, zaak_info_object: ZaakInformatieObject
):
# hook into userfeed
hooks.case_document_added_notification_received(user, case, zaak_info_object)

if not user.cases_notifications or not user.get_contact_email():
log_system_action(
f"ignored user-disabled notification delivery for user '{user}' zaakinformatieobject {zaak_info_object.url} case {case.url}",
log_level=logging.INFO,
)
return

note = UserCaseInfoObjectNotification.objects.record_if_unique_notification(
user,
case.uuid,
Expand Down Expand Up @@ -344,6 +355,17 @@ def _handle_status_notification(notification: Notification, case: Zaak, inform_u


def handle_status_update(user: User, case: Zaak, status: Status):
# hook into userfeed
hooks.case_status_notification_received(user, case, status)

if not user.cases_notifications or not user.get_contact_email():
log_system_action(
f"ignored user-disabled notification delivery for user '{user}' status {status.url} case {case.url}",
log_level=logging.INFO,
)
return

# email notification
note = UserCaseStatusNotification.objects.record_if_unique_notification(
user,
case.uuid,
Expand Down Expand Up @@ -443,19 +465,15 @@ def get_nnp_initiator_nnp_id_from_roles(roles: List[Rol]) -> List[str]:
return list(ret)


def get_emailable_initiator_users_from_roles(roles: List[Rol]) -> List[User]:
def get_initiator_users_from_roles(roles: List[Rol]) -> List[User]:
"""
iterate over Rollen and return User objects for all natural-person initiators we can notify
iterate over Rollen and return User objects for initiators
"""
users = []

bsn_list = get_np_initiator_bsns_from_roles(roles)
if bsn_list:
users += list(
User.objects.filter(
bsn__in=bsn_list, is_active=True, cases_notifications=True
).having_usable_email()
)
users += list(User.objects.filter(bsn__in=bsn_list, is_active=True))

nnp_id_list = get_nnp_initiator_nnp_id_from_roles(roles)
if nnp_id_list:
Expand All @@ -464,10 +482,6 @@ def get_emailable_initiator_users_from_roles(roles: List[Rol]) -> List[User]:
id_filter = {"rsin__in": nnp_id_list}
else:
id_filter = {"kvk__in": nnp_id_list}
users += list(
User.objects.filter(
is_active=True, cases_notifications=True, **id_filter
).having_usable_email()
)
users += list(User.objects.filter(is_active=True, **id_filter))

return users
11 changes: 9 additions & 2 deletions src/open_inwoner/openzaak/tests/test_case_detail.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from unittest.mock import patch
from unittest.mock import Mock, patch

from django.conf import settings
from django.contrib.auth.models import AnonymousUser
Expand Down Expand Up @@ -585,7 +585,11 @@ def _setUpMocks(self, m, use_eindstatus=True):
),
)

def test_status_is_retrieved_when_user_logged_in_via_digid(self, m):
@patch("open_inwoner.userfeed.hooks.case_status_seen")
@patch("open_inwoner.userfeed.hooks.case_documents_seen")
def test_status_is_retrieved_when_user_logged_in_via_digid(
self, m, mock_hook_status: Mock, mock_hook_documents: Mock
):
self.maxDiff = None

ZaakTypeStatusTypeConfigFactory.create(
Expand Down Expand Up @@ -663,6 +667,9 @@ def test_status_is_retrieved_when_user_logged_in_via_digid(self, m):
"new_docs": False,
},
)
# check userfeed hooks
mock_hook_status.assert_called_once()
mock_hook_documents.assert_called_once()

def test_pass_endstatus_type_data_if_endstatus_not_reached(self, m):
self.maxDiff = None
Expand Down
18 changes: 18 additions & 0 deletions src/open_inwoner/openzaak/tests/test_notification_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ def __init__(self):
zaak=self.zaak2["url"],
)

self.informatie_object_extra = generate_oas_component(
"drc",
"schemas/EnkelvoudigInformatieObject",
url=f"{DOCUMENTEN_ROOT}enkelvoudiginformatieobjecten/aaaaaaaa-0002-bbbb-aaaa-aaaaaaaaaaaa",
informatieobjecttype=f"{CATALOGI_ROOT}informatieobjecttypen/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
status="definitief",
vertrouwelijkheidaanduiding=VertrouwelijkheidsAanduidingen.openbaar,
)
self.zaak_informatie_object_extra = generate_oas_component(
"zrc",
"schemas/ZaakInformatieObject",
url=f"{ZAKEN_ROOT}zaakinformatieobjecten/aaaaaaaa-0003-aaaa-aaaa-aaaaaaaaaaaa",
informatieobject=self.informatie_object_extra["url"],
zaak=self.zaak["url"],
)

self.role_initiator = generate_oas_component(
"zrc",
"schemas/Rol",
Expand Down Expand Up @@ -250,6 +266,8 @@ def install_mocks(self, m, *, res404: Optional[List[str]] = None) -> "MockAPIDat
"informatie_object",
"zaak_informatie_object",
"zaak_informatie_object2",
"informatie_object_extra",
"zaak_informatie_object_extra",
]:
resource = getattr(self, resource_attr)
if resource_attr in res404:
Expand Down
Loading
Loading