Merge pull request #4447 from normanjaeckel/FixOnDelete

Fixed on_delete field attr. Fixed #4405
This commit is contained in:
Emanuel Schütze 2019-03-04 11:12:01 +01:00 committed by GitHub
commit 13eb764668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 Like models.SET_NULL but also informs the autoupdate system about the
instance that was reference. instance that was reference.
""" """
if len(sub_objs) != 1: instances = []
raise RuntimeError( for sub_obj in sub_objs:
"SET_NULL_AND_AUTOUPDATE is used in an invalid usecase. Please report the bug!" setattr(sub_obj, field.name, None)
) instances.append(sub_obj)
setattr(sub_objs[0], field.name, None) inform_changed_data(instances)
inform_changed_data(sub_objs[0])
models.SET_NULL(collector, field, sub_objs, using) 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 Like models.CASCADE but also informs the autoupdate system about the
root rest element of the also deleted instance. root rest element of the also deleted instance.
""" """
if len(sub_objs) != 1: elements = []
raise RuntimeError( for sub_obj in sub_objs:
"CASCADE_AND_AUTOUPDATE is used in an invalid usecase. Please report the bug!" root_rest_element = sub_obj.get_root_rest_element()
) elements.append(
root_rest_element = sub_objs[0].get_root_rest_element()
inform_changed_elements(
[
Element( Element(
collection_string=root_rest_element.get_collection_string(), collection_string=root_rest_element.get_collection_string(),
id=root_rest_element.pk, id=root_rest_element.pk,
full_data=None, full_data=None,
reload=True, reload=True,
) )
] )
) inform_changed_elements(elements)
models.CASCADE(collector, field, sub_objs, using) models.CASCADE(collector, field, sub_objs, using)

View File

@ -696,6 +696,18 @@ class DeleteMotion(TestCase):
motions = Motion.objects.count() motions = Motion.objects.count()
self.assertEqual(motions, 0) 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): class ManageMultipleSubmitters(TestCase):
""" """
@ -1966,3 +1978,33 @@ class DeleteWorkflow(TestCase):
) )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Workflow.objects.count(), 2) 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())