Skip to content

Commit

Permalink
[maykinmedia/open-producten#38] add method to set form slug in Open P…
Browse files Browse the repository at this point in the history
…roducten
  • Loading branch information
Floris272 committed Oct 29, 2024
1 parent 4bdee52 commit 85d387f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
17 changes: 16 additions & 1 deletion src/openforms/contrib/open_producten/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from zgw_consumers.client import build_client

from openforms.contrib.open_producten.models import OpenProductenConfig
from openforms.forms.models import Form
from openforms.utils.api_clients import pagination_helper

from ...utils.api_clients import pagination_helper
from .api_models import Field, ProductType

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,6 +50,20 @@ def get_product_type_fields(self, product_type_uuid) -> list[Field]:

return fields

def set_product_type_form_link(self, product_type_uuid, form: Form):
data = {"open_forms_slug": form.slug}
try:
response = self.patch(f"producttypes/{product_type_uuid}/", json=data)
response.raise_for_status()
except requests.RequestException as exc:
logger.exception(
"exception while setting form_link for product_type_uuid {}".format(
product_type_uuid
),
exc_info=exc,
)
raise exc


class NoServiceConfigured(RuntimeError):
pass
Expand Down
24 changes: 13 additions & 11 deletions src/openforms/contrib/open_producten/generate_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import requests

from ...forms.api.validators import FormIOComponentsValidator
from ...forms.models import Form, FormDefinition, FormStep
from openforms.forms.api.validators import FormIOComponentsValidator
from openforms.forms.models import Form, FormDefinition, FormStep

from .api_models import Field, FieldTypes
from .client import NoServiceConfigured, get_open_producten_client
from .models import ProductType
Expand Down Expand Up @@ -77,31 +78,32 @@ def generate_product_form(product_type: ProductType):
name=f"{product_type.name} form definition", configuration=configuration
)
form = Form.objects.create(
name=f"{product_type.name} form",
name_en=f"{product_type.name} form",
name_nl=f"{product_type.name} formulier",
active=False,
maintenance_mode=True,
product=product_type,
)
FormStep.objects.create(form=form, form_definition=form_definition)

open_producten_client.set_product_type_form_link(product_type.uuid, form)

except NoServiceConfigured:
raise FormGenerationException("No open producten service configured.")
except requests.RequestException as exc:
logger.error(
f"form generation for product type {product_type.name} failed on fields request for",
exc_info=exc,
)
raise FormGenerationException(
f"product type {product_type.name} fields request to Open Producten failed."
)
f"product type {product_type.name} request(s) to Open Producten failed."
) from exc
except ValidationError as exc:
logger.error(
f"form generation for product type {product_type.name} failed on configuration validation",
exc_info=exc,
)
raise FormGenerationException(
f"generated configuration for product {product_type.name} is invalid."
)
) from exc
except Exception as exc:
logger.error("form generation failed", exc_info=exc)
raise FormGenerationException("Something went wrong while generating forms.")
raise FormGenerationException(
"Something went wrong while generating forms."
) from exc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def handle(self, *args, **options):
updated,
) = price_importer.import_product_types()

self.stdout.write(f"updated {len(updated)} exising product type(s)")
self.stdout.write(f"created {len(created)} new product type(s):\n")
self.stdout.write(f"updated {len(updated)} exising object(s)")
self.stdout.write(f"created {len(created)} new object(s):\n")

for instance in created:
self.stdout.write(f"{type(instance).__name__}: {instance.uuid}")
25 changes: 25 additions & 0 deletions src/openforms/contrib/open_producten/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
get_open_producten_client,
)
from openforms.contrib.open_producten.models import OpenProductenConfig
from openforms.forms.tests.factories import FormFactory
from openforms.products.tests.factories import ProductFactory


Expand Down Expand Up @@ -123,3 +124,27 @@ def test_get_product_type_fields_with_request_exception(self):

with self.assertRaises(requests.RequestException):
self.client.get_product_type_fields(product.uuid)

def test_set_product_type_form_link(self):
form = FormFactory()
product = ProductFactory()
mock = self.requests_mock.patch(
json={"id": "35857b78-0bd4-42f8-8ed8-a8088a61545f"},
status_code=200,
url=f"https://test/producttypes/{product.uuid}/",
)

self.client.set_product_type_form_link(product.uuid, form)
self.assertEqual(mock.last_request.json(), {"open_forms_slug": form.slug})

def test_set_product_type_form_link_with_request_exception(self):
form = FormFactory()
product = ProductFactory()
self.requests_mock.patch(
status_code=404,
json={"error": "not found"},
url=f"https://test/producttypes/{product.uuid}/",
)

with self.assertRaises(requests.RequestException):
self.client.set_product_type_form_link(product.uuid, form)
2 changes: 1 addition & 1 deletion src/openforms/contrib/open_producten/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def test_import_prices(self, mock_import_product_types, mock_client):

self.assertEqual(
out,
f"updated 0 exising product type(s)\ncreated 3 new product type(s):\nProduct: {uuid}\nPrice: {uuid}\nPriceOption: {uuid}\n",
f"updated 0 exising object(s)\ncreated 3 new object(s):\nProduct: {uuid}\nPrice: {uuid}\nPriceOption: {uuid}\n",
)
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def test_generate_form(self, mock_get_client):
]
},
)
client_mock.set_product_type_form_link.assert_called_once()

@patch(
"openforms.contrib.open_producten.generate_form.get_open_producten_client",
Expand All @@ -257,7 +258,7 @@ def test_generate_form_with_request_exception(self, mock_get_client):

with self.assertRaisesMessage(
FormGenerationException,
f"product type {self.product.name} fields request to Open Producten failed.",
f"product type {self.product.name} request(s) to Open Producten failed.",
):
generate_product_form(self.product)

Expand Down

0 comments on commit 85d387f

Please sign in to comment.