Skip to content

Commit

Permalink
♻️ [#2060] Use ZGW API clients directly in views
Browse files Browse the repository at this point in the history
instead of using intermediate functions that each build a new client
  • Loading branch information
stevenbal committed Feb 6, 2024
1 parent 357fa9a commit 289fc21
Show file tree
Hide file tree
Showing 22 changed files with 540 additions and 775 deletions.
11 changes: 8 additions & 3 deletions src/open_inwoner/accounts/views/contactmoments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
from view_breadcrumbs import BaseBreadcrumbMixin

from open_inwoner.openklant.api_models import KlantContactMoment
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.constants import Status
from open_inwoner.openklant.models import ContactFormSubject
from open_inwoner.openklant.wrap import (
fetch_klantcontactmoment,
fetch_klantcontactmomenten,
fetch_objectcontactmoment,
get_fetch_parameters,
)
from open_inwoner.openzaak.clients import build_client as build_client_openzaak
from open_inwoner.utils.mixins import PaginationMixin
from open_inwoner.utils.views import CommonPageMixin

Expand Down Expand Up @@ -171,8 +172,12 @@ def get_context_data(self, **kwargs):
if not kcm:
raise Http404()

ocm = fetch_objectcontactmoment(kcm.contactmoment, "zaak")
ctx["zaak"] = getattr(ocm, "object", None)
if client := build_client("contactmomenten"):
zaken_client = build_client_openzaak("zaak")
ocm = client.retrieve_objectcontactmoment(
kcm.contactmoment, "zaak", zaken_client
)
ctx["zaak"] = getattr(ocm, "object", None)

ctx["contactmoment"] = self.get_kcm_data(kcm)
return ctx
19 changes: 10 additions & 9 deletions src/open_inwoner/accounts/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
inbox_page_is_published,
)
from open_inwoner.haalcentraal.utils import fetch_brp
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.wrap import get_fetch_parameters
from open_inwoner.plans.models import Plan
from open_inwoner.questionnaire.models import QuestionnaireStep
from open_inwoner.utils.mixins import ExportMixin
from open_inwoner.utils.views import CommonPageMixin, LogMixin

from ...openklant.wrap import fetch_klant, patch_klant
from ..forms import BrpUserForm, UserForm, UserNotificationsForm
from ..models import Action, User

Expand Down Expand Up @@ -202,18 +202,19 @@ def update_klant_api(self, user_form_data: dict):
if user_form_data.get(local_name)
}
if update_data:
klant = fetch_klant(**get_fetch_parameters(self.request))
if client := build_client("klanten"):
klant = client.retrieve_klant(**get_fetch_parameters(self.request))

if klant:
self.log_system_action(
"retrieved klant for user", user=self.request.user
)
klant = patch_klant(klant, update_data)
if klant:
self.log_system_action(
f"patched klant from user profile edit with fields: {', '.join(sorted(update_data.keys()))}",
user=self.request.user,
"retrieved klant for user", user=self.request.user
)
client.partial_update_klant(klant, update_data)
if klant:
self.log_system_action(
f"patched klant from user profile edit with fields: {', '.join(sorted(update_data.keys()))}",
user=self.request.user,
)

def get_form_class(self):
user = self.request.user
Expand Down
18 changes: 10 additions & 8 deletions src/open_inwoner/cms/cases/views/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

from open_inwoner.htmx.mixins import RequiresHtmxMixin
from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.openzaak.cases import (
fetch_cases,
fetch_cases_by_kvk_or_rsin,
preprocess_data,
)
from open_inwoner.openzaak.cases import preprocess_data
from open_inwoner.openzaak.clients import build_client
from open_inwoner.openzaak.formapi import fetch_open_submissions
from open_inwoner.openzaak.models import OpenZaakConfig
from open_inwoner.openzaak.types import UniformCase
Expand Down Expand Up @@ -59,20 +56,25 @@ def page_title(self):
return _("Mijn aanvragen")

def get_cases(self):
client = build_client("zaak")

if client is None:
return []

