|
| 1 | +""" |
| 2 | +Pipeline steps for the course enrollment filter. |
| 3 | +""" |
| 4 | +import logging |
| 5 | +from typing import Any |
| 6 | +from requests.exceptions import HTTPError |
| 7 | +from crum import get_current_request |
| 8 | + |
| 9 | +from openedx_filters.filters import PipelineStep |
| 10 | +from openedx_filters.learning.filters import CourseEnrollmentViewStarted |
| 11 | + |
| 12 | +from django.contrib.auth.base_user import AbstractBaseUser |
| 13 | + |
| 14 | +log = logging.getLogger(__name__) |
| 15 | + |
| 16 | +try: |
| 17 | + from openedx.features.enterprise_support.api import ( |
| 18 | + ConsentApiServiceClient, EnterpriseApiException, EnterpriseApiServiceClient |
| 19 | + ) |
| 20 | +except ImportError: |
| 21 | + ConsentApiServiceClient = None |
| 22 | + EnterpriseApiException = None |
| 23 | + EnterpriseApiServiceClient = None |
| 24 | + |
| 25 | + |
| 26 | +class EnterpriseEnrollmentViewProcessor(PipelineStep): |
| 27 | + """ |
| 28 | + Enrollment pipeline step: notify enterprise API and record consent. |
| 29 | +
|
| 30 | + When an enterprise customer user enrolls in a course, this step calls the enterprise and consent |
| 31 | + API clients to post the enrollment and provide consent on behalf of the enterprise customer. |
| 32 | +
|
| 33 | + This step is intended to be registered as a pipeline step for the |
| 34 | + ``org.openedx.learning.course.enrollment.view.started.v1`` filter. |
| 35 | + """ |
| 36 | + |
| 37 | + def run_filter( # pylint: disable=arguments-differ |
| 38 | + self, |
| 39 | + user: AbstractBaseUser, |
| 40 | + course_key: Any, |
| 41 | + has_api_key_permissions: bool |
| 42 | + ) -> dict[str, Any]: |
| 43 | + """ |
| 44 | + Post enterprise enrollment and consent if the user is an enterprise customer user. |
| 45 | + """ |
| 46 | + log.info( |
| 47 | + "EnterpriseEnrollmentViewProcessor running: user_id=%s, course_key=%s, api_permissions=%s", |
| 48 | + user.id, |
| 49 | + str(course_key), |
| 50 | + has_api_key_permissions, |
| 51 | + ) |
| 52 | + |
| 53 | + request = get_current_request() |
| 54 | + linked_enterprise = None |
| 55 | + if request and hasattr(request, 'data'): |
| 56 | + linked_enterprise = request.data.get("linked_enterprise_customer") |
| 57 | + |
| 58 | + if linked_enterprise is None or not has_api_key_permissions: |
| 59 | + return { |
| 60 | + 'user': user, |
| 61 | + 'course_key': course_key, |
| 62 | + 'has_api_key_permissions': has_api_key_permissions |
| 63 | + } |
| 64 | + |
| 65 | + username = user.username |
| 66 | + course_id = str(course_key) |
| 67 | + |
| 68 | + try: |
| 69 | + EnterpriseApiServiceClient().post_enterprise_course_enrollment(username, course_id) |
| 70 | + except EnterpriseApiException as exc: |
| 71 | + log.exception( |
| 72 | + "Failed to post enterprise course enrollment for user %s in course %s.", |
| 73 | + username, course_id, |
| 74 | + ) |
| 75 | + raise CourseEnrollmentViewStarted.PreventEnrollment( |
| 76 | + f"Failed to post enterprise course enrollment for {username} in {course_id}." |
| 77 | + ) from exc |
| 78 | + |
| 79 | + try: |
| 80 | + ConsentApiServiceClient().provide_consent( |
| 81 | + username=username, |
| 82 | + course_id=course_id, |
| 83 | + enterprise_customer_uuid=str(linked_enterprise), |
| 84 | + ) |
| 85 | + except HTTPError as exc: |
| 86 | + log.exception( |
| 87 | + "Failed to record enterprise consent for user %s in course %s.", |
| 88 | + username, course_id, |
| 89 | + ) |
| 90 | + raise CourseEnrollmentViewStarted.PreventEnrollment( |
| 91 | + f"Failed to record enterprise consent for {username} in {course_id}." |
| 92 | + ) from exc |
| 93 | + |
| 94 | + |
| 95 | + return { |
| 96 | + 'user': user, |
| 97 | + 'course_key': course_key, |
| 98 | + 'has_api_key_permissions': has_api_key_permissions |
| 99 | + } |
0 commit comments