Skip to content

Commit

Permalink
refactor: [AXM-1183] move credential types to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo-kh committed Dec 17, 2024
1 parent c757858 commit a4d47e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from rest_framework import serializers

from ..constants import CredentialsType


class EducationalOccupationalProgramSchema(serializers.Serializer): # pylint: disable=abstract-method
"""
Expand Down Expand Up @@ -52,9 +54,9 @@ def to_representation(self, instance):
"""
representation = super().to_representation(instance)

if instance.user_credential.credential_content_type.model == "programcertificate":
if instance.user_credential.credential_content_type.model == CredentialsType.PROGRAM:
representation["program"] = EducationalOccupationalProgramSchema(instance).data
elif instance.user_credential.credential_content_type.model == "coursecertificate":
elif instance.user_credential.credential_content_type.model == CredentialsType.COURSE:
representation["course"] = EducationalOccupationalCourseSchema(instance).data

return representation
Expand Down
6 changes: 6 additions & 0 deletions credentials/apps/verifiable_credentials/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CredentialsType:
"""
Enum to define the type of credentials.
"""
PROGRAM = "programcertificate"
COURSE = "coursecertificate"
17 changes: 9 additions & 8 deletions credentials/apps/verifiable_credentials/issuance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..composition.utils import get_data_model, get_data_models
from ..settings import vc_settings
from ..storages.utils import get_storage
from ..constants import CredentialsType


User = get_user_model()
Expand Down Expand Up @@ -107,8 +108,8 @@ def credential_verbose_type(self):
Map internal credential types to verbose labels (source models do not provide those).
"""
contenttype_to_verbose_name = {
"programcertificate": _("program certificate"),
"coursecertificate": _("course certificate"),
CredentialsType.PROGRAM: _("program certificate"),
CredentialsType.COURSE: _("course certificate"),
}
return contenttype_to_verbose_name.get(self.credential_content_type)

Expand All @@ -121,10 +122,10 @@ def credential_name(self):
return credential_title

contenttype_to_name = {
"programcertificate": _("program certificate for passing a program {program_title}").format(
CredentialsType.PROGRAM: _("program certificate for passing a program {program_title}").format(
program_title=getattr(self.program, "title", "")
),
"coursecertificate": self.credential_verbose_type,
CredentialsType.COURSE: self.credential_verbose_type,
}
return capitalize_first(contenttype_to_name.get(self.credential_content_type))

Expand All @@ -133,7 +134,7 @@ def credential_description(self):
"""
Verifiable credential achievement description resolution.
"""
if self.credential_content_type == "programcertificate":
if self.credential_content_type == CredentialsType.PROGRAM:
effort_portion = (
_(", with total {hours_of_effort} Hours of effort required to complete it").format(
hours_of_effort=self.program.total_hours_of_effort
Expand All @@ -152,7 +153,7 @@ def credential_description(self):
course_count=self.program.course_runs.count(),
effort_info=effort_portion,
)
elif self.credential_content_type == "coursecertificate":
elif self.credential_content_type == CredentialsType.COURSE:
description = _("{credential_type} is granted on course {course_title} completion offered by {organization}, in collaboration with {platform_name}").format(
credential_type=self.credential_verbose_type,
course_title=getattr(self.course, "title", ""),
Expand All @@ -166,7 +167,7 @@ def credential_narrative(self):
"""
Verifiable credential achievement criteria narrative.
"""
if self.credential_content_type == "programcertificate":
if self.credential_content_type == CredentialsType.PROGRAM:
narrative = _(
"{recipient_fullname} successfully completed all courses and received passing grades for a Professional Certificate in {program_title} a program offered by {organizations}, in collaboration with {platform_name}." # pylint: disable=line-too-long
).format(
Expand All @@ -175,7 +176,7 @@ def credential_narrative(self):
organizations=", ".join(list(self.program.authoring_organizations.values_list("name", flat=True))),
platform_name=self.platform_name,
)
elif self.credential_content_type == "coursecertificate":
elif self.credential_content_type == CredentialsType.COURSE:
narrative = _(
"{recipient_fullname} successfully completed a course and received a passing grade for a Course Certificate in {course_title} a course offered by {organization}, in collaboration with {platform_name}. " # pylint: disable=line-too-long
).format(
Expand Down

0 comments on commit a4d47e1

Please sign in to comment.