if self.request.user.kvk:
kvk_or_rsin = self.request.user.kvk
config = OpenZaakConfig.get_solo()
if config.fetch_eherkenning_zaken_with_rsin:
kvk_or_rsin = self.request.user.rsin
vestigingsnummer = get_kvk_branch_number(self.request.session)
if vestigingsnummer:
raw_cases = fetch_cases_by_kvk_or_rsin(
raw_cases = client.fetch_cases_by_kvk_or_rsin(
kvk_or_rsin=kvk_or_rsin, vestigingsnummer=vestigingsnummer
)
else:
raw_cases = fetch_cases_by_kvk_or_rsin(kvk_or_rsin=kvk_or_rsin)
raw_cases = client.fetch_cases_by_kvk_or_rsin(kvk_or_rsin=kvk_or_rsin)
else:
raw_cases = fetch_cases(self.request.user.bsn)
raw_cases = client.fetch_cases(self.request.user.bsn)
preprocessed_cases = preprocess_data(raw_cases)
return preprocessed_cases

Expand Down
41 changes: 26 additions & 15 deletions src/open_inwoner/cms/cases/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@

from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.openzaak.api_models import Zaak
from open_inwoner.openzaak.cases import (
fetch_roles_for_case_and_bsn,
fetch_roles_for_case_and_kvk_or_rsin,
fetch_roles_for_case_and_vestigingsnummer,
fetch_single_case,
)
from open_inwoner.openzaak.catalog import fetch_single_case_type
from open_inwoner.openzaak.clients import build_client
from open_inwoner.openzaak.models import OpenZaakConfig
from open_inwoner.openzaak.types import UniformCase
from open_inwoner.openzaak.utils import is_zaak_visible
Expand Down Expand Up @@ -67,11 +61,17 @@ def dispatch(self, request, *args, **kwargs):
)
return self.handle_no_permission()

self.case = self.get_case(kwargs)
client = build_client("zaak")
if client is None:
return super().dispatch(request, *args, **kwargs)

self.case = self.get_case(client, kwargs)
if self.case:
# check if we have a role in this case
if request.user.bsn:
if not fetch_roles_for_case_and_bsn(self.case.url, request.user.bsn):
if not client.fetch_roles_for_case_and_bsn(
self.case.url, request.user.bsn
):
logger.debug(
f"CaseAccessMixin - permission denied: no role for the case {self.case.url}"
)
Expand All @@ -83,22 +83,33 @@ def dispatch(self, request, *args, **kwargs):
identifier = self.request.user.rsin

vestigingsnummer = get_kvk_branch_number(self.request.session)
if vestigingsnummer and not fetch_roles_for_case_and_vestigingsnummer(
self.case.url, vestigingsnummer
if (
vestigingsnummer
and not client.fetch_roles_for_case_and_vestigingsnummer(
self.case.url, vestigingsnummer
)
):
logger.debug(
f"CaseAccessMixin - permission denied: no role for the case {self.case.url}"
)
return self.handle_no_permission()

if not fetch_roles_for_case_and_kvk_or_rsin(self.case.url, identifier):
if not client.fetch_roles_for_case_and_kvk_or_rsin(
self.case.url, identifier
):
logger.debug(
f"CaseAccessMixin - permission denied: no role for the case {self.case.url}"
)
return self.handle_no_permission()

# resolve case-type
self.case.zaaktype = fetch_single_case_type(self.case.zaaktype)
if catalogi_client := build_client("catalogi"):
self.case.zaaktype = catalogi_client.fetch_single_case_type(
self.case.zaaktype
)
else:
self.case.zaaktype = None

if not self.case.zaaktype:
logger.debug(
"CaseAccessMixin - permission denied: no case type for case {self.case.url}"
Expand All @@ -120,12 +131,12 @@ def handle_no_permission(self):

return super().handle_no_permission()

def get_case(self, kwargs) -> Optional[Zaak]:
def get_case(self, client, kwargs) -> Optional[Zaak]:
case_uuid = kwargs.get("object_id")
if not case_uuid:
return None

return fetch_single_case(case_uuid)
return client.fetch_single_case(case_uuid)


class OuterCaseAccessMixin(LoginRequiredMixin):
Expand Down
Loading

0 comments on commit 289fc21

Please sign in to comment.