Skip to content

Commit

Permalink
feat: [AXM-1169] adapt VC composition to course credentials (#184)
Browse files Browse the repository at this point in the history
* feat: [AXM-1169] adapt VC composition to course credentials

* refactor: [AXM-1169] dynamically add fields to serializer
  • Loading branch information
kyrylo-kh authored Dec 9, 2024
1 parent 8dadce3 commit e9e5ab1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
29 changes: 28 additions & 1 deletion credentials/apps/verifiable_credentials/composition/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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__"
Expand Down
6 changes: 6 additions & 0 deletions credentials/apps/verifiable_credentials/issuance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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", "")):
Expand Down

0 comments on commit e9e5ab1

Please sign in to comment.