Skip to content

Commit

Permalink
fix api workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Aug 9, 2024
1 parent 5cd952f commit 317bb05
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 966 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/generate-postman-collection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ on:
jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ 'v1' ]
# strategy:
# matrix:
# version: [ 'v1' ]

name: Run with version $
name: Run with version v1

steps:
- uses: actions/checkout@v4
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/generate-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ on:
jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ 'v1' ]
# strategy:
# matrix:
# version: [ 'v1' ]

name: Run with version $
name: Run with version v1

steps:
- uses: actions/checkout@v4
Expand All @@ -30,13 +30,13 @@ jobs:
id: vars
run: echo ::set-output name=oas::./src/openapi.yaml
- name: Validate schema
run: openapi-generator-cli validate -i $
run: openapi-generator-cli validate -i ${{ steps.vars.outputs.oas }}
- name: Generate Java client
run: openapi-generator-cli generate -i $ --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
run: openapi-generator-cli generate -i ${{ steps.vars.outputs.oas }} --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/java -g java --additional-properties=dateLibrary=java8,java8=true,optionalProjectFile=false,optionalAssemblyInfo=false
- name: Generate .NET client
run: openapi-generator-cli generate -i $ --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
run: openapi-generator-cli generate -i ${{ steps.vars.outputs.oas }} --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/net -g csharp --additional-properties=optionalProjectFile=false,optionalAssemblyInfo=false
- name: Generate Python client
run: openapi-generator-cli generate -i $ --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
run: openapi-generator-cli generate -i ${{ steps.vars.outputs.oas }} --global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/python -g python --additional-properties=optionalProjectFile=false,optionalAssemblyInfo=false+
8 changes: 4 additions & 4 deletions .github/workflows/lint-oas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ on:
jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ 'v1' ]
# strategy:
# matrix:
# version: [ 'v1' ]

name: Run with version $
name: Run with version v1

steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 6 additions & 0 deletions src/open_producten/producttypes/router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework.routers import DefaultRouter

from open_producten.producttypes.views import (
CategoryQuestionViewSet,
CategoryViewSet,
ProductTypeFieldViewSet,
ProductTypeLinkViewSet,
Expand Down Expand Up @@ -33,3 +34,8 @@
)

ProductTypesRouter.register("categories", CategoryViewSet, basename="category")
ProductTypesRouter.register(
"categories/(?P<id>[^/.]+)/questions",
CategoryQuestionViewSet,
basename="category-question",
)
52 changes: 49 additions & 3 deletions src/open_producten/producttypes/serializers/category.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from rest_framework import serializers

from open_producten.producttypes.models import Category, ProductType

from .children import QuestionSerializer, UpnSerializer


Expand All @@ -14,9 +13,56 @@ class Meta:


class CategorySerializer(serializers.ModelSerializer):
parent_category = serializers.PrimaryKeyRelatedField(
queryset=Category.objects.all(),
allow_null=True,
write_only=True,
)
product_types = SimpleProductTypeSerializer(many=True, read_only=True)
questions = QuestionSerializer(many=True)
questions = QuestionSerializer(many=True, read_only=True)

product_type_ids = serializers.PrimaryKeyRelatedField(
many=True,
queryset=ProductType.objects.all(),
default=[],
write_only=True,
)

class Meta:
model = Category
exclude = ("id", "path", "depth", "numchild")
exclude = ("path", "depth", "numchild")

def create(self, validated_data):
product_types = validated_data.pop("product_type_ids")
parent_category = validated_data.pop("parent_category")

if parent_category:
category = parent_category.add_child(**validated_data)
else:
category = Category.add_root(**validated_data)

category.product_types.set(product_types)
category.save()

return category

def update(self, instance, validated_data):
product_types = validated_data.pop("product_type_ids")
parent_category = validated_data.pop("parent_category")

instance_parent = instance.get_parent()

if parent_category is None and instance_parent is not None:
last_root = Category.get_last_root_node()
instance.move(last_root, "last-sibling")

elif parent_category != instance_parent:
instance.move(parent_category, "last-child")

instance.refresh_from_db()

instance = super().update(instance, validated_data)
instance.product_types.set(product_types)
instance.save()

return instance
36 changes: 18 additions & 18 deletions src/open_producten/producttypes/serializers/producttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Meta:
class ProductTypeSerializer(serializers.ModelSerializer):
tags = TagSerializer(many=True, read_only=True)
tag_ids = serializers.PrimaryKeyRelatedField(
many=True, queryset=Tag.objects.all(), default=[]
many=True, queryset=Tag.objects.all(), default=[], write_only=True
)

related_product_types = serializers.PrimaryKeyRelatedField(
Expand All @@ -33,7 +33,7 @@ class ProductTypeSerializer(serializers.ModelSerializer):
queryset=UniformProductName.objects.all()
)

conditions = ConditionSerializer(many=True, default=[], read_only=True)
conditions = ConditionSerializer(many=True, read_only=True)
condition_ids = serializers.PrimaryKeyRelatedField(
many=True, write_only=True, queryset=Condition.objects.all(), default=[]
)
Expand All @@ -59,36 +59,36 @@ class Meta:
def create(self, validated_data):
uniform_product_name_id = validated_data.pop("uniform_product_name")

