Skip to content

Commit

Permalink
Add location models to product type serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Oct 8, 2024
1 parent 4b664d1 commit 5beac4f
Show file tree
Hide file tree
Showing 5 changed files with 1,338 additions and 99 deletions.
10 changes: 9 additions & 1 deletion src/open_producten/products/tests/api/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ def product_to_dict(product):

product_dict["product_type"] = model_to_dict_with_id(
product.product_type,
exclude=("categories", "conditions", "tags", "related_product_types"),
exclude=(
"categories",
"conditions",
"tags",
"related_product_types",
"contacts",
"locations",
"organisations",
),
)
product_dict["product_type"][
"uniform_product_name"
Expand Down
10 changes: 9 additions & 1 deletion src/open_producten/producttypes/serializers/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ class SimpleProductTypeSerializer(serializers.ModelSerializer):

class Meta:
model = ProductType
exclude = ("categories", "conditions", "tags", "related_product_types")
exclude = (
"categories",
"conditions",
"tags",
"related_product_types",
"organisations",
"locations",
"contacts",
)


class CategorySerializer(serializers.ModelSerializer):
Expand Down
78 changes: 75 additions & 3 deletions src/open_producten/producttypes/serializers/producttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from rest_framework import serializers

from open_producten.locations.models import Contact, Location, Organisation
from open_producten.locations.serializers.location import (
ContactSerializer,
LocationSerializer,
OrganisationSerializer,
)
from open_producten.utils.serializers import build_array_duplicates_error_message

from ..models import Category, Condition, ProductType, Tag, UniformProductName
Expand Down Expand Up @@ -57,6 +63,33 @@ class ProductTypeSerializer(serializers.ModelSerializer):
source="categories",
)

locations = LocationSerializer(many=True, read_only=True)
location_ids = serializers.PrimaryKeyRelatedField(
many=True,
write_only=True,
queryset=Location.objects.all(),
default=[],
source="locations",
)

organisations = OrganisationSerializer(many=True, read_only=True)
organisation_ids = serializers.PrimaryKeyRelatedField(
many=True,
write_only=True,
queryset=Organisation.objects.all(),
default=[],
source="organisations",
)

contacts = ContactSerializer(many=True, read_only=True)
contact_ids = serializers.PrimaryKeyRelatedField(
many=True,
write_only=True,
queryset=Contact.objects.all(),
default=[],
source="contacts",
)

questions = QuestionSerializer(many=True, read_only=True)
fields = FieldSerializer(many=True, read_only=True)
prices = PriceSerializer(many=True, read_only=True)
Expand All @@ -73,7 +106,15 @@ def validate_category_ids(self, category_ids):
return category_ids

def _handle_relations(
self, instance, related_product_types, categories, tags, conditions
self,
instance,
related_product_types,
categories,
tags,
conditions,
locations,
organisations,
contacts,
):
errors = dict()
if related_product_types is not None:
Expand All @@ -90,6 +131,17 @@ def _handle_relations(
if conditions is not None:
build_array_duplicates_error_message(conditions, "condition_ids", errors)
instance.conditions.set(conditions)
if locations is not None:
build_array_duplicates_error_message(locations, "location_ids", errors)
instance.locations.set(locations)
if organisations is not None:
build_array_duplicates_error_message(
organisations, "organisation_ids", errors
)
instance.organisations.set(organisations)
if contacts is not None:
build_array_duplicates_error_message(contacts, "contact_ids", errors)
instance.contacts.set(contacts)

if errors:
raise serializers.ValidationError(errors)
Expand All @@ -100,11 +152,21 @@ def create(self, validated_data):
categories = validated_data.pop("categories")
conditions = validated_data.pop("conditions")
tags = validated_data.pop("tags")
locations = validated_data.pop("locations")
organisations = validated_data.pop("organisations")
contacts = validated_data.pop("contacts")

product_type = ProductType.objects.create(**validated_data)

self._handle_relations(
product_type, related_product_types, categories, tags, conditions
product_type,
related_product_types,
categories,
tags,
conditions,
locations,
organisations,
contacts,
)
product_type.save()

Expand All @@ -116,10 +178,20 @@ def update(self, instance, validated_data):
categories = validated_data.pop("categories", None)
conditions = validated_data.pop("conditions", None)
tags = validated_data.pop("tags", None)
locations = validated_data.pop("locations", None)
organisations = validated_data.pop("organisations", None)
contacts = validated_data.pop("contacts", None)

instance = super().update(instance, validated_data)
self._handle_relations(
instance, related_product_types, categories, tags, conditions
instance,
related_product_types,
categories,
tags,
conditions,
locations,
organisations,
contacts,
)

instance.save()
Expand Down
41 changes: 41 additions & 0 deletions src/open_producten/producttypes/tests/api/test_producttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APIClient

from open_producten.locations.tests.factories import (
ContactFactory,
LocationFactory,
OrganisationFactory,
)
from open_producten.producttypes.models import Link, ProductType, Tag
from open_producten.producttypes.tests.factories import (
CategoryFactory,
Expand Down Expand Up @@ -155,6 +160,42 @@ def test_create_product_type_with_condition(self):
ProductType.objects.first().conditions.first().name, condition.name
)

def test_create_product_type_with_location(self):
location = LocationFactory.create()

data = self.data | {"location_ids": [location.id]}
response = self.post(data)

self.assertEqual(response.status_code, 201)
self.assertEqual(ProductType.objects.count(), 1)
self.assertEqual(
ProductType.objects.first().locations.first().name, location.name
)

def test_create_product_type_with_organisation(self):
organisation = OrganisationFactory.create()

data = self.data | {"organisation_ids": [organisation.id]}
response = self.post(data)

self.assertEqual(response.status_code, 201)
self.assertEqual(ProductType.objects.count(), 1)
self.assertEqual(
ProductType.objects.first().organisations.first().name, organisation.name
)

def test_create_product_type_with_contact(self):
contact = ContactFactory.create()

data = self.data | {"contact_ids": [contact.id]}
response = self.post(data)

self.assertEqual(response.status_code, 201)
self.assertEqual(ProductType.objects.count(), 1)
self.assertEqual(
ProductType.objects.first().contacts.first().first_name, contact.first_name
)

def test_create_product_type_with_duplicate_ids_returns_error(self):
tag = TagFactory.create()
condition = ConditionFactory.create()
Expand Down
Loading

0 comments on commit 5beac4f

Please sign in to comment.