diff --git a/aidants_connect_web/admin.py b/aidants_connect_web/admin.py index 84e858634..d184d8b53 100644 --- a/aidants_connect_web/admin.py +++ b/aidants_connect_web/admin.py @@ -743,6 +743,22 @@ class UsagerAdmin(VisibleToTechAdmin, NestedModelAdmin, TabbedModelAdmin): tab_mandats = (UsagerMandatInline,) tabs = [("Informations", tab_infos), ("Mandats", tab_mandats)] + actions = ("specific_deleted_action",) + + def get_actions(self, request): + actions = super(UsagerAdmin, self).get_actions(request) + try: + del actions["delete_selected"] + except KeyError: + pass + return actions + + def specific_deleted_action(self, request, queryset): + for usager in queryset: + usager.clean_journal_entries_and_delete_mandats() + usager.delete() + + specific_deleted_action.short_description = "Supprimer les usagers sélectionnés" class MandatAutorisationInline(VisibleToTechAdmin, TabularInline): @@ -905,13 +921,25 @@ class ConnectionAdmin(ModelAdmin): class JournalAdmin(VisibleToTechAdmin, ModelAdmin): - list_display = ("id", "action", "aidant", "creation_date") + list_display = ( + "creation_date", + "action", + "aidant", + "usager", + "additional_information", + "id", + ) list_filter = ("action",) + search_fields = ( "action", "aidant__first_name", "aidant__last_name", "aidant__email", + "usager__family_name", + "usager__given_name", + "usager__email", + "additional_information", ) ordering = ("-creation_date",) diff --git a/aidants_connect_web/models.py b/aidants_connect_web/models.py index 00960d65e..55d401341 100644 --- a/aidants_connect_web/models.py +++ b/aidants_connect_web/models.py @@ -5,7 +5,8 @@ from django.contrib.auth.models import AbstractUser, UserManager from django.contrib.postgres.fields import ArrayField from django.db import models, transaction -from django.db.models import Q, QuerySet, SET_NULL, CASCADE +from django.db.models import Q, QuerySet, SET_NULL, CASCADE, Value +from django.db.models.functions import Concat from django.dispatch import Signal from django.template import loader, defaultfilters from django.urls import reverse @@ -567,6 +568,39 @@ def normalize_birthplace(self): return self.birthplace + def clean_journal_entries_and_delete_mandats(self): + today = timezone.now() + str_today = today.strftime("%d/%m/%Y à %Hh%M") + for mandat in self.mandats.all(): + entries = Journal.objects.filter(mandat=mandat) + manda_str_add_inf = ( + f"Added by clean_journal_entries_and_delete_mandats :" + f"\n Relatif au mandat supprimé {mandat} le {str_today}" + ) + entries.update( + mandat=None, + additional_information=Concat( + "additional_information", Value(manda_str_add_inf) + ), + ) + mandat.delete() + + entries = Journal.objects.filter(usager=self) + + usager_str_add_inf = ( + f"Add by clean_journal_entries_and_delete_mandats :" + f"\n Relatif à l'usager supprimé {self.family_name} " + f"{self.given_name} {self.preferred_username} " + f"{self.email} le {str_today}" + ) + entries.update( + usager=None, + additional_information=Concat( + "additional_information", Value(usager_str_add_inf) + ), + ) + entries.delete() + def get_staff_organisation_name_id() -> int: try: diff --git a/aidants_connect_web/tests/test_models.py b/aidants_connect_web/tests/test_models.py index ecfcbd0bc..7f77d6797 100644 --- a/aidants_connect_web/tests/test_models.py +++ b/aidants_connect_web/tests/test_models.py @@ -1595,3 +1595,42 @@ def test_do_not_validate_if_invalid_status(self): ) db_hab_request = HabilitationRequest.objects.get(id=habilitation_request.id) self.assertEqual(db_hab_request.status, status) + + +@tag("models", "manndat", "usager", "journal") +class UsagerDeleteTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.aidant_marge = AidantFactory() + cls.usager_homer = UsagerFactory(given_name="Homer") + cls.mandat_marge_homer = MandatFactory( + organisation=cls.aidant_marge.organisation, + usager=cls.usager_homer, + expiration_date=timezone.now() - timedelta(days=6), + ) + cls.autorisation = AutorisationFactory( + mandat=cls.mandat_marge_homer, + demarche="Carte grise", + ) + Journal.log_autorisation_creation(cls.autorisation, aidant=cls.aidant_marge) + Journal.log_franceconnection_usager( + aidant=cls.aidant_marge, + usager=cls.usager_homer, + ) + Journal.log_mandat_cancel( + aidant=cls.aidant_marge, mandat=cls.mandat_marge_homer + ) + + def test_usager_clean_journal_entries_and_delete_mandats(self): + self.assertEqual(len(Journal.objects.all()), 3) + testing = "Add by clean_journal_entries_and_delete_mandats" + self.usager_homer.clean_journal_entries_and_delete_mandats() + self.assertEqual(len(Journal.objects.all()), 3) + self.assertEqual( + len(Journal.objects.filter(additional_information__icontains=testing)), + 3, + ) + self.usager_homer.delete() + self.assertEqual(len(Usager.objects.all()), 0) + self.assertEqual(len(Mandat.objects.all()), 0) + self.assertEqual(len(Journal.objects.all()), 3)