-
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.
Merge pull request #3646 from open-formulieren/feature/3607-brk-valid…
…ator [#3607] Refactor validation plugin, add BRK validator
- Loading branch information
Showing
54 changed files
with
1,635 additions
and
59 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
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
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
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
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
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,10 @@ | ||
from django.contrib import admin | ||
|
||
from solo.admin import SingletonModelAdmin | ||
|
||
from .models import BRKConfig | ||
|
||
|
||
@admin.register(BRKConfig) | ||
class BRKConfigAdmin(SingletonModelAdmin): | ||
pass |
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,12 @@ | ||
from django.apps import AppConfig | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class BRKApp(AppConfig): | ||
name = "openforms.contrib.brk" | ||
label = "brk" | ||
verbose_name = _("BRK configuration") | ||
|
||
def ready(self): | ||
# register the plugin | ||
from . import validators # noqa |
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,48 @@ | ||
from django.urls import reverse | ||
from django.utils.translation import gettext, gettext_lazy as _ | ||
|
||
import requests | ||
|
||
from openforms.config.data import Action | ||
from openforms.plugins.exceptions import InvalidPluginConfiguration | ||
|
||
from .client import NoServiceConfigured, get_client | ||
from .models import BRKConfig | ||
|
||
|
||
class BRKValidatorCheck: | ||
verbose_name: str = _("Validation plugin config: BRK - Zakelijk gerechtigd") # type: ignore | ||
|
||
@staticmethod | ||
def check_config(): | ||
try: | ||
with get_client() as client: | ||
results = client.get_real_estate_by_address( | ||
{"postcode": "1234AB", "huisnummer": "1"} | ||
) | ||
except NoServiceConfigured as exc: | ||
msg = _("{api_name} endpoint is not configured.").format(api_name="KVK") | ||
raise InvalidPluginConfiguration(msg) from exc | ||
except requests.RequestException as exc: | ||
raise InvalidPluginConfiguration( | ||
_("Invalid response: {exception}").format(exception=exc) | ||
) from exc | ||
|
||
if not isinstance(results, dict): | ||
raise InvalidPluginConfiguration(_("Response data is not a dictionary")) | ||
|
||
items = results.get("_embedded") | ||
if items is None or not isinstance(items, dict): | ||
raise InvalidPluginConfiguration(_("Response does not contain results")) | ||
|
||
@staticmethod | ||
def get_config_actions() -> list[Action]: | ||
return [ | ||
( | ||
gettext("Configuration"), | ||
reverse( | ||
"admin:brk_brkconfig_change", | ||
args=(BRKConfig.singleton_instance_id,), | ||
), | ||
), | ||
] |
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 @@ | ||
import logging | ||
from typing import TypedDict | ||
|
||
import requests | ||
from ape_pie.client import APIClient | ||
from typing_extensions import NotRequired | ||
|
||
from zgw_consumers_ext.api_client import ServiceClientFactory | ||
|
||
from .models import BRKConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class NoServiceConfigured(RuntimeError): | ||
pass | ||
|
||
|
||
def get_client() -> "BRKClient": | ||
config = BRKConfig.get_solo() | ||
assert isinstance(config, BRKConfig) | ||
if not (service := config.service): | ||
raise NoServiceConfigured("No BRK service configured!") | ||
service_client_factory = ServiceClientFactory(service) | ||
return BRKClient.configure_from(service_client_factory) | ||
|
||
|
||
class SearchParams(TypedDict): | ||
postcode: str | ||
huisnummer: str | ||
huisletter: NotRequired[str] | ||
huisnummertoevoeging: NotRequired[str] | ||
|
||
|
||
class BRKClient(APIClient): | ||
def get_real_estate_by_address(self, query_params: SearchParams): | ||
""" | ||
Search for real estate by querying for a specific address. | ||
API docs: https://vng-realisatie.github.io/Haal-Centraal-BRK-bevragen/swagger-ui-2.0#/Kadastraal%20Onroerende%20Zaken/GetKadastraalOnroerendeZaken | ||
""" | ||
assert query_params, "You must provide at least one query parameter" | ||
|
||
try: | ||
response = self.get( | ||
"kadastraalonroerendezaken", | ||
params=query_params, | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
logger.exception("exception while making BRK request", exc_info=exc) | ||
raise exc | ||
|
||
return response.json() | ||
|
||
def get_cadastral_titleholders_by_cadastral_id(self, cadastral_id: str): | ||
""" | ||
Look up the rightholders of a property (e.g. a house) in the Dutch cadastre. | ||
API docs: https://vng-realisatie.github.io/Haal-Centraal-BRK-bevragen/swagger-ui-2.0#/Zakelijke%20Gerechtigden/GetZakelijkGerechtigden | ||
""" | ||
try: | ||
response = self.get( | ||
f"kadastraalonroerendezaken/{cadastral_id}/zakelijkgerechtigden", | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
logger.exception("exception while making BRK request", exc_info=exc) | ||
raise exc | ||
|
||
return response.json() |
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,10 @@ | ||
from typing import TypedDict | ||
|
||
from typing_extensions import NotRequired | ||
|
||
|
||
class AddressValue(TypedDict): | ||
postcode: str | ||
house_number: str | ||
house_letter: NotRequired[str] | ||
house_number_addition: NotRequired[str] |
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,45 @@ | ||
# Generated by Django 3.2.23 on 2023-11-29 16:05 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
("zgw_consumers", "0019_alter_service_uuid"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="BRKConfig", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"service", | ||
models.OneToOneField( | ||
help_text="Service for API interaction with the BRK.", | ||
limit_choices_to={"api_type": "orc"}, | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="+", | ||
to="zgw_consumers.service", | ||
verbose_name="BRK API", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "BRK configuration", | ||
}, | ||
), | ||
] |
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,36 @@ | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from solo.models import SingletonModel | ||
from zgw_consumers.constants import APITypes | ||
|
||
|
||
class BRKConfigManager(models.Manager): | ||
def get_queryset(self): | ||
qs = super().get_queryset() | ||
return qs.select_related( | ||
"service", | ||
"service__client_certificate", | ||
"service__server_certificate", | ||
) | ||
|
||
|
||
class BRKConfig(SingletonModel): | ||
""" | ||
Global configuration and defaults. | ||
""" | ||
|
||
service = models.OneToOneField( | ||
"zgw_consumers.Service", | ||
verbose_name=_("BRK API"), | ||
help_text=_("Service for API interaction with the BRK."), | ||
on_delete=models.PROTECT, | ||
limit_choices_to={"api_type": APITypes.orc}, | ||
related_name="+", | ||
null=True, | ||
) | ||
|
||
objects = BRKConfigManager() | ||
|
||
class Meta: | ||
verbose_name = _("BRK configuration") |
Empty file.
Oops, something went wrong.