diff --git a/emeis/core/migrations/0009_alter_scope_parent.py b/emeis/core/migrations/0009_alter_scope_parent.py new file mode 100644 index 00000000..311c3f95 --- /dev/null +++ b/emeis/core/migrations/0009_alter_scope_parent.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.12 on 2022-04-21 07:24 + +from django.db import migrations +import django.db.models.deletion +import mptt.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ("emeis_core", "0008_scope_is_active"), + ] + + operations = [ + migrations.AlterField( + model_name="scope", + name="parent", + field=mptt.fields.TreeForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="children", + to="emeis_core.scope", + ), + ), + ] diff --git a/emeis/core/models.py b/emeis/core/models.py index 1dc2ad0e..5d82811c 100644 --- a/emeis/core/models.py +++ b/emeis/core/models.py @@ -163,7 +163,7 @@ class Scope(MPTTModel, UUIDModel): ) parent = TreeForeignKey( "self", - on_delete=models.SET_NULL, + on_delete=models.CASCADE, null=True, blank=True, related_name="children", diff --git a/emeis/core/tests/test_models.py b/emeis/core/tests/test_models.py index 9346085f..8a86bfd2 100644 --- a/emeis/core/tests/test_models.py +++ b/emeis/core/tests/test_models.py @@ -42,6 +42,22 @@ def test_scope_model(db): parent_scope.save() +def test_scope_deletion(db, scope_factory): + root = scope_factory() # not deleted + child = scope_factory(parent=root) + grandchild = scope_factory(parent=child) + scope_factory(parent=grandchild) + scope_factory(parent=grandchild) + + other_child = scope_factory(parent=root) # not deleted + scope_factory(parent=other_child) # not deleted + + child.delete() + + assert Scope.objects.count() == 3 + assert list(Scope.objects.root_nodes()) == [root] + + def test_can_authenticate(db, user): # Test whether the authentication mechanism works correctly user.set_password("test_password")