Skip to content

Commit

Permalink
Merge pull request #1494 from maykinmedia/issue/openklant2-integration
Browse files Browse the repository at this point in the history
Improve error handling for use of klanten services
  • Loading branch information
alextreme authored Nov 13, 2024
2 parents 5e660ba + ddc820e commit 9d468e1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 42 deletions.
47 changes: 27 additions & 20 deletions src/open_inwoner/accounts/signals.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging

from django.conf import settings
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.core.exceptions import ImproperlyConfigured
from django.dispatch import receiver
from django.urls import reverse
from django.utils.translation import gettext as _

from open_inwoner.accounts.models import User
from open_inwoner.haalcentraal.models import HaalCentraalConfig
from open_inwoner.haalcentraal.utils import update_brp_data_in_db
from open_inwoner.openklant.models import OpenKlant2Config
from open_inwoner.openklant.services import OpenKlant2Service, eSuiteKlantenService
from open_inwoner.utils.logentry import user_action

Expand Down Expand Up @@ -37,31 +37,38 @@ def update_user_from_klant_on_login(sender, user, request, *args, **kwargs):
return

# OpenKlant2
# TODO: replace with proper config and refactor branching
use_ok2 = getattr(settings, "OPENKLANT2_ACTIVE", None)
if use_ok2 and (openklant2_config := OpenKlant2Config.from_django_settings()):
try:
service = OpenKlant2Service(config=openklant2_config)
except RuntimeError:
logger.error("OpenKlant2 service failed to build")
return

if not (fetch_params := service.get_fetch_parameters(request=request)):
return

partij, created = service.get_or_create_partij_for_user(
fetch_params=fetch_params, user=user
)
if partij and not created:
service.update_user_from_partij(partij_uuid=partij["uuid"], user=user)
try:
service = OpenKlant2Service()
except ImproperlyConfigured:
logger.error("OpenKlant2 configuration missing")
else:
_update_user_from_openklant2(user=user, service=service, request=request)

# eSuite
try:
service = eSuiteKlantenService()
except ImproperlyConfigured:
logger.error("eSuiteKlantenService missing configuration")
except RuntimeError:
logger.error("eSuiteKlantenService failed to build")
return
else:
_update_user_from_esuite(user=user, service=service, request=request)


def _update_user_from_openklant2(
user: User, service: OpenKlant2Service, request
) -> None:
if fetch_params := service.get_fetch_parameters(request=request):
partij, created = service.get_or_create_partij_for_user(
fetch_params=fetch_params, user=user
)
if partij and not created:
service.update_user_from_partij(partij_uuid=partij.uuid, user=user)


def _update_user_from_esuite(
user: User, service: eSuiteKlantenService, request
) -> None:
if not (fetch_params := service.get_fetch_parameters(request=request)):
return

Expand Down
4 changes: 4 additions & 0 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django import forms
from django.conf import settings
from django.contrib.auth import signals
from django.template.defaultfilters import date as django_date
from django.test import TestCase, override_settings, tag
from django.urls import reverse, reverse_lazy
Expand All @@ -15,6 +16,7 @@
from webtest import Upload

from open_inwoner.accounts.choices import NotificationChannelChoice, StatusChoices
from open_inwoner.accounts.signals import update_user_from_klant_on_login
from open_inwoner.cms.profile.cms_appconfig import ProfileConfig
from open_inwoner.configurations.models import SiteConfiguration
from open_inwoner.haalcentraal.tests.mixins import HaalCentraalMixin
Expand Down Expand Up @@ -316,6 +318,8 @@ def test_messages_enabled_disabled(self):
)
class EditProfileTests(AssertTimelineLogMixin, WebTest):
def setUp(self):
signals.user_logged_in.disconnect(receiver=update_user_from_klant_on_login)

self.url = reverse("profile:edit")
self.return_url = reverse("profile:detail")
self.user = UserFactory()
Expand Down
45 changes: 29 additions & 16 deletions src/open_inwoner/accounts/views/contactmoments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Iterable, Protocol

