Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [AXM-1169] adapt VC composition to course credentials #184

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading