From 7039eb10a6d52a24fc82c23106f5ad86f1b170f6 Mon Sep 17 00:00:00 2001 From: NiedielnitsevIvan <81557788+NiedielnitsevIvan@users.noreply.github.com> Date: Mon, 19 Dec 2022 13:22:21 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20[EDXOLDMNG-236]=20certificate=20config?= =?UTF-8?q?=20for=20old=20courses=20is=20taken=20fro=E2=80=A6=20(#2466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- lms/djangoapps/certificates/utils.py | 21 +++++++++++++ lms/djangoapps/certificates/views/webview.py | 32 ++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lms/djangoapps/certificates/utils.py b/lms/djangoapps/certificates/utils.py index 725c54ac09bc..bba617afe942 100644 --- a/lms/djangoapps/certificates/utils.py +++ b/lms/djangoapps/certificates/utils.py @@ -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 @@ -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, + ) diff --git a/lms/djangoapps/certificates/views/webview.py b/lms/djangoapps/certificates/views/webview.py index b859cfe92ca2..7f1d59d8d29f 100644 --- a/lms/djangoapps/certificates/views/webview.py +++ b/lms/djangoapps/certificates/views/webview.py @@ -4,6 +4,7 @@ import logging +from typing import Dict, List, Union import urllib from datetime import datetime from uuid import uuid4 @@ -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 ) @@ -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) @@ -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.