Skip to content

Commit

Permalink
refactor: [AXM-1169] dynamically add fields to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo-kh committed Dec 9, 2024
1 parent 82bddb2 commit fcc6fc0
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions credentials/apps/verifiable_credentials/composition/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

from rest_framework import serializers
from django.contrib.contenttypes.models import ContentType


class EducationalOccupationalProgramSchema(serializers.Serializer): # pylint: disable=abstract-method
Expand Down Expand Up @@ -46,20 +45,19 @@ class EducationalOccupationalCredentialSchema(serializers.Serializer): # pylint
id = serializers.CharField(default=TYPE, help_text="https://schema.org/EducationalOccupationalCredential")
name = serializers.CharField(source="user_credential.credential.title")
description = serializers.CharField(source="user_credential.uuid")
program = EducationalOccupationalProgramSchema(source="*")
course = EducationalOccupationalCourseSchema(source="*")

def to_representation(self, instance):
"""
Dynamically remove fields based on the type before serialization.
Dynamically add fields based on the type.
"""
program_content_type = ContentType.objects.get(app_label="credentials", model="programcertificate")
if instance.user_credential.credential_content_type == program_content_type:
self.fields.pop("course", None)
else:
self.fields.pop("program", None)
representation = super().to_representation(instance)

return super().to_representation(instance)
if instance.user_credential.credential_content_type.model == "programcertificate":
representation["program"] = EducationalOccupationalProgramSchema(instance).data
elif instance.user_credential.credential_content_type.model == "coursecertificate":
representation["course"] = EducationalOccupationalCourseSchema(instance).data

return representation

class Meta:
read_only_fields = "__all__"
Expand Down

0 comments on commit fcc6fc0

Please sign in to comment.