From 30e07d4175a09355e272169b407fdebf813c0803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Vergez?= Date: Fri, 23 Feb 2024 15:47:34 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20relation=20inverse=20vers=20le?= =?UTF-8?q?=20propri=C3=A9taire=20pour=20les=20notifications?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utile d'une manière générale, obligatoire pour certaines notifications à venir --- ...r_notification_owner_structure_and_more.py | 66 +++++++++++++++++++ dora/notifications/models.py | 3 + dora/notifications/tests.py | 36 ++++++++++ 3 files changed, 105 insertions(+) create mode 100644 dora/notifications/migrations/0003_alter_notification_owner_structure_and_more.py diff --git a/dora/notifications/migrations/0003_alter_notification_owner_structure_and_more.py b/dora/notifications/migrations/0003_alter_notification_owner_structure_and_more.py new file mode 100644 index 00000000..b1b5ac45 --- /dev/null +++ b/dora/notifications/migrations/0003_alter_notification_owner_structure_and_more.py @@ -0,0 +1,66 @@ +# Generated by Django 4.2.10 on 2024-02-23 14:27 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("structures", "0063_structure_is_obsolete"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("notifications", "0002_remove_notification_check_structure_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="notification", + name="owner_structure", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications", + to="structures.structure", + verbose_name="Structure propriétaire", + ), + ), + migrations.AlterField( + model_name="notification", + name="owner_structureputativemember", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications", + to="structures.structureputativemember", + verbose_name="Invitation propriétaire", + ), + ), + migrations.AlterField( + model_name="notification", + name="owner_user", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications", + to=settings.AUTH_USER_MODEL, + verbose_name="Utilisateur propriétaire", + ), + ), + migrations.AlterField( + model_name="notification", + name="task_type", + field=models.CharField( + choices=[ + ("orphan_structures", "Orphan Structures"), + ("service_activation", "Service Activation"), + ("invited_users", "Invited Users"), + ("self_invited_users", "Self Invited Users"), + ("generic_task", "Generic Task"), + ], + verbose_name="Type de tâche", + ), + ), + ] diff --git a/dora/notifications/models.py b/dora/notifications/models.py index 58e64b7d..8366a542 100644 --- a/dora/notifications/models.py +++ b/dora/notifications/models.py @@ -55,6 +55,7 @@ class Notification(models.Model): blank=True, on_delete=models.CASCADE, verbose_name="Structure propriétaire", + related_name="notifications", ) owner_user = models.ForeignKey( User, @@ -62,6 +63,7 @@ class Notification(models.Model): blank=True, on_delete=models.CASCADE, verbose_name="Utilisateur propriétaire", + related_name="notifications", ) owner_structureputativemember = models.ForeignKey( StructurePutativeMember, @@ -69,6 +71,7 @@ class Notification(models.Model): blank=True, on_delete=models.CASCADE, verbose_name="Invitation propriétaire", + related_name="notifications", ) ... diff --git a/dora/notifications/tests.py b/dora/notifications/tests.py index 47281875..bf00a8ab 100644 --- a/dora/notifications/tests.py +++ b/dora/notifications/tests.py @@ -38,6 +38,42 @@ def test_notification_str(notification, status): assert notification.task_type in str(notification) +def test_related_objects(): + structure = make_structure() + + assert not structure.notifications.count() + + notification = Notification( + owner_structure=structure, task_type=TaskType.GENERIC_TASK + ) + notification.save() + + assert notification == structure.notifications.first() + + user = make_user() + + assert not user.notifications.count() + + notification = Notification(owner_user=user, task_type=TaskType.GENERIC_TASK) + notification.save() + + assert notification == user.notifications.first() + + putative_membership = make_structure( + putative_member=make_user() + ).putative_membership.first() + + assert not putative_membership.notifications.count() + + notification = Notification( + owner_structureputativemember=putative_membership, + task_type=TaskType.GENERIC_TASK, + ) + notification.save() + + assert notification == putative_membership.notifications.first() + + def test_constraints(notification): notification.owner_structure = None