from django.contrib.auth.mixins import AccessMixin
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import redirect
from django.urls import reverse
Expand Down Expand Up @@ -89,11 +90,19 @@ def get_fetch_parameters(
class KlantContactMomentBaseView(
CommonPageMixin, BaseBreadcrumbMixin, KlantContactMomentAccessMixin, TemplateView
):
def get_service(self, service_type: KlantenServiceType) -> VragenService:
def get_service(self, service_type: KlantenServiceType) -> VragenService | None:
if service_type == KlantenServiceType.ESUITE:
return eSuiteVragenService()
try:
return eSuiteVragenService()
except ImproperlyConfigured:
logger.error("eSuiteVragenService configuration missing")
elif service_type == KlantenServiceType.OPENKLANT2:
return OpenKlant2Service()
try:
return OpenKlant2Service()
except ImproperlyConfigured:
logger.error("OpenKlant2 configuration missing")
except RuntimeError:
logger.error("Failed to build OpenKlant2Service")

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
Expand Down Expand Up @@ -135,20 +144,24 @@ def get_anchors(self) -> list:

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
esuite_service = self.get_service(service_type=KlantenServiceType.ESUITE)
ok2_service = self.get_service(service_type=KlantenServiceType.OPENKLANT2)

questions_esuite = esuite_service.list_questions(
fetch_params=self.get_fetch_params(esuite_service),
user=self.request.user,
)
questions_ok2 = ok2_service.list_questions(
self.get_fetch_params(ok2_service),
user=self.request.user,
)
all_questions = questions_esuite + questions_ok2
all_questions.sort(key=lambda q: q["registered_date"], reverse=True)
ctx["contactmomenten"] = all_questions
questions = []
if esuite_service := self.get_service(service_type=KlantenServiceType.ESUITE):
questions.extend(
esuite_service.list_questions(
fetch_params=self.get_fetch_params(esuite_service),
user=self.request.user,
)
)
if ok2_service := self.get_service(service_type=KlantenServiceType.OPENKLANT2):
questions.extend(
ok2_service.list_questions(
self.get_fetch_params(ok2_service),
user=self.request.user,
)
)
questions.sort(key=lambda q: q["registered_date"], reverse=True)
ctx["contactmomenten"] = questions

paginator_dict = self.paginate_with_context(ctx["contactmomenten"])
ctx.update(paginator_dict)
Expand Down
4 changes: 3 additions & 1 deletion src/open_inwoner/accounts/views/mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from django.core.exceptions import ImproperlyConfigured

from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.openklant.services import eSuiteKlantenService

Expand All @@ -13,7 +15,7 @@ def patch_klant(self, update_data: dict):

try:
service = eSuiteKlantenService(config=OpenKlantConfig.get_solo())
except RuntimeError:
except ImproperlyConfigured:
logger.error("Error building KlantenService")
return

Expand Down
13 changes: 8 additions & 5 deletions src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Iterable, Literal, NotRequired, Protocol, Self

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -142,11 +143,13 @@ class eSuiteKlantenService(KlantenService):
def __init__(self, config: OpenKlantConfig | None = None):
self.config = config or OpenKlantConfig.get_solo()
if not self.config:
raise RuntimeError("eSuiteKlantenService instance needs a configuration")
raise ImproperlyConfigured(
"eSuiteKlantenService instance needs a configuration"
)

self.service_config = self.config.klanten_service
if not self.service_config:
raise RuntimeError(
raise ImproperlyConfigured(
"eSuiteKlantenService instance needs a servivce configuration"
)

Expand Down Expand Up @@ -770,16 +773,16 @@ class OpenKlant2Service(KlantenService):
def __init__(self, config: OpenKlant2Config | None = None):
self.config = config or OpenKlant2Config.from_django_settings()
if not self.config:
raise RuntimeError("OpenKlant2Service instance needs a configuration")
raise ImproperlyConfigured(
"Please set OPENKLANT2_CONFIG in your settings to configure OpenKlant2"
)

self.client = OpenKlant2Client(
base_url=self.config.api_url,
request_kwargs={
"headers": {"Authorization": f"Token {self.config.api_token}"}
},
)
if not self.client:
raise RuntimeError("OpenKlant2Service instance needs a client")

if mijn_vragen_actor := getattr(config, "mijn_vragen_actor", None):
self.mijn_vragen_actor = (
Expand Down
4 changes: 4 additions & 0 deletions src/open_inwoner/openklant/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch
from uuid import uuid4

from django.contrib.auth import signals
from django.test import modify_settings, override_settings
from django.urls import reverse
from django.utils.translation import gettext as _
Expand All @@ -12,6 +13,7 @@
from pyquery import PyQuery
from zgw_consumers.api_models.base import factory

from open_inwoner.accounts.signals import update_user_from_klant_on_login
from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.configurations.models import SiteConfiguration
from open_inwoner.openklant.api_models import ContactMoment, Klant, KlantContactMoment
Expand Down Expand Up @@ -91,6 +93,8 @@ class ContactMomentViewsTestCase(

def setUp(self):
super().setUp()
signals.user_logged_in.disconnect(receiver=update_user_from_klant_on_login)

MockAPIReadData.setUpServices()
self.api_group = ZGWApiGroupConfig.objects.get()

Expand Down

0 comments on commit 9d468e1

Please sign in to comment.