Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Oct 16, 2024
1 parent a2ba80f commit 0817671
Show file tree
Hide file tree
Showing 33 changed files with 1,625 additions and 1,119 deletions.
1 change: 1 addition & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9748,6 +9748,7 @@ components:
type: string
format: decimal
pattern: ^-?\d{0,8}(?:\.\d{0,2})?$
nullable: true
information:
type: string
description: Information text to be displayed in the confirmation page and
Expand Down
4 changes: 0 additions & 4 deletions src/openforms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from openforms.utils.urls import decorator_include
from openforms.variables.api.viewsets import ServiceFetchConfigurationViewSet

from ..contrib.open_producten.api.viewsets import PriceViewSet
from .views import PingView

# from .schema import schema_view
Expand Down Expand Up @@ -59,9 +58,6 @@
# products
router.register("products", ProductViewSet)

# product prices (Open Producten)
router.register("product_prices", PriceViewSet, "prices")

# services
router.register("services", ServiceViewSet)

Expand Down
1,992 changes: 1,098 additions & 894 deletions src/openforms/conf/locale/nl/LC_MESSAGES/django.po

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/openforms/contrib/open_producten/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
PRICE_OPTION_KEY = "productPrice"
27 changes: 0 additions & 27 deletions src/openforms/contrib/open_producten/api/serializers.py

This file was deleted.

41 changes: 0 additions & 41 deletions src/openforms/contrib/open_producten/api/viewsets.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ def handle(self, *args, **options):
(

Check warning on line 21 in src/openforms/contrib/open_producten/management/commands/import_prices.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/contrib/open_producten/management/commands/import_prices.py#L21

Added line #L21 was not covered by tests
created,
updated,
deleted_count,
soft_deleted_count,
) = price_importer.import_product_types()

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

Check warning on line 27 in src/openforms/contrib/open_producten/management/commands/import_prices.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/contrib/open_producten/management/commands/import_prices.py#L26-L27

Added lines #L26 - L27 were not covered by tests

Expand Down
17 changes: 16 additions & 1 deletion src/openforms/contrib/open_producten/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.16 on 2024-10-10 12:59
# Generated by Django 4.2.16 on 2024-10-10 15:06

import datetime
from decimal import Decimal
Expand All @@ -12,6 +12,10 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
(
"products",
"0002_product_is_deleted_product_upl_name_product_upl_uri_and_more",
),
("zgw_consumers", "0020_service_timeout"),
]

Expand All @@ -33,10 +37,21 @@ class Migration(migrations.Migration):
verbose_name="Start date",
),
),
(
"product_type",
models.ForeignKey(
help_text="The product type that this price belongs to",
on_delete=django.db.models.deletion.CASCADE,
related_name="prices",
to="products.product",
verbose_name="Product type",
),
),
],
options={
"verbose_name": "Price",
"verbose_name_plural": "Prices",
"unique_together": {("product_type", "valid_from")},
},
),
migrations.CreateModel(
Expand Down
85 changes: 44 additions & 41 deletions src/openforms/contrib/open_producten/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,49 @@ class Meta:
abstract = True


class ProductType(models.Model):
upl_name = models.CharField(
verbose_name=_("Name"),
max_length=100,
help_text=_("Uniform product name"),
blank=True,
null=True,
editable=False,
)

upl_uri = models.URLField(
verbose_name=_("Url"),
blank=True,
null=True,
editable=False,
help_text=_("Url to the upn definition."),
)

is_deleted = models.BooleanField(
verbose_name=_("Is deleted"),
default=False,
help_text=_(
"set when the product is deleted in open producten but is linked to existing submission."
),
)

@property
def open_producten_price(self):
now = datetime.date.today()
return self.prices.filter(valid_from__lte=now).order_by("valid_from").last()

class Meta:
abstract = True


class Price(BaseModel):
product_type = models.ForeignKey(
"products.Product",
verbose_name=_("Product type"),
on_delete=models.CASCADE,
related_name="prices",
help_text=_("The product type that this price belongs to"),
)
valid_from = models.DateField(
verbose_name=_("Start date"),
validators=[MinValueValidator(datetime.date.today)],
Expand All @@ -42,9 +84,10 @@ class Price(BaseModel):
class Meta:
verbose_name = _("Price")
verbose_name_plural = _("Prices")
unique_together = ("product_type", "valid_from")

def __str__(self):
return f"{self.product_type.name} {self.valid_from}"
return f"{self.product_type.upl_name} {self.valid_from}"

Check warning on line 90 in src/openforms/contrib/open_producten/models.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/contrib/open_producten/models.py#L90

Added line #L90 was not covered by tests


class PriceOption(BaseModel):
Expand Down Expand Up @@ -74,43 +117,3 @@ class Meta:

def __str__(self):
return f"{self.description} {self.amount}"

Check warning on line 119 in src/openforms/contrib/open_producten/models.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/contrib/open_producten/models.py#L119

Added line #L119 was not covered by tests


class ProductType(models.Model):
open_producten_price = models.OneToOneField(
Price,
verbose_name=_("Price"),
on_delete=models.CASCADE,
related_name="product_type",
help_text=_("The price this option belongs to"),
blank=True,
null=True,
editable=False,
)
upl_name = models.CharField(
verbose_name=_("Name"),
max_length=100,
help_text=_("Uniform product name"),
blank=True,
null=True,
editable=False,
)

upl_uri = models.URLField(
verbose_name=_("Url"),
blank=True,
null=True,
editable=False,
help_text=_("Url to the upn definition."),
)

is_deleted = models.BooleanField(
verbose_name=_("Is deleted"),
default=False,
help_text=_(
"set when the product is deleted in open producten but is linked to existing submission."
),
)

class Meta:
abstract = True
37 changes: 7 additions & 30 deletions src/openforms/contrib/open_producten/price_import.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import models, transaction
from django.db.models import Q
from django.db.models.deletion import ProtectedError

import openforms.contrib.open_producten.api_models as api_models
from openforms.contrib.open_producten.models import Price, PriceOption
Expand All @@ -13,8 +11,6 @@ def __init__(self, client):
self.client = client
self.created_objects = []
self.updated_objects = []
self.deleted_count = 0
self.soft_deleted_count = 0
self.product_types = []

def _add_to_log_list(self, instance: models.Model, created: bool):
Expand All @@ -28,12 +24,9 @@ def import_product_types(self):

self.product_types = self.client.get_current_prices()
self._handle_product_types(self.product_types)
self._delete_non_updated_objects()
return (
self.created_objects,
self.updated_objects,
self.deleted_count,
self.soft_deleted_count,
)

def _handle_options(self, options: [api_models.PriceOption], price_instance: Price):
Expand All @@ -48,11 +41,12 @@ def _handle_options(self, options: [api_models.PriceOption], price_instance: Pri
)
self._add_to_log_list(option_instance, created)

def _update_or_create_price(self, price: api_models.Price):
def _update_or_create_price(self, price: api_models.Price, product_type_instance):
price_instance, created = Price.objects.update_or_create(
uuid=price.id,
defaults={
"valid_from": price.valid_from,
"product_type": product_type_instance,
},
)
self._add_to_log_list(price_instance, created)
Expand All @@ -61,35 +55,18 @@ def _update_or_create_price(self, price: api_models.Price):
def _handle_product_types(self, product_types: list[api_models.ProductType]):
for product_type in product_types:

if product_type.current_price:
price_instance = self._update_or_create_price(
product_type.current_price
)
self._handle_options(product_type.current_price.options, price_instance)
else:
price_instance = None

product_type_instance, created = ProductType.objects.update_or_create(
uuid=product_type.id,
defaults={
"name": product_type.name,
"upl_uri": product_type.upl_uri,
"upl_name": product_type.upl_name,
"open_producten_price": price_instance,
},
)
self._add_to_log_list(product_type_instance, created)

def _delete_non_updated_objects(self):
objects_to_be_deleted = ProductType.objects.exclude(
Q(uuid__in=self.product_types) | Q(uuid=True)
)

for obj in objects_to_be_deleted:
try:
obj.delete()
self.deleted_count += 1
except ProtectedError:
obj.is_deleted = True
obj.save()
self.soft_deleted_count += 1
if product_type.current_price:
price_instance = self._update_or_create_price(
product_type.current_price, product_type_instance
)
self._handle_options(product_type.current_price.options, price_instance)
24 changes: 24 additions & 0 deletions src/openforms/contrib/open_producten/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import factory.fuzzy

from openforms.products.tests.factories import ProductFactory

from ..models import Price, PriceOption


class PriceFactory(factory.django.DjangoModelFactory):
uuid = factory.Faker("uuid4")
valid_from = factory.Faker("date")
product_type = factory.SubFactory(ProductFactory)

class Meta:
model = Price


class PriceOptionFactory(factory.django.DjangoModelFactory):
uuid = factory.Faker("uuid4")
description = factory.Faker("sentence")
amount = factory.fuzzy.FuzzyDecimal(1, 10)
price = factory.SubFactory(PriceFactory)

class Meta:
model = PriceOption
31 changes: 31 additions & 0 deletions src/openforms/contrib/open_producten/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import date

from ..api_models import Price, PriceOption, ProductType


def create_price_option(uuid):
return PriceOption(
id=uuid,
amount="10.00",
description="description",
)


def create_price(price_uuid, option_uuid):
return Price(
id=price_uuid,
valid_from=date.today(),
options=[
create_price_option(option_uuid).__dict__
], # __dict__ is needed for zgw_consumers.api_models.Model _type_cast
)


def create_product_type(product_uuid, price_uuid, option_uuid):
return ProductType(
id=product_uuid,
current_price=create_price(price_uuid, option_uuid),
upl_uri="http://standaarden.overheid.nl/owms/terms/AangifteVertrekBuitenland",
upl_name="aangifte vertrek buitenland",
name="aangifte vertrek buitenland",
)
Loading

0 comments on commit 0817671

Please sign in to comment.