Skip to content
45 changes: 31 additions & 14 deletions common/djangoapps/student/tests/test_recent_enrollments.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def setUp(self):

# New Course
course_location = locator.CourseLocator('Org1', 'Course1', 'Run1')
self.course, _ = self._create_course_and_enrollment(course_location)
self.course, self.enrollment = self._create_course_and_enrollment(course_location)

def _create_course_and_enrollment(self, course_location):
""" Creates a course and associated enrollment. """
Expand Down Expand Up @@ -112,10 +112,10 @@ def test_enrollments_sorted_most_recent(self):
recent_course_list = _get_recently_enrolled_courses(courses_list)
self.assertEqual(len(recent_course_list), 5)

self.assertEqual(recent_course_list[1], courses[0])
self.assertEqual(recent_course_list[2], courses[1])
self.assertEqual(recent_course_list[3], courses[2])
self.assertEqual(recent_course_list[4], courses[3])
self.assertEqual(recent_course_list[1][0], courses[0])
self.assertEqual(recent_course_list[2][0], courses[1])
self.assertEqual(recent_course_list[3][0], courses[2])
self.assertEqual(recent_course_list[4][0], courses[3])

def test_dashboard_rendering(self):
"""
Expand All @@ -127,24 +127,41 @@ def test_dashboard_rendering(self):
self.assertContains(response, "Thank you for enrolling in")

@ddt.data(
(['audit', 'honor', 'verified'], False),
(['professional'], False),
(['verified'], False),
(['audit'], True),
(['honor'], True),
([], True)
#Register as an honor in any course modes with no payment option
([('audit', 0), ('honor', 0)], 'honor', True),
([('honor', 0)], 'honor', True),
([], 'honor', True),
#Register as an honor in any course modes which has payment option
([('honor', 10)], 'honor', False), # This is a paid course
([('audit', 0), ('honor', 0), ('professional', 20)], 'honor', True),
([('audit', 0), ('honor', 0), ('verified', 20)], 'honor', True),
([('audit', 0), ('honor', 0), ('verified', 20), ('professional', 20)], 'honor', True),
([], 'honor', True),
#Register as an audit in any course modes with no payment option
([('audit', 0), ('honor', 0)], 'audit', True),
([('audit', 0)], 'audit', True),
#Register as an audit in any course modes which has no payment option
([('audit', 0), ('honor', 0), ('verified', 10)], 'audit', True),
#Register as a verified in any course modes which has payment option

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nitpick: please add a space between "#" and "Register" in each of these comments.

Also, we use "register" to mean "a user has created an account". This is different from "enroll", which means, "a user has enrolled in a course". So I think these comments should say "enroll" instead of "register".

([('professional', 20)], 'professional', False),
([('verified', 20)], 'verified', False),
([('professional', 20), ('verified', 20)], 'verified', False),
([('audit', 0), ('honor', 0), ('verified', 20)], 'verified', False)
)
@ddt.unpack
def test_donate_button(self, course_modes, show_donate):
def test_donate_button(self, course_modes, enrollment_mode, show_donate):
# Enable the enrollment success message
self._configure_message_timeout(10000)

# Enable donations
DonationConfiguration(enabled=True).save()

# Create the course mode(s)
for mode in course_modes:
CourseModeFactory(mode_slug=mode, course_id=self.course.id)
for mode, min_price in course_modes:
CourseModeFactory(mode_slug=mode, course_id=self.course.id, min_price=min_price)

self.enrollment.mode = enrollment_mode
self.enrollment.save()

# Check that the donate button is or is not displayed
self.client.login(username=self.student.username, password=self.PASSWORD)
Expand Down
13 changes: 6 additions & 7 deletions common/djangoapps/student/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,9 @@ def _create_recent_enrollment_message(course_enrollment_pairs, course_modes):
{
"course_id": course.id,
"course_name": course.display_name,
"allow_donation": _allow_donation(course_modes, course.id)
"allow_donation": _allow_donation(course_modes, course.id, enrollment)
}
for course in recently_enrolled_courses
for course, enrollment in recently_enrolled_courses
]

return render_to_string(
Expand All @@ -738,30 +738,29 @@ def _get_recently_enrolled_courses(course_enrollment_pairs):
seconds = DashboardConfiguration.current().recent_enrollment_time_delta
time_delta = (datetime.datetime.now(UTC) - datetime.timedelta(seconds=seconds))
return [
course for course, enrollment in course_enrollment_pairs
(course, enrollment) for course, enrollment in course_enrollment_pairs
# If the enrollment has no created date, we are explicitly excluding the course
# from the list of recent enrollments.
if enrollment.is_active and enrollment.created > time_delta
]


def _allow_donation(course_modes, course_id):
def _allow_donation(course_modes, course_id, enrollment):
"""Determines if the dashboard will request donations for the given course.

Check if donations are configured for the platform, and if the current course is accepting donations.

Args:
course_modes (dict): Mapping of course ID's to course mode dictionaries.
course_id (str): The unique identifier for the course.
enrollment(CourseEnrollment): The enrollment object in which the user is enrolled

Returns:
True if the course is allowing donations.

"""
donations_enabled = DonationConfiguration.current().enabled
is_verified_mode = CourseMode.has_verified_mode(course_modes[course_id])
has_payment_option = CourseMode.has_payment_options(course_id)
return donations_enabled and not is_verified_mode and not has_payment_option
return donations_enabled and enrollment.mode in course_modes[course_id] and course_modes[course_id][enrollment.mode].min_price == 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these class methods aren't being used anywhere else, you can delete them from the CourseMode model.



def try_change_enrollment(request):
Expand Down