From bb210f8d47327551db4e17d7e4fe8a58defab166 Mon Sep 17 00:00:00 2001 From: imperosol Date: Sat, 4 Jan 2025 16:43:38 +0100 Subject: [PATCH] change club group names when the club name changes --- club/models.py | 13 ++++++++++--- club/tests.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/club/models.py b/club/models.py index 8785b517..4184715a 100644 --- a/club/models.py +++ b/club/models.py @@ -106,9 +106,16 @@ class Club(models.Model): @transaction.atomic() def save(self, *args, **kwargs): creation = self._state.adding - if not creation and Club.objects.get(id=self.id).unix_name != self.unix_name: - self.home.name = self.unix_name - self.home.save() + if not creation: + db_club = Club.objects.get(id=self.id) + if self.unix_name != db_club.unix_name: + self.home.name = self.unix_name + self.home.save() + if self.name != db_club.name: + self.board_group.name = f"{self.name} - Bureau" + self.board_group.save() + self.members_group.name = f"{self.name} - Membres" + self.members_group.save() if creation: self.board_group = Group.objects.create( name=f"{self.name} - Bureau", is_manually_manageable=False diff --git a/club/tests.py b/club/tests.py index 5844eeff..b81aa38d 100644 --- a/club/tests.py +++ b/club/tests.py @@ -548,6 +548,26 @@ class TestClubModel(TestClub): Membership(club=self.ae, user=self.sli, role=3).save() assert self.club.is_owned_by(self.sli) + def test_change_club_name(self): + """Test that changing the club name doesn't break things.""" + members_group = self.club.members_group + board_group = self.club.board_group + initial_members = set(members_group.users.values_list("id", flat=True)) + initial_board = set(board_group.users.values_list("id", flat=True)) + self.club.name = "something else" + self.club.save() + self.club.refresh_from_db() + + # The names should have changed, but not the ids nor the group members + assert self.club.members_group.name == "something else - Membres" + assert self.club.board_group.name == "something else - Bureau" + assert self.club.members_group.id == members_group.id + assert self.club.board_group.id == board_group.id + new_members = set(self.club.members_group.users.values_list("id", flat=True)) + new_board = set(self.club.board_group.users.values_list("id", flat=True)) + assert new_members == initial_members + assert new_board == initial_board + class TestMailingForm(TestCase): """Perform validation tests for MailingForm."""