Skip to content

Commit

Permalink
Merge pull request #33646 from raccoongang/rg/feat/FC0031/update_cour…
Browse files Browse the repository at this point in the history
…se_detail_view_endpoint

feat: [FC-0031] Add optional field 'is_enrolled' to course detail view
  • Loading branch information
Feanil Patel authored Dec 15, 2023
2 parents 2573f34 + d344a7e commit 01fcbc9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lms/djangoapps/course_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import urllib

from common.djangoapps.student.models import CourseEnrollment
from django.contrib.auth import get_user_model
from django.urls import reverse
from edx_django_utils import monitoring as monitoring_utils
from rest_framework import serializers
Expand Down Expand Up @@ -164,10 +166,23 @@ def to_representation(self, instance):
"""
Get the `certificate_available_date` in response
if the `certificates.auto_certificate_generation` waffle switch is enabled
Get the 'is_enrolled' in response if 'username' is in query params,
user is staff, superuser, or user is authenticated and
the has the same 'username' as the 'username' in the query params.
"""
response = super().to_representation(instance)
if can_show_certificate_available_date_field(instance):
response['certificate_available_date'] = instance.certificate_available_date

requested_username = self.context['request'].query_params.get('username', None)
if requested_username:
user = self.context['request'].user
if ((user.is_authenticated and user.username == requested_username)
or user.is_staff or user.is_superuser):
User = get_user_model()
requested_user = User.objects.get(username=requested_username)
response['is_enrolled'] = CourseEnrollment.is_enrolled(requested_user, instance.id)
return response


Expand Down
43 changes: 43 additions & 0 deletions lms/djangoapps/course_api/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,49 @@ def test_basic(self):
)
self.assertDictEqual(result, self.expected_data)

@mock.patch('lms.djangoapps.course_api.serializers.CourseEnrollment.is_enrolled', return_value=True)
def test_is_enrolled_field_true(self, mock_is_enrolled):
course = self.create_course()
result = self._get_result_with_query_param(course)
assert result['is_enrolled'] is True
mock_is_enrolled.assert_called_once()

@mock.patch('lms.djangoapps.course_api.serializers.CourseEnrollment.is_enrolled', return_value=False)
def test_is_enrolled_field_false(self, mock_is_enrolled):
course = self.create_course()
result = self._get_result_with_query_param(course)
assert result['is_enrolled'] is False
mock_is_enrolled.assert_called_once()

def test_is_enrolled_field_anonymous_user(self):
course = self.create_course()
result = self._get_anonymous_result(course)
self.assertNotIn('is_enrolled', result)

def _get_anonymous_request(self):
return Request(self.request_factory.get('/'))

def _get_anonymous_result(self, course):
course_overview = CourseOverview.get_from_id(course.id)
return self.serializer_class(course_overview, context={'request': self._get_anonymous_request()}).data

def _get_result_with_query_param(self, course):
"""
Return the CourseSerializer for the specified course with 'username' in query params.
"""
course_overview = CourseOverview.get_from_id(course.id)
return self.serializer_class(course_overview, context={'request': self._get_request_with_query_param()}).data

def _get_request_with_query_param(self, user=None):
"""
Build a Request object for the specified user with 'username' in query params.
"""
if user is None:
user = self.honor_user
request = Request(self.request_factory.get('/', {'username': user.username}))
request.user = user
return request


class TestCourseKeySerializer(TestCase): # lint-amnesty, pylint: disable=missing-class-docstring

Expand Down

0 comments on commit 01fcbc9

Please sign in to comment.