-
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 Referentielijsten API
- Loading branch information
Showing
4 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
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,86 @@ | ||
from django.utils.translation import gettext as _ | ||
|
||
from ape_pie import APIClient | ||
from glom import glom | ||
from json_logic import jsonLogic | ||
from requests.exceptions import RequestException | ||
from zgw_consumers.client import build_client | ||
from zgw_consumers.models import Service | ||
|
||
from openforms.logging import logevent | ||
from openforms.submissions.models import Submission | ||
from openforms.typing import DataMapping, JSONObject | ||
|
||
from ..typing import Component | ||
|
||
|
||
class ReferentielijstenClient(APIClient): | ||
# TODO cache this | ||
def get_items_for_tabel(self, code): | ||
response = self.get("items", params={"tabel__code": code}, timeout=10) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
def fetch_options_from_referentielijsten( | ||
component: Component, | ||
data: DataMapping, | ||
submission: Submission, | ||
items_expression: JSONObject, | ||
): | ||
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 | ||
|
||
# TODO also check if service is part of `referentielijsten_services`? | ||
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(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: | ||
# TODO handle non success cases | ||
if not (items_array := jsonLogic(items_expression, result)): | ||
logevent.referentielijsten_failure_response( | ||
submission.form, | ||
component, | ||
_("No results found from Referentielijsten API."), | ||
) | ||
return items_array |
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