Skip to content

Commit

Permalink
feat: [AXM-1169] adapt VC composition to course credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo-kh committed Dec 9, 2024
1 parent 8dadce3 commit 82bddb2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions credentials/apps/verifiable_credentials/composition/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

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


class EducationalOccupationalProgramSchema(serializers.Serializer): # pylint: disable=abstract-method
Expand All @@ -20,6 +21,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 @@ -31,6 +47,19 @@ class EducationalOccupationalCredentialSchema(serializers.Serializer): # pylint
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.
"""
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)

return super().to_representation(instance)

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 82bddb2

Please sign in to comment.