Skip to content

Commit

Permalink
feat: [EDXOLDMNG-236] certificate config for old courses is taken fro… (
Browse files Browse the repository at this point in the history
#2466)

* feat: [EDXOLDMNG-236] certificate config for old courses is taken from credentials

* style: [EDXOLDMNG-236] disable pylint issues

* refactor: [EDXOLDMNG-236] refactored the logic for getting the certificate configuration

* refactor: [EDXOLDMNG-236] refactored the logic for getting the certificate configuration

* fix: [EDXOLDMNG-236] remove extra try except
  • Loading branch information
NiedielnitsevIvan authored Dec 19, 2022
1 parent 6ae1371 commit 7039eb1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lms/djangoapps/certificates/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
Certificates utilities
"""
from datetime import datetime
from typing import Optional
import logging

from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.urls import reverse
from eventtracking import tracker
from opaque_keys.edx.keys import CourseKey
from pytz import utc
from requests.models import Response

from common.djangoapps.student import models_api as student_api
from lms.djangoapps.certificates.data import CertificateStatuses
from lms.djangoapps.certificates.models import GeneratedCertificate
from openedx.core.djangoapps.content.course_overviews.api import get_course_overview_or_none
from openedx.core.djangoapps.credentials.models import CredentialsApiConfig
from openedx.core.djangoapps.credentials.utils import get_credentials_api_base_url, get_credentials_api_client
from openedx.core.lib.edx_api_utils import get_api_data
from openedx.features.name_affirmation_api.utils import get_name_affirmation_service
from xmodule.data import CertificatesDisplayBehaviors # lint-amnesty, pylint: disable=wrong-import-order

Expand Down Expand Up @@ -250,3 +256,18 @@ def get_preferred_certificate_name(user):
name_to_use = ''

return name_to_use


def get_certificate_configuration_from_credentials(course_id: str, mode: str) -> Optional[Response]:
"""
Makes a request to the credentials service to get a certificate configuration of the course.
"""
credentials_client = get_credentials_api_client(User.objects.get(username=settings.CREDENTIALS_SERVICE_USERNAME))
return get_api_data(
api_config=CredentialsApiConfig,
resource='course_certificates',
api_client=credentials_client,
base_api_url=get_credentials_api_base_url(),
querystring={'course_id': course_id, 'certificate_type': mode},
traverse_pagination=False,
)
32 changes: 32 additions & 0 deletions lms/djangoapps/certificates/views/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import logging
from typing import Dict, List, Union
import urllib
from datetime import datetime
from uuid import uuid4
Expand Down Expand Up @@ -47,6 +48,7 @@
from lms.djangoapps.certificates.permissions import PREVIEW_CERTIFICATES
from lms.djangoapps.certificates.utils import (
emit_certificate_event,
get_certificate_configuration_from_credentials,
get_certificate_url,
get_preferred_certificate_name
)
Expand Down Expand Up @@ -575,6 +577,13 @@ def render_html_view(request, course_id, certificate=None): # pylint: disable=t
)
return _render_invalid_certificate(request, course_id, platform_name, configuration)

if course_key.deprecated:
active_configuration = _update_certificate_configuration_from_credentials(
configuration=active_configuration,
course_id=str(course.id),
mode=preview_mode or user_certificate.mode,
)

# Get data from Discovery service that will be necessary for rendering this Certificate.
catalog_data = _get_catalog_data_for_course(course_key)

Expand Down Expand Up @@ -669,6 +678,29 @@ def render_html_view(request, course_id, certificate=None): # pylint: disable=t
return response


def _update_certificate_configuration_from_credentials(
configuration: Dict[str, Union[str, List[Dict[str, str]]]],
course_id: str,
mode: str
) -> Dict[str, Union[str, List[Dict[str, str]]]]:
"""
Populates certificate configuration from credentials service.
If config not found in credentials returns configuration without changes.
"""
credentials_certificate_conf = get_certificate_configuration_from_credentials(course_id, mode)
if credentials_certificate_conf:
if title := credentials_certificate_conf.get('title'):
configuration['course_title'] = title
if is_active := credentials_certificate_conf.get('is_active'):
configuration['is_active'] = is_active
if signatories := credentials_certificate_conf.get('signatories', []):
for signatory in signatories:
signatory['signature_image_path'] = signatory.pop('image') or ''
configuration['signatories'] = signatories
return configuration


def _get_catalog_data_for_course(course_key):
"""
Retrieve data from the Discovery service necessary for rendering a certificate for a specific course.
Expand Down

0 comments on commit 7039eb1

Please sign in to comment.