From 03227bb5f4d7f7705948ce902b653c01c3aefad8 Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Tue, 27 Aug 2024 13:25:40 -0400 Subject: [PATCH] add logic for computing followers when team staff changes --- bemade_sports_clinic/models/patient.py | 15 +++++++++++++++ bemade_sports_clinic/models/sports_team.py | 2 ++ 2 files changed, 17 insertions(+) diff --git a/bemade_sports_clinic/models/patient.py b/bemade_sports_clinic/models/patient.py index ad5cc28..2916ad8 100644 --- a/bemade_sports_clinic/models/patient.py +++ b/bemade_sports_clinic/models/patient.py @@ -253,3 +253,18 @@ class Patient(models.Model): } ) return res + + def recompute_followers(self): + """ Recompute the followers for this patient (and its injuries) based on the + changes to a specific team's staff members. Ignoring manually unsubscribed + followers, the set of followers should be the set of staff on all teams the + patient is part of.""" + for patient in self: + current_followers = patient.message_partner_ids + future_followers = patient.team_ids.mapped('staff_ids').mapped('partner_id') + removed_followers = current_followers - future_followers + added_followers = future_followers - current_followers + patient.message_unsubscribe(removed_followers.ids) + patient.message_subscribe(added_followers.ids) + patient.injury_ids.message_unsubscribe(removed_followers.ids) + patient.injury_ids.message_subscribe(added_followers.ids) \ No newline at end of file diff --git a/bemade_sports_clinic/models/sports_team.py b/bemade_sports_clinic/models/sports_team.py index 11e7225..452bd9e 100644 --- a/bemade_sports_clinic/models/sports_team.py +++ b/bemade_sports_clinic/models/sports_team.py @@ -56,6 +56,7 @@ class SportsTeam(models.Model): res = super().write(vals) if 'staff_ids' in vals: self._allow_access_for_staff_internal_users() + self.patient_ids.recompute_followers() return res @api.model_create_multi @@ -64,6 +65,7 @@ class SportsTeam(models.Model): for index, rec in enumerate(res): if 'staff_ids' in vals_list[index]: rec._allow_access_for_staff_internal_users() + rec.patient_ids.recompute_followers() return res @api.depends('patient_ids.is_injured')