Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deleting features for Usager #512

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion aidants_connect_web/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Donc ce que ça fait là, c'est que tu supprimes l'action "Supprimer" normale ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oui parce que dans notre cas, elle ne peut pas fonctionner.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est un dictionnaire ? Tu pouvais utiliser .pop() pour t'éviter le try ... except mais je pinaille.

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):
Expand Down Expand Up @@ -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",)

Expand Down
36 changes: 35 additions & 1 deletion aidants_connect_web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions aidants_connect_web/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)