From 9eec2404ddcc47a27a23b091f6abc22523f2b4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Fri, 1 Mar 2019 20:51:42 +0100 Subject: [PATCH] Fixed on_delete field attr. Fixed #4405 --- openslides/utils/models.py | 26 ++++++-------- tests/integration/motions/test_viewset.py | 42 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/openslides/utils/models.py b/openslides/utils/models.py index d997ed2aa..e106e47d1 100644 --- a/openslides/utils/models.py +++ b/openslides/utils/models.py @@ -163,12 +163,11 @@ def SET_NULL_AND_AUTOUPDATE( Like models.SET_NULL but also informs the autoupdate system about the instance that was reference. """ - if len(sub_objs) != 1: - raise RuntimeError( - "SET_NULL_AND_AUTOUPDATE is used in an invalid usecase. Please report the bug!" - ) - setattr(sub_objs[0], field.name, None) - inform_changed_data(sub_objs[0]) + instances = [] + for sub_obj in sub_objs: + setattr(sub_obj, field.name, None) + instances.append(sub_obj) + inform_changed_data(instances) models.SET_NULL(collector, field, sub_objs, using) @@ -179,19 +178,16 @@ def CASCADE_AND_AUTOUODATE( Like models.CASCADE but also informs the autoupdate system about the root rest element of the also deleted instance. """ - if len(sub_objs) != 1: - raise RuntimeError( - "CASCADE_AND_AUTOUPDATE is used in an invalid usecase. Please report the bug!" - ) - root_rest_element = sub_objs[0].get_root_rest_element() - inform_changed_elements( - [ + elements = [] + for sub_obj in sub_objs: + root_rest_element = sub_obj.get_root_rest_element() + elements.append( Element( collection_string=root_rest_element.get_collection_string(), id=root_rest_element.pk, full_data=None, reload=True, ) - ] - ) + ) + inform_changed_elements(elements) models.CASCADE(collector, field, sub_objs, using) diff --git a/tests/integration/motions/test_viewset.py b/tests/integration/motions/test_viewset.py index f737da93f..4417e4e0b 100644 --- a/tests/integration/motions/test_viewset.py +++ b/tests/integration/motions/test_viewset.py @@ -696,6 +696,18 @@ class DeleteMotion(TestCase): motions = Motion.objects.count() self.assertEqual(motions, 0) + def test_delete_with_two_change_recommendations(self): + self.cr1 = MotionChangeRecommendation.objects.create( + motion=self.motion, internal=False, line_from=1, line_to=1 + ) + self.cr2 = MotionChangeRecommendation.objects.create( + motion=self.motion, internal=False, line_from=2, line_to=2 + ) + response = self.client.delete(reverse("motion-detail", args=[self.motion.pk])) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + motions = Motion.objects.count() + self.assertEqual(motions, 0) + class ManageMultipleSubmitters(TestCase): """ @@ -1966,3 +1978,33 @@ class DeleteWorkflow(TestCase): ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Workflow.objects.count(), 2) + + +class DeleteCategory(TestCase): + """ + Tests the deletion of categories. + """ + + def setUp(self): + self.client = APIClient() + self.client.login(username="admin", password="admin") + self.category = Category.objects.create( + name="test_name_dacei2iiTh", prefix="test_prefix_lahngoaW9L" + ) + + def test_simple_delete_category_with_two_motions(self): + self.motion1 = Motion.objects.create( + title="test_title_fieB5ko4ahGeex5ohsh7", + text="test_text_EFoh6Ahtho9eihei1xua", + category=self.category, + ) + self.motion2 = Motion.objects.create( + title="test_title_ahboo8eerohchuoD7sut", + text="test_text_pahghah9iuM9moo8Ohve", + category=self.category, + ) + response = self.client.delete( + reverse("category-detail", args=[self.category.pk]) + ) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertFalse(Category.objects.exists())