diff --git a/credentials/apps/verifiable_credentials/composition/schemas.py b/credentials/apps/verifiable_credentials/composition/schemas.py index ade36631c..5756ccd6f 100644 --- a/credentials/apps/verifiable_credentials/composition/schemas.py +++ b/credentials/apps/verifiable_credentials/composition/schemas.py @@ -20,6 +20,21 @@ class Meta: read_only_fields = "__all__" +class EducationalOccupationalCourseSchema(serializers.Serializer): # pylint: disable=abstract-method + """ + Defines Open edX Course. + """ + + TYPE = "Course" + + id = serializers.CharField(default=TYPE, help_text="https://schema.org/Course") + name = serializers.CharField(source="course.title") + courseCode = serializers.CharField(source="user_credential.credential.course_id") + + class Meta: + read_only_fields = "__all__" + + class EducationalOccupationalCredentialSchema(serializers.Serializer): # pylint: disable=abstract-method """ Defines Open edX user credential. @@ -30,7 +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="*") + + def to_representation(self, instance): + """ + Dynamically add fields based on the type. + """ + representation = 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__" diff --git a/credentials/apps/verifiable_credentials/issuance/models.py b/credentials/apps/verifiable_credentials/issuance/models.py index aaea75bb2..10ad3f246 100644 --- a/credentials/apps/verifiable_credentials/issuance/models.py +++ b/credentials/apps/verifiable_credentials/issuance/models.py @@ -12,6 +12,7 @@ from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel +from credentials.apps.catalog.models import Course from credentials.apps.credentials.models import UserCredential from credentials.apps.verifiable_credentials.utils import capitalize_first @@ -183,6 +184,11 @@ def credential_content_type(self): def program(self): return getattr(self.user_credential.credential, "program", None) + @property + def course(self): + course_id = getattr(self.user_credential.credential, "course_id", None) + return Course.objects.filter(course_runs__key=course_id).first() + @property def platform_name(self): if not (site_configuration := getattr(self.user_credential.credential.site, "siteconfiguration", "")):