Skip to content

Commit

Permalink
♻️ [#3489] Refactor stuf_zds registration to use similar interface...
Browse files Browse the repository at this point in the history
... as the other client definitions/usages in other parts of the
project.
  • Loading branch information
sergei-maertens committed Sep 27, 2023
1 parent d133c37 commit 07ad16f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
41 changes: 19 additions & 22 deletions src/openforms/registrations/contrib/stuf_zds/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from openforms.submissions.models import Submission, SubmissionReport
from openforms.utils.mixins import JsonSchemaSerializerMixin
from stuf.stuf_zds.client import NoServiceConfigured, ZaakOptions, get_client
from stuf.stuf_zds.constants import VertrouwelijkheidsAanduidingen
from stuf.stuf_zds.models import StufZDSConfig

Expand Down Expand Up @@ -195,11 +196,10 @@ class StufZDSRegistration(BasePlugin):
),
}

def pre_register_submission(self, submission: "Submission", options: dict) -> None:
config = StufZDSConfig.get_solo()
config.apply_defaults_to(options)

with config.get_client(options) as client:
def pre_register_submission(
self, submission: "Submission", options: ZaakOptions
) -> None:
with get_client(options=options) as client:
# obtain a zaaknummer & save it - first, check if we have an intermediate result
# from earlier attempts. if we do, do not generate a new number
zaak_id = execute_unless_result_exists(
Expand All @@ -213,8 +213,8 @@ def pre_register_submission(self, submission: "Submission", options: dict) -> No
submission.save()

def register_submission(
self, submission: Submission, options: dict
) -> Optional[dict]:
self, submission: Submission, options: ZaakOptions
) -> dict | None:
"""
Register the submission by creating a ZAAK.
Expand All @@ -225,12 +225,8 @@ def register_submission(
prevents Open Forms from reserving case numbers over and over again (for
example). See #1183 for a reported issue about this.
"""
config = StufZDSConfig.get_solo()
config.apply_defaults_to(options)

options["omschrijving"] = submission.form.admin_name

with config.get_client(options) as client:
with get_client(options=options) as client:
# Zaak ID reserved during the pre-registration phase
zaak_id = submission.public_registration_reference

Expand Down Expand Up @@ -339,34 +335,35 @@ def get_reference_from_result(self, result: Dict[str, str]) -> str:
"""
return result["zaak"]

def update_payment_status(self, submission: "Submission", options: dict):
config = StufZDSConfig.get_solo()
config.apply_defaults_to(options)
with config.get_client(options) as client:
def update_payment_status(self, submission: "Submission", options: ZaakOptions):
with get_client(options) as client:
client.set_zaak_payment(
submission.registration_result["zaak"],
)

def check_config(self):
config = StufZDSConfig.get_solo()
assert isinstance(config, StufZDSConfig)
if not config.service_id:
raise InvalidPluginConfiguration(_("StufService not selected"))

if not config.gemeentecode:
raise InvalidPluginConfiguration(
_("StufService missing setting '{name}'").format(name="gemeentecode")
)

options = {
options: ZaakOptions = {
"omschrijving": "MyForm",
"zds_zaaktype_code": "test",
"zds_zaaktype_omschrijving": "test",
"zds_zaaktype_status_code": "test",
"zds_zaaktype_status_omschrijving": "test",
"zds_documenttype_omschrijving_inzending": "test",
}
config.apply_defaults_to(options)
with config.get_client(options) as client:
} # type: ignore
try:
client = get_client(options)
except NoServiceConfigured:
raise InvalidPluginConfiguration(_("StufService not selected"))

with client:
try:
client.check_config()
except Exception as e:
Expand Down
14 changes: 14 additions & 0 deletions src/stuf/stuf_zds/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..service_client_factory import ServiceClientFactory, get_client_init_kwargs
from ..xml import fromstring
from .constants import STUF_ZDS_EXPIRY_MINUTES
from .models import StufZDSConfig

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,6 +97,19 @@ class ZaakOptions(TypedDict):
referentienummer: str


class NoServiceConfigured(RuntimeError):
pass


def get_client(options: ZaakOptions) -> "Client":
config = StufZDSConfig.get_solo()
assert isinstance(config, StufZDSConfig)
config.apply_defaults_to(options)
if not (service := config.service):
raise NoServiceConfigured("You must configure a service!")
return StufZDSClient(service, options)


def StufZDSClient(service: StufService, options: ZaakOptions) -> "Client":
factory = ServiceClientFactory(service)
init_kwargs = get_client_init_kwargs(
Expand Down
5 changes: 0 additions & 5 deletions src/stuf/stuf_zds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,5 @@ class StufZDSConfig(SingletonModel):
def apply_defaults_to(self, options):
options.setdefault("gemeentecode", self.gemeentecode)

def get_client(self, options):
from .client import StufZDSClient

return StufZDSClient(self.service, options)

class Meta:
verbose_name = _("StUF-ZDS configuration")

0 comments on commit 07ad16f

Please sign in to comment.