Fixed on_delete field attr. Fixed #4405

This commit is contained in:
Norman Jäckel 2019-03-01 20:51:42 +01:00
parent 4366f753f4
commit 9eec2404dd
2 changed files with 53 additions and 15 deletions

View File

@ -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)

View File

@ -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())