Repository: ocadotechnology/codeforlife-portal

Issue:
Class page - reset student passwords



Discussion:

Resolving patch:
diff --git a/portal/urls.py b/portal/urls.py
--- a/portal/urls.py
+++ b/portal/urls.py
@@ -70,8 +70,9 @@
 from portal.views.home_new import login_view, logout_view_new, register_view
 from portal.views.organisation_new import organisation_fuzzy_lookup_new, organisation_manage_new
 from portal.views.teacher.teach_new import teacher_classes_new, teacher_class_new, teacher_view_class, \
-    teacher_delete_class_new, teacher_edit_class_new, teacher_move_class_new, teacher_edit_student_new, \
-    teacher_student_reset_new, materials_viewer_new, teacher_print_reminder_cards_new, teacher_delete_students_new
+    teacher_edit_class_new, teacher_move_class_new, teacher_edit_student_new, \
+    teacher_student_reset_new, materials_viewer_new, teacher_print_reminder_cards_new, teacher_delete_students_new, \
+    teacher_delete_class_new, teacher_class_password_reset_new
 from portal.views.teacher.dashboard import dashboard_manage, organisation_allow_join_new, organisation_deny_join_new, \
     organisation_kick_new, organisation_toggle_admin_new, teacher_disable_2FA_new
 
@@ -235,6 +236,7 @@
     url(r'^redesign/teach/class/edit/(?P<access_code>[A-Z0-9]+)/$', teacher_edit_class_new, name='teacher_edit_class_new'),
     url(r'^redesign/teach/class/student/edit/(?P<pk>[0-9]+)/$', teacher_edit_student_new, name='teacher_edit_student_new'),
     url(r'^redesign/teach/class/student/reset/(?P<pk>[0-9]+)/$', teacher_student_reset_new, name='teacher_student_reset_new'),
+    url(r'^redesign/teach/class/(?P<access_code>[A-Z0-9]+)/password_reset/$', teacher_class_password_reset_new, name='teacher_class_password_reset_new'),
     url(r'^redesign/teach/class/move/(?P<access_code>[A-Z0-9]+)/$', teacher_move_class_new, name='teacher_move_class_new'),
 
     url(r'^api/', include([
diff --git a/portal/views/teacher/teach_new.py b/portal/views/teacher/teach_new.py
--- a/portal/views/teacher/teach_new.py
+++ b/portal/views/teacher/teach_new.py
@@ -432,6 +432,34 @@ def teacher_student_reset_new(request, pk):
                    'query_data': json.dumps(name_pass)})
 
 
+@login_required(login_url=reverse_lazy('login_new'))
+@user_passes_test(logged_in_as_teacher, login_url=reverse_lazy('login_new'))
+def teacher_class_password_reset_new(request, access_code):
+    klass = get_object_or_404(Class, access_code=access_code)
+    students = Student.objects.filter(class_field=klass).order_by('new_user__first_name')
+
+    # check user authorised to see class
+    if request.user.new_teacher != klass.teacher:
+        raise Http404
+
+    student_ids = json.loads(request.POST.get('transfer_students', '[]'))
+    students = [get_object_or_404(Student, id=i, class_field=klass) for i in student_ids]
+
+    name_tokens = []
+    for student in students:
+        password = generate_password(6)
+        name_tokens.append({'name': student.new_user.first_name, 'password': password})
+        student.new_user.set_password(password)
+        student.new_user.save()
+
+    return render(request, 'redesign/teach_new/onboarding_print.html',
+                  {'class': klass,
+                   'onboarding_done': True,
+                   'passwords_reset': True,
+                   'name_tokens': name_tokens,
+                   'query_data': json.dumps(name_tokens)})
+
+
 @login_required(login_url=reverse_lazy('login_new'))
 @user_passes_test(logged_in_as_teacher, login_url=reverse_lazy('login_new'))
 def teacher_move_class_new(request, access_code):


Test patch:
diff --git a/portal/tests/pageObjects/portal/teach/class_page_new.py b/portal/tests/pageObjects/portal/teach/class_page_new.py
--- a/portal/tests/pageObjects/portal/teach/class_page_new.py
+++ b/portal/tests/pageObjects/portal/teach/class_page_new.py
@@ -36,7 +36,6 @@
 # identified as the original program.
 from teach_base_page_new import TeachBasePage
 import dashboard_page_new
-import dashboard_page_new
 import class_settings_page_new
 import edit_student_page
 import onboarding_student_list_page
@@ -70,6 +69,10 @@ def delete_students(self):
         self.browser.find_element_by_id('deleteSelectedStudents').click()
         return self
 
+    def reset_passwords(self):
+        self.browser.find_element_by_id('resetSelectedStudents').click()
+        return self
+
     def cancel_dialog(self):
         self.browser.find_element_by_xpath(
             "//div[contains(@class,'ui-dialog')]//span[contains(text(),'Cancel')]").click()
@@ -85,6 +88,11 @@ def confirm_delete_student_dialog(self):
 
         return self
 
+    def confirm_reset_student_dialog(self):
+        self._click_confirm()
+
+        return onboarding_student_list_page.OnboardingStudentListPage(self.browser)
+
     def confirm_dialog_expect_error(self):
         self._click_confirm()
 
diff --git a/portal/tests/test_teacher_student_new.py b/portal/tests/test_teacher_student_new.py
--- a/portal/tests/test_teacher_student_new.py
+++ b/portal/tests/test_teacher_student_new.py
@@ -179,3 +179,21 @@ def test_delete(self):
         page = page.confirm_delete_student_dialog()
 
         assert not page.student_exists(student_name)
+
+    def test_reset_passwords(self):
+        email, password = signup_teacher_directly()
+        create_organisation_directly(email)
+        _, class_name, access_code = create_class_directly(email)
+        student_name, student_password, _ = create_school_student_directly(access_code)
+
+        selenium.get(self.live_server_url + "/portal/redesign/home")
+        page = HomePage(selenium).go_to_login_page().login(email, password)
+        page = page.go_to_class_page()
+        assert page.student_exists(student_name)
+
+        page = page.toggle_select_student().reset_passwords()
+        assert page.is_dialog_showing()
+        page = page.confirm_reset_student_dialog()
+
+        assert page.student_exists(student_name)
+        assert page.__class__.__name__ == 'OnboardingStudentListPage'