From 4ab368720ec8e6c2a0866a78fa54b5ad41485f84 Mon Sep 17 00:00:00 2001 From: amir-qayyum-arbisoft Date: Tue, 28 Apr 2015 20:26:33 +0500 Subject: [PATCH] Added error message in enrollment tab ccx coach dashboard --- lms/djangoapps/ccx/tests/test_views.py | 30 ++++++++++++++++ lms/djangoapps/ccx/views.py | 47 ++++++++++++++++++++------ lms/templates/ccx/coach_dashboard.html | 45 ++++++++++++------------ lms/templates/ccx/enrollment.html | 12 +++++-- 4 files changed, 101 insertions(+), 33 deletions(-) diff --git a/lms/djangoapps/ccx/tests/test_views.py b/lms/djangoapps/ccx/tests/test_views.py index eb9ebc4e5e..7ec56e5df6 100644 --- a/lms/djangoapps/ccx/tests/test_views.py +++ b/lms/djangoapps/ccx/tests/test_views.py @@ -79,6 +79,7 @@ def ccx_dummy_request(): @attr('shard_1') +@ddt.ddt class TestCoachDashboard(SharedModuleStoreTestCase, LoginEnrollmentTestCase): """ Tests for Custom Courses views. @@ -387,6 +388,35 @@ class TestCoachDashboard(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ).exists() ) + @ddt.data("dummy_student_id", "xyz@gmail.com") + def test_manage_add_single_invalid_student(self, student_id): + """enroll a single non valid student + """ + self.make_coach() + ccx = self.make_ccx() + course_key = CCXLocator.from_course_locator(self.course.id, ccx.id) + url = reverse( + 'ccx_manage_student', + kwargs={'course_id': course_key} + ) + redirect_url = reverse( + 'ccx_coach_dashboard', + kwargs={'course_id': course_key} + ) + data = { + 'student-action': 'add', + 'student-id': u','.join([student_id, ]), # pylint: disable=no-member + } + response = self.client.post(url, data=data, follow=True) + + error_message = 'Could not find a user with name or email "{student_id}" '.format( + student_id=student_id + ) + self.assertContains(response, error_message, status_code=200) + + # we were redirected to our current location + self.assertRedirects(response, redirect_url, status_code=302) + def test_manage_add_single_student(self): """enroll a single student who is a member of the class already """ diff --git a/lms/djangoapps/ccx/views.py b/lms/djangoapps/ccx/views.py index 859ff2ef02..520e5452d3 100644 --- a/lms/djangoapps/ccx/views.py +++ b/lms/djangoapps/ccx/views.py @@ -131,7 +131,7 @@ def dashboard(request, course, ccx=None): context['schedule'] = json.dumps(schedule, indent=4) context['save_url'] = reverse( 'save_ccx', kwargs={'course_id': ccx_locator}) - context['ccx_members'] = CourseEnrollment.objects.filter(course_id=ccx_locator) + context['ccx_members'] = CourseEnrollment.objects.filter(course_id=ccx_locator, is_active=True) context['gradebook_url'] = reverse( 'ccx_gradebook', kwargs={'course_id': ccx_locator}) context['grades_csv_url'] = reverse( @@ -440,6 +440,30 @@ def ccx_invite(request, course, ccx=None): return redirect(url) +def validate_student_email(email): + """ + validate student's email id + """ + error_message = None + try: + validate_email(email) + except ValidationError: + log.info( + 'Invalid user name or email when trying to enroll student: %s', + email + ) + if email: + error_message = _( + 'Could not find a user with name or email "{email}" ' + ).format(email=email) + else: + error_message = _( + 'Please enter a valid username or email.' + ) + + return error_message + + @ensure_csrf_cookie @cache_control(no_cache=True, no_store=True, must_revalidate=True) @coach_dashboard @@ -452,29 +476,32 @@ def ccx_student_management(request, course, ccx=None): action = request.POST.get('student-action', None) student_id = request.POST.get('student-id', '') user = email = None + error_message = "" + course_key = CCXLocator.from_course_locator(course.id, ccx.id) try: user = get_student_from_identifier(student_id) except User.DoesNotExist: email = student_id + error_message = validate_student_email(email) + if email and not error_message: + error_message = _( + 'Could not find a user with name or email "{email}" ' + ).format(email=email) else: email = user.email + error_message = validate_student_email(email) - course_key = CCXLocator.from_course_locator(course.id, ccx.id) - try: - validate_email(email) + if error_message is None: if action == 'add': # by decree, no emails sent to students added this way # by decree, any students added this way are auto_enrolled enroll_email(course_key, email, auto_enroll=True, email_students=False) elif action == 'revoke': unenroll_email(course_key, email, email_students=False) - except ValidationError: - log.info('Invalid user name or email when trying to enroll student: %s', email) + else: + messages.error(request, error_message) - url = reverse( - 'ccx_coach_dashboard', - kwargs={'course_id': course_key} - ) + url = reverse('ccx_coach_dashboard', kwargs={'course_id': course_key}) return redirect(url) diff --git a/lms/templates/ccx/coach_dashboard.html b/lms/templates/ccx/coach_dashboard.html index 529ccda196..3f781de688 100644 --- a/lms/templates/ccx/coach_dashboard.html +++ b/lms/templates/ccx/coach_dashboard.html @@ -22,27 +22,26 @@ from django.core.urlresolvers import reverse

${_("CCX Coach Dashboard")}

- % if messages: - - % endif - %if not ccx: -
-
- - -
- -
-
+ % if messages: + + % endif +
+
+ + +
+ +
+
%endif %if ccx: @@ -151,5 +150,9 @@ from django.core.urlresolvers import reverse $(setup_tabs); $(setup_management_form) - + $( document ).ready(function() { + if ($('#ccx_std_list_messages').length) { + $('#ccx_std_list_messages')[0].focus(); + } + }); diff --git a/lms/templates/ccx/enrollment.html b/lms/templates/ccx/enrollment.html index 2d054da3e4..c26dfac6f7 100644 --- a/lms/templates/ccx/enrollment.html +++ b/lms/templates/ccx/enrollment.html @@ -46,13 +46,21 @@ -
+

${_("Student List Management")}

+ %if messages: + +
+ %for message in messages: + ${message} + %endfor +
+ %endif @@ -66,7 +74,7 @@ - + %endfor
${member.user} ${member.user.email}
Revoke access
${_("Revoke access")}