Skip to content

Commit

Permalink
Merge pull request #7 from nens/required_fields
Browse files Browse the repository at this point in the history
mixins added for required fields swagger
  • Loading branch information
JJFlorian authored Mar 19, 2024
2 parents 3097d65 + 8773ed2 commit 2076eb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ def to_representation(self, instance):
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
8 changes: 5 additions & 3 deletions gmn/serializers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from rest_framework import serializers

from api.mixins import UrlFieldMixin
from api.mixins import RequiredFieldsMixin, UrlFieldMixin

from . import models as gmn_models


class GMNSerializer(UrlFieldMixin, serializers.ModelSerializer):
class GMNSerializer(UrlFieldMixin, RequiredFieldsMixin, serializers.ModelSerializer):
class Meta:
model = gmn_models.GMN
fields = "__all__"


class MeasuringpointSerializer(UrlFieldMixin, serializers.ModelSerializer):
class MeasuringpointSerializer(
UrlFieldMixin, RequiredFieldsMixin, serializers.ModelSerializer
):
class Meta:
model = gmn_models.Measuringpoint
fields = "__all__"
13 changes: 10 additions & 3 deletions gmw/serializers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from rest_framework import serializers

from api.mixins import UrlFieldMixin
from api.mixins import RequiredFieldsMixin, UrlFieldMixin

from . import models as gmw_models


class GMWSerializer(UrlFieldMixin, serializers.ModelSerializer):
class GMWSerializer(UrlFieldMixin, RequiredFieldsMixin, serializers.ModelSerializer):
class Meta:
model = gmw_models.GMW
fields = "__all__"


class MonitoringTubeSerializer(UrlFieldMixin, serializers.ModelSerializer):
class MonitoringTubeSerializer(
UrlFieldMixin, RequiredFieldsMixin, serializers.ModelSerializer
):
class Meta:
model = gmw_models.MonitoringTube
fields = "__all__"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.required = True

0 comments on commit 2076eb8

Please sign in to comment.