Skip to content
Snippets Groups Projects
Commit 47f1a9ef authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Merge branch 'GREG-126-api-expand-ou-objects' into 'master'

Greg 126 api expand ou objects

See merge request !175
parents b22114dc fea43f21
No related branches found
No related tags found
1 merge request!175Greg 126 api expand ou objects
Pipeline #101295 passed
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 rest_framework.fields import IntegerField
from greg.api.serializers.organizational_unit import OrganizationalUnitSerializer
from greg.models import Role, RoleType
class RoleSerializer(serializers.ModelSerializer):
type = serializers.SlugRelatedField(
queryset=RoleType.objects.all(), slug_field="identifier"
)
orgunit = OrganizationalUnitSerializer()
class Meta:
model = Role
fields = [
"id",
"start_date",
"end_date",
"sponsor",
"orgunit",
"created",
"updated",
"type",
]
class RoleWriteSerializer(RoleSerializer):
"""
Serializer for use with GET.
When doing GET we want the complete orgunit object, not just the id.
"""
orgunit = IntegerField(source="orgunit_id") # type: ignore
......@@ -8,9 +8,9 @@ from greg.api.filters import PersonFilter, RoleFilter, IdentityFilter
from greg.api.pagination import PrimaryKeyCursorPagination
from greg.api.serializers.person import (
PersonSerializer,
RoleSerializer,
IdentitySerializer,
)
from greg.api.serializers.role import RoleSerializer, RoleWriteSerializer
from greg.models import Person, Role, Identity
......@@ -49,12 +49,21 @@ class RoleViewSet(viewsets.ModelViewSet):
"""Person role API"""
queryset = Role.objects.all().order_by("id")
serializer_class = RoleSerializer
pagination_class = PrimaryKeyCursorPagination
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = RoleFilter
lookup_field = "id"
def get_serializer_class(self):
"""
Fetch different serializer depending on http method.
To return the complete orgunit object for GET we use a different serializer.
"""
if self.request.method in ("POST", "PATCH"):
return RoleWriteSerializer
return RoleSerializer
def get_queryset(self):
qs = self.queryset
if not self.kwargs:
......
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