1212from django .utils .translation import gettext_lazy as _
1313from django_extensions .db .models import TimeStampedModel
1414
15+ from credentials .apps .catalog .models import Course
1516from credentials .apps .credentials .models import UserCredential
1617from credentials .apps .verifiable_credentials .utils import capitalize_first
1718
1819from ..composition .utils import get_data_model , get_data_models
1920from ..settings import vc_settings
2021from ..storages .utils import get_storage
22+ from ..constants import CredentialsType
2123
2224
2325User = get_user_model ()
@@ -106,8 +108,8 @@ def credential_verbose_type(self):
106108 Map internal credential types to verbose labels (source models do not provide those).
107109 """
108110 contenttype_to_verbose_name = {
109- "programcertificate" : _ ("program certificate" ),
110- "coursecertificate" : _ ("course certificate" ),
111+ CredentialsType . PROGRAM : _ ("program certificate" ),
112+ CredentialsType . COURSE : _ ("course certificate" ),
111113 }
112114 return contenttype_to_verbose_name .get (self .credential_content_type )
113115
@@ -120,10 +122,10 @@ def credential_name(self):
120122 return credential_title
121123
122124 contenttype_to_name = {
123- "programcertificate" : _ ("program certificate for passing a program {program_title}" ).format (
125+ CredentialsType . PROGRAM : _ ("program certificate for passing a program {program_title}" ).format (
124126 program_title = getattr (self .program , "title" , "" )
125127 ),
126- "coursecertificate" : self .credential_verbose_type ,
128+ CredentialsType . COURSE : self .credential_verbose_type ,
127129 }
128130 return capitalize_first (contenttype_to_name .get (self .credential_content_type ))
129131
@@ -132,48 +134,58 @@ def credential_description(self):
132134 """
133135 Verifiable credential achievement description resolution.
134136 """
135- effort_portion = (
136- _ (", with total {hours_of_effort} Hours of effort required to complete it" ).format (
137- hours_of_effort = self .program .total_hours_of_effort
137+ if self .credential_content_type == CredentialsType .PROGRAM :
138+ effort_portion = (
139+ _ (", with total {hours_of_effort} Hours of effort required to complete it" ).format (
140+ hours_of_effort = self .program .total_hours_of_effort
141+ )
142+ if self .program .total_hours_of_effort
143+ else ""
138144 )
139- if self .program .total_hours_of_effort
140- else ""
141- )
142145
143- program_certificate_description = _ (
144- "{credential_type} is granted on program {program_title} completion offered by {organizations}, in collaboration with {platform_name}. The {program_title} program includes {course_count} course(s){effort_info}." # pylint: disable=line-too-long
145- ).format (
146- credential_type = self .credential_verbose_type ,
147- program_title = self .program .title ,
148- organizations = ", " .join (list (self .program .authoring_organizations .values_list ("name" , flat = True ))),
149- platform_name = self .platform_name ,
150- course_count = self .program .course_runs .count (),
151- effort_info = effort_portion ,
152- )
153- type_to_description = {
154- "programcertificate" : program_certificate_description ,
155- "coursecertificate" : "" ,
156- }
157- return capitalize_first (type_to_description .get (self .credential_content_type ))
146+ description = _ (
147+ "{credential_type} is granted on program {program_title} completion offered by {organizations}, in collaboration with {platform_name}. The {program_title} program includes {course_count} course(s){effort_info}." # pylint: disable=line-too-long
148+ ).format (
149+ credential_type = self .credential_verbose_type ,
150+ program_title = self .program .title ,
151+ organizations = ", " .join (list (self .program .authoring_organizations .values_list ("name" , flat = True ))),
152+ platform_name = self .platform_name ,
153+ course_count = self .program .course_runs .count (),
154+ effort_info = effort_portion ,
155+ )
156+ elif self .credential_content_type == CredentialsType .COURSE :
157+ description = _ ("{credential_type} is granted on course {course_title} completion offered by {organization}, in collaboration with {platform_name}" ).format (
158+ credential_type = self .credential_verbose_type ,
159+ course_title = getattr (self .course , "title" , "" ),
160+ platform_name = self .platform_name ,
161+ organization = self .user_credential .credential .course_key .org ,
162+ )
163+ return capitalize_first (description )
158164
159165 @property
160166 def credential_narrative (self ):
161167 """
162168 Verifiable credential achievement criteria narrative.
163169 """
164- program_certificate_narrative = _ (
165- "{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
166- ).format (
167- recipient_fullname = self .subject_fullname or _ ("recipient" ),
168- program_title = self .program .title ,
169- organizations = ", " .join (list (self .program .authoring_organizations .values_list ("name" , flat = True ))),
170- platform_name = self .platform_name ,
171- )
172- type_to_narrative = {
173- "programcertificate" : program_certificate_narrative ,
174- "coursecertificate" : "" ,
175- }
176- return capitalize_first (type_to_narrative .get (self .credential_content_type ))
170+ if self .credential_content_type == CredentialsType .PROGRAM :
171+ narrative = _ (
172+ "{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
173+ ).format (
174+ recipient_fullname = self .subject_fullname or _ ("recipient" ),
175+ program_title = self .program .title ,
176+ organizations = ", " .join (list (self .program .authoring_organizations .values_list ("name" , flat = True ))),
177+ platform_name = self .platform_name ,
178+ )
179+ elif self .credential_content_type == CredentialsType .COURSE :
180+ narrative = _ (
181+ "{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
182+ ).format (
183+ recipient_fullname = self .subject_fullname or _ ("recipient" ),
184+ course_title = getattr (self .course , "title" , "" ),
185+ organization = self .user_credential .credential .course_key .org ,
186+ platform_name = self .platform_name ,
187+ )
188+ return capitalize_first (narrative )
177189
178190 @property
179191 def credential_content_type (self ):
@@ -183,6 +195,11 @@ def credential_content_type(self):
183195 def program (self ):
184196 return getattr (self .user_credential .credential , "program" , None )
185197
198+ @property
199+ def course (self ):
200+ course_id = getattr (self .user_credential .credential , "course_id" , None )
201+ return Course .objects .filter (course_runs__key = course_id ).first ()
202+
186203 @property
187204 def platform_name (self ):
188205 if not (site_configuration := getattr (self .user_credential .credential .site , "siteconfiguration" , "" )):
0 commit comments