-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#4993] Implement fetching select(boxes) options from Referentielij…
…sten this was previously possible with logic and service fetch, but this functionality provides a shortcut to more easily integrate with Re ferentielijsten API
- Loading branch information
Showing
7 changed files
with
186 additions
and
24 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from functools import partial | ||
from typing import Any, TypedDict | ||
|
||
from django.core.cache import cache | ||
|
||
from ape_pie import APIClient | ||
from zgw_consumers.service import pagination_helper | ||
|
||
REFERENTIELIJSTEN_LOOKUP_CACHE_TIMEOUT = 5 * 60 | ||
|
||
|
||
class TabelItem(TypedDict): | ||
code: str | ||
naam: str | ||
begindatumGeldigheid: str # ISO 8601 datetime string | ||
einddatumGeldigheid: str | None # ISO 8601 datetime string | ||
aanvullendeGegevens: Any | ||
|
||
|
||
class ReferentielijstenClient(APIClient): | ||
def get_items_for_tabel(self, code: str) -> list[TabelItem]: | ||
response = self.get("items", params={"tabel__code": code}, timeout=10) | ||
response.raise_for_status() | ||
data = response.json() | ||
all_data = list(pagination_helper(self, data)) | ||
return all_data | ||
|
||
def get_items_for_tabel_cached(self, code: str) -> list[TabelItem]: | ||
result = cache.get_or_set( | ||
key=f"referentielijsten|get_items_for_tabel|code:{code}", | ||
default=partial(self.get_items_for_tabel, code), | ||
timeout=REFERENTIELIJSTEN_LOOKUP_CACHE_TIMEOUT, | ||
) | ||
assert result is not None | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from django.utils.translation import gettext as _ | ||
|
||
from glom import glom | ||
from requests.exceptions import RequestException | ||
from zgw_consumers.client import build_client | ||
from zgw_consumers.models import Service | ||
|
||
from openforms.contrib.referentielijsten.client import ReferentielijstenClient | ||
from openforms.logging import logevent | ||
from openforms.submissions.models import Submission | ||
|
||
from ..typing import Component | ||
|
||
|
||
def fetch_options_from_referentielijsten( | ||
component: Component, submission: Submission | ||
) -> list[tuple[str, str]] | None: | ||
service_slug = glom(component, "openForms.service", default=None) | ||
code = glom(component, "openForms.code", default=None) | ||
if not service_slug: | ||
logevent.form_configuration_error( | ||
submission.form, | ||
component, | ||
_( | ||
"Cannot fetch from Referentielijsten API, because no `service` is configured." | ||
), | ||
) | ||
return | ||
|
||
if not code: | ||
logevent.form_configuration_error( | ||
submission.form, | ||
component, | ||
_( | ||
"Cannot fetch from Referentielijsten API, because no `code` is configured." | ||
), | ||
) | ||
return | ||
|
||
try: | ||
service = Service.objects.get(slug=service_slug) | ||
except Service.DoesNotExist: | ||
logevent.form_configuration_error( | ||
submission.form, | ||
component, | ||
_( | ||
"Cannot fetch from Referentielijsten API, service with {service_slug} does not exist." | ||
).format(service_slug=service_slug), | ||
) | ||
return | ||
|
||
try: | ||
with build_client(service, client_factory=ReferentielijstenClient) as client: | ||
result = client.get_items_for_tabel_cached(code) | ||
except RequestException as e: | ||
logevent.referentielijsten_failure_response( | ||
submission.form, | ||
component, | ||
_( | ||
"Exception occurred while fetching from Referentielijsten API: {exception}." | ||
).format(exception=e), | ||
) | ||
return | ||
else: | ||
if not result: | ||
logevent.referentielijsten_failure_response( | ||
submission.form, | ||
component, | ||
_("No results found from Referentielijsten API."), | ||
) | ||
return [[item["code"], item["naam"]] for item in result] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/openforms/logging/templates/logging/events/referentielijsten_failure_response.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% load i18n %} | ||
{% blocktrans trimmed with lead=log.fmt_lead url=log.fmt_url %} | ||
{{ lead }}: Failed to fetch items from Referentielijsten API: {{ error }} | ||
{% endblocktrans %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters