-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
964 additions
and
48 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,11 @@ | ||
from django.contrib import admin | ||
|
||
from open_producten.logging.admin_tools import AuditLogInlineformset | ||
from open_producten.producten.models import ProductEigenschap | ||
|
||
|
||
class ProductEigenschapInline(admin.TabularInline): | ||
formset = AuditLogInlineformset | ||
model = ProductEigenschap | ||
extra = 1 | ||
ordering = ("pk",) |
67 changes: 67 additions & 0 deletions
67
src/open_producten/producten/migrations/0005_producteigenschap.py
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,67 @@ | ||
# Generated by Django 4.2.17 on 2025-03-06 15:38 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("producttypen", "0010_eigenschap"), | ||
("producten", "0004_product_frequentie_product_prijs"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="ProductEigenschap", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
( | ||
"waarde", | ||
models.CharField( | ||
help_text="De waarde van de eigenschap.", | ||
max_length=255, | ||
validators=[ | ||
django.core.validators.RegexValidator("^[^:\\[\\]]+$") | ||
], | ||
verbose_name="waarde", | ||
), | ||
), | ||
( | ||
"eigenschap", | ||
models.ForeignKey( | ||
help_text="Het eigenschap waarbij deze waarde hoort.", | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="waardes", | ||
to="producttypen.eigenschap", | ||
verbose_name="producttype", | ||
), | ||
), | ||
( | ||
"product", | ||
models.ForeignKey( | ||
help_text="Het product waarbij deze eigenschap hoort.", | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="eigenschappen", | ||
to="producten.product", | ||
verbose_name="producttype", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "product eigenschap", | ||
"verbose_name_plural": "product eigenschappen", | ||
"unique_together": {("product", "eigenschap")}, | ||
}, | ||
), | ||
] |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .product import Product | ||
from .product_eigenschap import ProductEigenschap | ||
|
||
__all__ = ["Product"] | ||
__all__ = ["Product", "ProductEigenschap"] |
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,62 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.core.validators import RegexValidator | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from open_producten.producten.models import Product | ||
from open_producten.producttypen.models import Eigenschap | ||
from open_producten.utils.models import BaseModel | ||
|
||
|
||
class ProductEigenschap(BaseModel): | ||
|
||
waarde = models.CharField( | ||
verbose_name=_("waarde"), | ||
max_length=255, | ||
help_text=_("De waarde van de eigenschap."), | ||
validators=[RegexValidator(r"^[^:\[\]]+$")], | ||
) | ||
|
||
eigenschap = models.ForeignKey( | ||
Eigenschap, | ||
verbose_name=_("producttype"), | ||
on_delete=models.CASCADE, | ||
related_name="waardes", | ||
help_text=_("Het eigenschap waarbij deze waarde hoort."), | ||
) | ||
|
||
product = models.ForeignKey( | ||
Product, | ||
verbose_name=_("producttype"), | ||
on_delete=models.CASCADE, | ||
related_name="eigenschappen", | ||
help_text=_("Het product waarbij deze eigenschap hoort."), | ||
) | ||
|
||
@property | ||
def naam(self): | ||
return self.eigenschap.naam | ||
|
||
class Meta: | ||
verbose_name = _("product eigenschap") | ||
verbose_name_plural = _("product eigenschappen") | ||
unique_together = (("product", "eigenschap"),) | ||
|
||
def clean(self): | ||
validate_eigenschap_part_of_producttype( | ||
self.eigenschap, self.waarde, self.product | ||
) | ||
|
||
def __str__(self): | ||
return f"{self.eigenschap.naam} {self.waarde} {self.product.naam}" | ||
|
||
|
||
def validate_eigenschap_part_of_producttype(eigenschap, waarde, product): | ||
if eigenschap.product_type != product.product_type: | ||
raise ValidationError( | ||
{ | ||
"eigenschap": _( | ||
"eigenschap {}: {} is niet onderdeel van het producttype {}" | ||
).format(eigenschap.naam, waarde, product.product_type.naam) | ||
} | ||
) |
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
20 changes: 20 additions & 0 deletions
20
src/open_producten/producten/serializers/product_eigenschap.py
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,20 @@ | ||
from rest_framework import serializers | ||
|
||
from open_producten.producten.models import ProductEigenschap | ||
from open_producten.producten.serializers.validators import ProductEigenschapValidator | ||
|
||
|
||
class NestedProductEigenschapSerializer(serializers.ModelSerializer): | ||
naam = serializers.CharField() | ||
|
||
class Meta: | ||
model = ProductEigenschap | ||
fields = ["waarde", "naam"] | ||
|
||
|
||
class ProductEigenschapSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = ProductEigenschap | ||
fields = ["eigenschap", "product", "waarde"] | ||
validators = [ProductEigenschapValidator()] |
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
Oops, something went wrong.