-
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
7 changed files
with
518 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,18 @@ | ||
from django.core.validators import ValidationError | ||
|
||
from rest_framework import serializers | ||
|
||
from open_producten.producten.models import Product | ||
from open_producten.producten.serializers.validators import BsnOrKvkValidator | ||
from open_producten.producttypen.models import ProductType | ||
from open_producten.producttypen.serializers.onderwerp import ( | ||
SimpleProductTypeSerializer, | ||
) | ||
from open_producten.utils.serializers import model_to_dict_with_related_ids | ||
|
||
|
||
class BaseProductSerializer(serializers.ModelSerializer): | ||
def validate(self, attrs): | ||
|
||
if self.partial: | ||
all_attrs = model_to_dict_with_related_ids(self.instance) | attrs | ||
else: | ||
all_attrs = attrs | ||
|
||
instance = Product(**all_attrs) | ||
|
||
try: | ||
instance.clean() | ||
except ValidationError as e: | ||
raise serializers.ValidationError({"bsn_or_kvk": e.message}) | ||
from open_producten.producttypen.serializers.thema import NestedProductTypeSerializer | ||
|
||
return attrs | ||
|
||
|
||
class ProductSerializer(BaseProductSerializer): | ||
product_type = SimpleProductTypeSerializer(read_only=True) | ||
class ProductSerializer(serializers.ModelSerializer): | ||
product_type = NestedProductTypeSerializer(read_only=True) | ||
product_type_id = serializers.PrimaryKeyRelatedField( | ||
write_only=True, queryset=ProductType.objects.all(), source="product_type" | ||
) | ||
|
||
class Meta: | ||
model = Product | ||
fields = "__all__" | ||
|
||
|
||
class ProductUpdateSerializer(BaseProductSerializer): | ||
class Meta: | ||
model = Product | ||
exclude = ("product_type",) | ||
validators = [BsnOrKvkValidator()] |
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,18 @@ | ||
from django.core.exceptions import ValidationError | ||
|
||
from rest_framework import serializers | ||
|
||
from open_producten.producten.models.product import validate_bsn_or_kvk | ||
from open_producten.utils.serializers import get_from_serializer_data_or_instance | ||
|
||
|
||
class BsnOrKvkValidator: | ||
requires_context = True | ||
|
||
def __call__(self, value, serializer): | ||
bsn = get_from_serializer_data_or_instance("bsn", value, serializer) | ||
kvk = get_from_serializer_data_or_instance("kvk", value, serializer) | ||
try: | ||
validate_bsn_or_kvk(bsn, kvk) | ||
except ValidationError as e: | ||
raise serializers.ValidationError({"bsn_or_kvk": e.message}) |
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 |
---|---|---|
@@ -1,20 +1,13 @@ | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
|
||
from open_producten.producten.models import Product | ||
from open_producten.producten.serializers.product import ( | ||
ProductSerializer, | ||
ProductUpdateSerializer, | ||
) | ||
from open_producten.producten.serializers.product import ProductSerializer | ||
from open_producten.utils.views import OrderedModelViewSet | ||
|
||
|
||
class ProductViewSet(OrderedModelViewSet): | ||
queryset = Product.objects.all() | ||
lookup_url_field = "id" | ||
serializer_class = ProductSerializer | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_fields = ["gepubliceerd"] | ||
|
||
def get_serializer_class(self): | ||
if self.action in ("update", "partial_update"): | ||
return ProductUpdateSerializer | ||
return ProductSerializer |
Oops, something went wrong.