From a3a7ff24282bda5bc069569cd33bc69d37060fd9 Mon Sep 17 00:00:00 2001 From: Jamie Folsom Date: Fri, 16 Oct 2015 16:11:08 -0400 Subject: [PATCH] Enroll the coach in the CCX on creation Make the course URL pattern more generic. Comment newly added functionality. Fix quality issues. Address two lint errors, with regex and variable naming. Changed how we access course_key and course_id. Replace another instance of self.course.id.to_deprecated_string() Remove unused import, add missing one. Improve how the ccx key is extracted from the URL Use resolve() instead of os.path to get the course_id. Remove the granting of staff access to coaches. --- lms/djangoapps/ccx/tests/test_views.py | 19 ++++++++++++++++--- lms/djangoapps/ccx/views.py | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/ccx/tests/test_views.py b/lms/djangoapps/ccx/tests/test_views.py index 94a059b78b..1839cfd200 100644 --- a/lms/djangoapps/ccx/tests/test_views.py +++ b/lms/djangoapps/ccx/tests/test_views.py @@ -6,6 +6,7 @@ import json import re import pytz import ddt +import urlparse from mock import patch, MagicMock from nose.plugins.attrib import attr @@ -14,12 +15,13 @@ from courseware.courses import get_course_by_id from courseware.tests.factories import StudentModuleFactory from courseware.tests.helpers import LoginEnrollmentTestCase from courseware.tabs import get_course_tab_list -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, resolve from django.utils.timezone import UTC from django.test.utils import override_settings from django.test import RequestFactory from edxmako.shortcuts import render_to_response from request_cache.middleware import RequestCache +from opaque_keys.edx.keys import CourseKey from student.roles import CourseCcxCoachRole from student.models import ( CourseEnrollment, @@ -200,7 +202,7 @@ class TestCoachDashboard(SharedModuleStoreTestCase, LoginEnrollmentTestCase): self.make_coach() url = reverse( 'ccx_coach_dashboard', - kwargs={'course_id': self.course.id.to_deprecated_string()}) + kwargs={'course_id': unicode(self.course.id)}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue(re.search( @@ -212,15 +214,26 @@ class TestCoachDashboard(SharedModuleStoreTestCase, LoginEnrollmentTestCase): Create CCX. Follow redirect to coach dashboard, confirm we see the coach dashboard for the new CCX. """ + self.make_coach() url = reverse( 'create_ccx', - kwargs={'course_id': self.course.id.to_deprecated_string()}) + kwargs={'course_id': unicode(self.course.id)}) + response = self.client.post(url, {'name': 'New CCX'}) self.assertEqual(response.status_code, 302) url = response.get('location') # pylint: disable=no-member response = self.client.get(url) self.assertEqual(response.status_code, 200) + + # Get the ccx_key + path = urlparse.urlparse(url).path + resolver = resolve(path) + ccx_key = resolver.kwargs['course_id'] + + course_key = CourseKey.from_string(ccx_key) + + self.assertTrue(CourseEnrollment.is_enrolled(self.coach, course_key)) self.assertTrue(re.search('id="ccx-schedule"', response.content)) @SharedModuleStoreTestCase.modifies_courseware diff --git a/lms/djangoapps/ccx/views.py b/lms/djangoapps/ccx/views.py index e022875f81..df5684d8db 100644 --- a/lms/djangoapps/ccx/views.py +++ b/lms/djangoapps/ccx/views.py @@ -47,7 +47,6 @@ from instructor.enrollment import ( unenroll_email, get_email_params, ) - from .models import CustomCourseForEdX from .overrides import ( get_override_for_ccx, @@ -56,7 +55,6 @@ from .overrides import ( bulk_delete_ccx_override_fields, ) - log = logging.getLogger(__name__) TODAY = datetime.datetime.today # for patching in tests @@ -183,7 +181,19 @@ def create_ccx(request, course, ccx=None): override_field_for_ccx(ccx, vertical, hidden, True) ccx_id = CCXLocator.from_course_locator(course.id, ccx.id) # pylint: disable=no-member + url = reverse('ccx_coach_dashboard', kwargs={'course_id': ccx_id}) + + # Enroll the coach in the course + email_params = get_email_params(course, auto_enroll=True, course_key=ccx_id, display_name=ccx.display_name) + enroll_email( + course_id=ccx_id, + student_email=request.user.email, + auto_enroll=True, + email_students=True, + email_params=email_params, + ) + return redirect(url)