From cc0b2ade51de4d7fc1e41ebdf25ec76c5ca3cc74 Mon Sep 17 00:00:00 2001 From: Yusuf Musleh Date: Wed, 21 Jun 2023 14:31:38 +0300 Subject: [PATCH] Backport enterprise course enrollments api implementation (#5) * feat: add additional fields to EnterpriseCourseEnrollmentViewSet Created new manager and serializer for EnterpriseCourseEnrollment model, which add additional fields to the model, which allow to reduce the number of requests to the API by providing the necessary data in the feilds along side the model. Use the new manager and serializer in EnterpriseCourseEnrollmentViewSet. Ref: https://github.com/openedx/edx-enterprise/pull/1714 Co-authored-by: 0x29a , Cup0fCoffee * chore: bump version and update changelog entry * chore: Bump up version --- CHANGELOG.rst | 3 +++ enterprise/__init__.py | 2 +- enterprise/api/v1/serializers.py | 12 ++++++++++++ enterprise/api/v1/views.py | 4 ++-- enterprise/models.py | 16 ++++++++++++++++ tests/test_enterprise/api/test_views.py | 2 ++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e12f610d94..d354de2c2c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,9 @@ Unreleased ---------- * nothing +[3.42.8] +feat: add additional fields to EnterpriseCourseEnrollmentViewSet + [3.42.7] -------- feat: allow enrollment to invite-only courses via manage learners admin page diff --git a/enterprise/__init__.py b/enterprise/__init__.py index decedbe1cb..2478874dae 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,6 +2,6 @@ Your project description goes here. """ -__version__ = "3.42.7" +__version__ = "3.42.8" default_app_config = "enterprise.apps.EnterpriseConfig" diff --git a/enterprise/api/v1/serializers.py b/enterprise/api/v1/serializers.py index f21e6bc3cd..b142935d26 100644 --- a/enterprise/api/v1/serializers.py +++ b/enterprise/api/v1/serializers.py @@ -316,12 +316,24 @@ class EnterpriseCourseEnrollmentReadOnlySerializer(serializers.ModelSerializer): """ Serializer for EnterpriseCourseEnrollment model. """ + class Meta: + model = models.EnterpriseCourseEnrollment + fields = ( + 'enterprise_customer_user', 'course_id', 'created', + ) + + +class EnterpriseCourseEnrollmentWithAdditionalFieldsReadOnlySerializer(EnterpriseCourseEnrollmentReadOnlySerializer): + """ + Serializer for EnterpriseCourseEnrollment model with additional fields. + """ class Meta: model = models.EnterpriseCourseEnrollment fields = ( 'enterprise_customer_user', 'course_id', + 'created', 'enrollment_date', 'enrollment_track', 'user_email', diff --git a/enterprise/api/v1/views.py b/enterprise/api/v1/views.py index 8d87ec9e06..8859ef60e5 100644 --- a/enterprise/api/v1/views.py +++ b/enterprise/api/v1/views.py @@ -457,7 +457,7 @@ class EnterpriseCourseEnrollmentViewSet(EnterpriseReadWriteModelViewSet): API views for the ``enterprise-course-enrollment`` API endpoint. """ - queryset = models.EnterpriseCourseEnrollment.objects.all() + queryset = models.EnterpriseCourseEnrollment.with_additional_fields.all() filter_backends = (filters.OrderingFilter, DjangoFilterBackend, EnterpriseCourseEnrollmentFilterBackend) USER_ID_FILTER = 'enterprise_customer_user__user_id' @@ -472,7 +472,7 @@ def get_serializer_class(self): Use a special serializer for any requests that aren't read-only. """ if self.request.method in ('GET',): - return serializers.EnterpriseCourseEnrollmentReadOnlySerializer + return serializers.EnterpriseCourseEnrollmentWithAdditionalFieldsReadOnlySerializer return serializers.EnterpriseCourseEnrollmentWriteSerializer diff --git a/enterprise/models.py b/enterprise/models.py index 07d90cf7ec..7cdb362fd3 100644 --- a/enterprise/models.py +++ b/enterprise/models.py @@ -1676,6 +1676,21 @@ class EnterpriseCourseEnrollmentManager(models.Manager): Model manager for `EnterpriseCourseEnrollment`. """ + def get_queryset(self): + """ + Override to return only those enrollment records for which learner is linked to an enterprise. + """ + + return super().get_queryset().select_related('enterprise_customer_user').filter( + enterprise_customer_user__linked=True + ) + + +class EnterpriseCourseEnrollmentWithAdditionalFieldsManager(models.Manager): + """ + Model manager for `EnterpriseCourseEnrollment`. + """ + def get_queryset(self): """ Override to return only those enrollment records for which learner is linked to an enterprise. @@ -1739,6 +1754,7 @@ class EnterpriseCourseEnrollment(TimeStampedModel): """ objects = EnterpriseCourseEnrollmentManager() + with_additional_fields = EnterpriseCourseEnrollmentWithAdditionalFieldsManager() class Meta: unique_together = (('enterprise_customer_user', 'course_id',),) diff --git a/tests/test_enterprise/api/test_views.py b/tests/test_enterprise/api/test_views.py index 4fdb9f4b4d..0445a10ab8 100644 --- a/tests/test_enterprise/api/test_views.py +++ b/tests/test_enterprise/api/test_views.py @@ -1220,10 +1220,12 @@ class TestEnterpriseCustomerViewSet(BaseTestEnterpriseAPIViews): [{ 'enterprise_customer_user__id': 1, 'course_id': 'course-v1:edX+DemoX+DemoCourse', + 'created': '2021-10-20T19:01:31Z', }], [{ 'enterprise_customer_user': 1, 'course_id': 'course-v1:edX+DemoX+DemoCourse', + 'created': '2021-10-20T19:01:31Z', 'enrollment_date': None, 'enrollment_track': None, 'user_email': None,