Skip to content
Snippets Groups Projects
Verified Commit 76dadfb6 authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Move serializers to their own files

Done to compartmentalize functionality.
parent 1563e77a
No related branches found
No related tags found
1 merge request!175Greg 126 api expand ou objects
from rest_framework import serializers
from greg.api.serializers.consent_type import ConsentTypeSerializerBrief
from greg.models import Consent
class ConsentSerializerBrief(serializers.ModelSerializer):
type = ConsentTypeSerializerBrief(read_only=True)
class Meta:
model = Consent
fields = ["type", "consent_given_at"]
......@@ -7,3 +7,13 @@ class ConsentTypeSerializer(ModelSerializer):
class Meta:
model = ConsentType
fields = "__all__"
class ConsentTypeSerializerBrief(ModelSerializer):
class Meta:
model = ConsentType
fields = [
"identifier",
"valid_from",
"user_allowed_to_change",
]
from django.core.exceptions import ValidationError
from rest_framework import serializers
from greg.models import Identity
class IdentitySerializer(serializers.ModelSerializer):
class Meta:
model = Identity
fields = "__all__"
def is_duplicate(self, identity_type: str, value: str) -> bool:
# Guests may be verified using another unrecognised identification method,
# which the sponsor is required to elaborate in the value column.
# In this case we cannot assume the union of the identity type and
# the value to be unique across all records.
if identity_type == Identity.IdentityType.OTHER:
return False
# If the type is a specific ID type, then duplicates are not expected
return Identity.objects.filter(type=identity_type).filter(value=value).exists()
def validate(self, attrs):
if self.is_duplicate(attrs["type"], attrs["value"]):
raise ValidationError("Identity already exists")
return attrs
from django.core.exceptions import ValidationError
from rest_framework import serializers
from greg.models import Person, Role, RoleType, Identity, Consent, ConsentType
class RoleSerializer(serializers.ModelSerializer):
type = serializers.SlugRelatedField(
queryset=RoleType.objects.all(), slug_field="identifier"
)
class Meta:
model = Role
fields = [
"id",
"start_date",
"end_date",
"sponsor",
"orgunit",
"created",
"updated",
"type",
]
class IdentitySerializer(serializers.ModelSerializer):
class Meta:
model = Identity
fields = "__all__"
def is_duplicate(self, identity_type: str, value: str) -> bool:
# Guests may be verified using another unrecognised identification method,
# which the sponsor is required to elaborate in the value column.
# In this case we cannot assume the union of the identity type and
# the value to be unique across all records.
if identity_type == Identity.IdentityType.OTHER:
return False
# If the type is a specific ID type, then duplicates are not expected
return Identity.objects.filter(type=identity_type).filter(value=value).exists()
def validate(self, attrs):
if self.is_duplicate(attrs["type"], attrs["value"]):
raise ValidationError("Identity already exists")
return attrs
class ConsentTypeSerializerBrief(serializers.ModelSerializer):
class Meta:
model = ConsentType
fields = [
"identifier",
"valid_from",
"user_allowed_to_change",
]
class ConsentSerializerBrief(serializers.ModelSerializer):
type = ConsentTypeSerializerBrief(read_only=True)
class Meta:
model = Consent
fields = ["type", "consent_given_at"]
from greg.api.serializers.consent import ConsentSerializerBrief
from greg.api.serializers.identity import IdentitySerializer
from greg.api.serializers.role import RoleSerializer
from greg.models import Person
class PersonSerializer(serializers.ModelSerializer):
......
from rest_framework import serializers
from greg.models import Role, RoleType
class RoleSerializer(serializers.ModelSerializer):
type = serializers.SlugRelatedField(
queryset=RoleType.objects.all(), slug_field="identifier"
)
class Meta:
model = Role
fields = [
"id",
"start_date",
"end_date",
"sponsor",
"orgunit",
"created",
"updated",
"type",
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment