Skip to content

Commit

Permalink
fixed bugs with mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
JJFlorian committed Apr 16, 2024
1 parent b9598dc commit d78d629
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
31 changes: 13 additions & 18 deletions api/mixins.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
from typing import Any

from django.db.models import QuerySet
from django.http import HttpResponse
from rest_framework import serializers, viewsets
from rest_framework.reverse import reverse


class UserOrganizationMixin(viewsets.ModelViewSet):
def get_user_organisation(self: Any) -> str:
class UserOrganizationMixin:
def get_user_organisation(self):
return self.request.user.userprofile.organisation

def get_queryset(self: Any) -> QuerySet:
def get_queryset(self):
user_organization = self.get_user_organisation()
queryset = super().get_queryset()
return queryset.filter(data_owner=user_organization)


class RequiredFieldsMixin:
def __init__(self: Any, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.required = True


class UrlFieldMixin(serializers.ModelSerializer, RequiredFieldsMixin):
class UrlFieldMixin:
"""
Mixin to add a URL field to serialized data.
"""

def get_url_field(self: Any, obj: Any) -> HttpResponse | None:
def get_url_field(self, obj):
"""
Method to get the URL field.
"""
Expand All @@ -47,7 +35,7 @@ def get_url_field(self: Any, obj: Any) -> HttpResponse | None:
)
return None

def to_representation(self: Any, instance: Any) -> dict[str, str]:
def to_representation(self, instance):
"""
Method to include the URL field in serialized data.
"""
Expand All @@ -56,3 +44,10 @@ def to_representation(self: Any, instance: Any) -> dict[str, str]:
if url_field:
data = {"url": url_field, **data} # Add URL field at the top
return data


class RequiredFieldsMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.required = True
6 changes: 3 additions & 3 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class UserLoggedInSerializer(serializers.Serializer):
kvk = serializers.CharField()


class ImportTaskSerializer(UrlFieldMixin):
class ImportTaskSerializer(UrlFieldMixin, serializers.ModelSerializer):
class Meta:
model = api_models.ImportTask
fields = "__all__"


class UploadTaskSerializer(UrlFieldMixin):
class UploadTaskSerializer(UrlFieldMixin, serializers.ModelSerializer):
class Meta:
model = api_models.UploadTask
fields = "__all__"
fields = "__all__"
4 changes: 2 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def logged_in(self, request: HttpRequest) -> Response:
)


class ImportTaskViewSet(mixins.UserOrganizationMixin):
class ImportTaskViewSet(mixins.UserOrganizationMixin, viewsets.ModelViewSet):
"""
This endpoint handles the import of data from the BRO.
As input, it takes one of the four possible BRO Objects (GMN, GMW, GLD, FRD).
Expand Down Expand Up @@ -173,7 +173,7 @@ def create(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpRespons
)


class UploadTaskViewSet(mixins.UserOrganizationMixin):
class UploadTaskViewSet(mixins.UserOrganizationMixin, viewsets.ModelViewSet):
"""This endpoint handles the upload of data to the BRO.
It takes the registration type, request type, and the sourcedocument data as input.
Expand Down

0 comments on commit d78d629

Please sign in to comment.