Repository: ocadotechnology/codeforlife-portal

Issue:
Class page - delete class



Discussion:

Resolving patch:
diff --git a/portal/urls.py b/portal/urls.py
--- a/portal/urls.py
+++ b/portal/urls.py
@@ -70,7 +70,7 @@
 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, \
-    materials_viewer_new, teacher_print_reminder_cards_new, teacher_view_class
+    materials_viewer_new, teacher_print_reminder_cards_new, teacher_view_class, teacher_delete_class_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
 
@@ -228,7 +228,8 @@
     url(r'^redesign/teach/dashboard/disable_2FA/(?P<pk>[0-9]+)/$', teacher_disable_2FA_new, name='teacher_disable_2FA_new'),
     url(r'^redesign/teach/dashboard/allow_join/(?P<pk>[0-9]+)/$', organisation_allow_join_new, name='organisation_allow_join_new'),
     url(r'^redesign/teach/dashboard/deny_join/(?P<pk>[0-9]+)/$', organisation_deny_join_new, name='organisation_deny_join_new'),
-    url(r'^redesign/teach/class/(?P<access_code>[A-Z0-9]+)', teacher_view_class, name='view_class'),
+    url(r'^redesign/teach/class/(?P<access_code>[A-Z0-9]+)/$', teacher_view_class, name='view_class'),
+    url(r'^redesign/teach/class/delete/(?P<access_code>[A-Z0-9]+)/$', teacher_delete_class_new, name='teacher_delete_class_new'),
 
     url(r'^api/', include([
         url(r'^registered/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', registered_users, name="registered-users"),
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
@@ -245,6 +245,24 @@ def teacher_view_class(request, access_code):
                    'num_students': len(students)})
 
 
+@login_required(login_url=reverse_lazy('login_new'))
+@user_passes_test(logged_in_as_teacher, login_url=reverse_lazy('login_new'))
+def teacher_delete_class_new(request, access_code):
+    klass = get_object_or_404(Class, access_code=access_code)
+
+    # check user authorised to see class
+    if request.user.new_teacher != klass.teacher:
+        raise Http404
+
+    if Student.objects.filter(class_field=klass).exists():
+        messages.info(request, 'This class still has students, please remove or delete them all before deleting the class.')
+        return HttpResponseRedirect(reverse_lazy('view_class', kwargs={'access_code': access_code}))
+
+    klass.delete()
+
+    return HttpResponseRedirect(reverse_lazy('dashboard'))
+
+
 @login_required(login_url=reverse_lazy('login_new'))
 @user_passes_test(logged_in_as_teacher, login_url=reverse_lazy('login_new'))
 def teacher_print_reminder_cards_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
@@ -35,6 +35,7 @@
 # program; modified versions of the program must be marked as such and not
 # identified as the original program.
 from teach_base_page_new import TeachBasePage
+import dashboard_page_new
 import onboarding_student_list_page
 
 
@@ -57,3 +58,35 @@ def _click_create_students(self):
 
     def student_exists(self, name):
         return name in self.browser.find_element_by_id('student_table').text
+
+    def delete_class(self):
+        self.browser.find_element_by_id('deleteClass').click()
+        return self
+
+    def cancel_dialog(self):
+        self.browser.find_element_by_xpath(
+            "//div[contains(@class,'ui-dialog')]//span[contains(text(),'Cancel')]").click()
+        return self
+
+    def confirm_dialog(self):
+        self._click_confirm()
+
+        return dashboard_page_new.TeachDashboardPage(self.browser)
+
+    def is_dialog_showing(self):
+        return self.browser.find_element_by_xpath("//div[contains(@class,'ui-dialog')]").is_displayed()
+
+    def confirm_dialog_expect_error(self):
+        self._click_confirm()
+
+        return self
+
+    def _click_confirm(self):
+        self.browser.find_element_by_xpath(
+            "//div[contains(@class,'ui-dialog')]//span[contains(text(),'Confirm')]").click()
+
+    def wait_for_messages(self):
+        self.wait_for_element_by_id('messages')
+
+    def has_students(self):
+        return self.element_exists_by_id('student_table')
diff --git a/portal/tests/test_class_new.py b/portal/tests/test_class_new.py
--- a/portal/tests/test_class_new.py
+++ b/portal/tests/test_class_new.py
@@ -40,7 +40,7 @@
 from utils.organisation_new import create_organisation_directly
 from utils.classes_new import create_class, create_class_directly
 from utils.student_new import create_school_student_directly
-from utils.messages import is_class_created_message_showing
+from utils.messages import is_class_created_message_showing, is_class_nonempty_message_showing
 
 from django_selenium_clean import selenium
 
@@ -83,3 +83,22 @@ def test_create_dashboard(self):
         page, class_name = create_class(page)
 
         assert is_class_created_message_showing(selenium, class_name)
+
+    def test_delete_nonempty(self):
+        email, password = signup_teacher_directly()
+        create_organisation_directly(email)
+        _, class_name, access_code = create_class_directly(email)
+        create_school_student_directly(access_code)
+
+        page = self.go_to_homepage().go_to_login_page().login(email, password)
+        page = page.go_to_class_page()
+
+        page = page.delete_class()
+        assert page.is_dialog_showing()
+        page = page.cancel_dialog()
+        assert not page.is_dialog_showing()
+        page = page.delete_class()
+        page = page.confirm_dialog_expect_error()
+        assert page.__class__.__name__ == 'TeachClassPage'
+        page.wait_for_messages()
+        assert is_class_nonempty_message_showing(selenium)