add logic for computing followers when team staff changes

This commit is contained in:
Marc Durepos 2024-08-27 13:25:40 -04:00
parent e3198168c1
commit 03227bb5f4
2 changed files with 17 additions and 0 deletions

View file

@ -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)

View file

@ -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')