Skip to content

Commit

Permalink
Backport enterprise course enrollments api implementation (#5)
Browse files Browse the repository at this point in the history
* 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: openedx#1714

Co-authored-by: 0x29a <[email protected]>, Cup0fCoffee
<[email protected]>

* chore: bump version and update changelog entry

* chore: Bump up version
  • Loading branch information
yusuf-musleh authored Jun 21, 2023
1 parent 8c499ec commit cc0b2ad
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Your project description goes here.
"""

__version__ = "3.42.7"
__version__ = "3.42.8"

default_app_config = "enterprise.apps.EnterpriseConfig"
12 changes: 12 additions & 0 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions enterprise/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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


Expand Down
16 changes: 16 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1739,6 +1754,7 @@ class EnterpriseCourseEnrollment(TimeStampedModel):
"""

objects = EnterpriseCourseEnrollmentManager()
with_additional_fields = EnterpriseCourseEnrollmentWithAdditionalFieldsManager()

class Meta:
unique_together = (('enterprise_customer_user', 'course_id',),)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cc0b2ad

Please sign in to comment.