Skip to content

Commit

Permalink
feat: [FC-0031] Add optional field 'is_enrolled' to course detail view
Browse files Browse the repository at this point in the history
  • Loading branch information
KyryloKireiev committed Dec 15, 2023
1 parent d7b7f70 commit bd53863
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 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,19 @@ 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 user is authenticated and 'username' is in query params.
"""
response = super().to_representation(instance)
if can_show_certificate_available_date_field(instance):
response['certificate_available_date'] = instance.certificate_available_date

requested_user = self.context['request'].query_params.get('username', None)
if self.context['request'].user.is_authenticated and requested_user:
User = get_user_model()
requested_user = User.objects.get(username=requested_user)
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 bd53863

Please sign in to comment.