related_product_type_ids = validated_data.pop("related_product_types")
category_ids = validated_data.pop("category_ids")
condition_ids = validated_data.pop("condition_ids")
tag_ids = validated_data.pop("tag_ids")
related_product_types = validated_data.pop("related_product_types")
categories = validated_data.pop("category_ids")
conditions = validated_data.pop("condition_ids")
tags = validated_data.pop("tag_ids")

product_type = ProductType.objects.create(
**validated_data, uniform_product_name=uniform_product_name_id
)

product_type.related_product_types.set(related_product_type_ids)
product_type.categories.set(category_ids)
product_type.tags.set(tag_ids)
product_type.conditions.set(condition_ids)
product_type.related_product_types.set(related_product_types)
product_type.categories.set(categories)
product_type.tags.set(tags)
product_type.conditions.set(conditions)

product_type.save()

return product_type

def update(self, instance, validated_data):
related_product_type_ids = validated_data.pop("related_product_types")
category_ids = validated_data.pop("category_ids")
condition_ids = validated_data.pop("condition_ids")
tag_ids = validated_data.pop("tag_ids")
related_product_types = validated_data.pop("related_product_types")
categories = validated_data.pop("category_ids")
conditions = validated_data.pop("condition_ids")
tags = validated_data.pop("tag_ids")

instance = super().update(instance, validated_data)

instance.related_product_types.set(related_product_type_ids)
instance.categories.set(category_ids)
instance.tags.set(tag_ids)
instance.conditions.set(condition_ids)
instance.related_product_types.set(related_product_types)
instance.categories.set(categories)
instance.tags.set(tags)
instance.conditions.set(conditions)

instance.save()

Expand Down
65 changes: 65 additions & 0 deletions src/open_producten/producttypes/tests/test_category_viewset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from rest_framework.test import APITestCase

from .factories import CategoryFactory, ProductTypeFactory
from ..models import Category


class TestCategoryViewSet(APITestCase):

def setUp(self):
self.data = {
"name": "test-category",
"parent_category": None,
}

def test_create_minimal_category(self):
response = self.client.post("/api/v1/categories/", self.data, format="json")

self.assertEqual(response.status_code, 201)
self.assertEqual(Category.objects.count(), 1)

def test_create_category_with_parent(self):
parent = CategoryFactory.create()
data = self.data | {"parent_category": parent.id}

response = self.client.post("/api/v1/categories/", data, format="json")

self.assertEqual(response.status_code, 201)
self.assertEqual(Category.objects.count(), 2)
self.assertEqual(Category.objects.get(id=response.data["id"]).get_parent(), parent)

def test_create_category_with_product_type(self):
product_type = ProductTypeFactory.create()
data = self.data | {"product_type_ids": [product_type.id]}

response = self.client.post("/api/v1/categories/", data, format="json")

self.assertEqual(response.status_code, 201)
self.assertEqual(Category.objects.count(), 1)
self.assertEqual(Category.objects.first().product_types.first(), product_type)

def test_change_parent(self):
new_parent = CategoryFactory.create()
category = CategoryFactory.create()

data = self.data | {"parent_category": new_parent.id}
response = self.client.put(
f"/api/v1/categories/{category.id}/", data, format="json"
)

category.refresh_from_db()

self.assertEqual(response.status_code, 200)
self.assertEqual(category.get_parent(), new_parent)

def test_add_question_to_product_type(self):
category = CategoryFactory.create()

data = {"question": "18?", "answer": "eligible"}
response = self.client.post(
f"/api/v1/categories/{category.id}/questions/", data, format="json"
)

self.assertEqual(response.status_code, 201)
self.assertEqual(Category.objects.count(), 1)
self.assertEqual(category.questions.first().question, "18?")
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


class TestProducttypeViewSet(APITestCase):

def setUp(self):
upn = UniformProductNameFactory.create()

Expand Down
24 changes: 21 additions & 3 deletions src/open_producten/producttypes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
from open_producten.producttypes.serializers.producttype import ProductTypeSerializer


class ProductTypeViewSet(ModelViewSet):
class BaseModelViewSet(ModelViewSet):
http_method_names = ["get", "post", "put", "delete"]


class ProductTypeViewSet(BaseModelViewSet):
queryset = ProductType.objects.all()
serializer_class = ProductTypeSerializer
lookup_field = "id"


class ProductTypeChildViewSet(ModelViewSet):
class ProductTypeChildViewSet(BaseModelViewSet):

def get_queryset(self):
return self.queryset.filter(product_type_id=self.kwargs["id"])
Expand Down Expand Up @@ -57,7 +61,21 @@ class ProductTypeQuestionViewSet(ProductTypeChildViewSet):
serializer_class = QuestionSerializer


class CategoryViewSet(ModelViewSet):
class CategoryViewSet(BaseModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
lookup_field = "id"


class CategoryChildViewSet(BaseModelViewSet):

def get_queryset(self):
return self.queryset.filter(category_id=self.kwargs["id"])

def perform_create(self, serializer):
serializer.save(category=get_object_or_404(Category, id=self.kwargs["id"]))


class CategoryQuestionViewSet(CategoryChildViewSet):
queryset = Question.objects.all()
serializer_class = QuestionSerializer
Loading

0 comments on commit 317bb05

Please sign in to comment.