From 4eb7f14505586498bfc0b1bc3d3a6afee613dbda Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Sun, 29 Sep 2024 16:18:00 -0400 Subject: [PATCH] [FIX] sports_clinic: patient name updates on partner. --- bemade_sports_clinic/models/patient.py | 22 +++++++++++++++------- bemade_sports_clinic/models/res_partner.py | 8 ++++++-- bemade_sports_clinic/tests/test_patient.py | 13 +++++++++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/bemade_sports_clinic/models/patient.py b/bemade_sports_clinic/models/patient.py index 123add1..c28ccb3 100644 --- a/bemade_sports_clinic/models/patient.py +++ b/bemade_sports_clinic/models/patient.py @@ -38,7 +38,7 @@ class Patient(models.Model): first_name = fields.Char(required=True, tracking=True) last_name = fields.Char(required=True, tracking=True) name = fields.Char( - related="partner_id.name", compute="_compute_name", compute_sudo=True + related="partner_id.name", ) phone = fields.Char(related="partner_id.phone", readonly=False) mobile = fields.Char(related="partner_id.mobile", readonly=False) @@ -133,8 +133,16 @@ class Patient(models.Model): res = super().write(values) if "team_ids" in values: self.sudo().recompute_followers() + if "first_name" in values or "last_name" in values: + self._recompute_name() return res + def _recompute_name(self): + for rec in self: + rec.partner_id.with_context(patient_update=True).name = ( + rec._get_name_from_first_and_last(rec.first_name, rec.last_name) + ) + @api.model_create_multi def create(self, vals_list): for row in vals_list: @@ -203,14 +211,14 @@ class Patient(models.Model): else: rec.age = relativedelta(date.today(), rec.date_of_birth).years - @api.depends("first_name", "last_name") - def _compute_name(self): - for rec in self: - rec.name = self._get_name_from_first_and_last(rec.first_name, rec.last_name) - @api.model def _get_name_from_first_and_last(self, first_name, last_name): - return ((first_name or "") + " " + (last_name or "")).strip() + names = [] + if first_name: + names.append(first_name) + if last_name: + names.append(last_name) + return " ".join(names) @api.depends("practice_status", "match_status", "injury_ids.injury_date") def _compute_is_injured(self): diff --git a/bemade_sports_clinic/models/res_partner.py b/bemade_sports_clinic/models/res_partner.py index 99841ce..7698b7d 100644 --- a/bemade_sports_clinic/models/res_partner.py +++ b/bemade_sports_clinic/models/res_partner.py @@ -28,7 +28,11 @@ class Partner(models.Model): ) def write(self, vals): - if self.patient_ids and "name" in vals: + if ( + self.patient_ids + and "name" in vals + and not self._context.get("patient_update") + ): raise ValidationError( _("To change a patient's name, change it from the patient form.") ) @@ -49,5 +53,5 @@ class Partner(models.Model): for team in rec.teams_served_ids: if team not in served_teams: raise UserError( - "To add a staff member to a team, use the team view." + _("To add a staff member to a team, use the team view.") ) diff --git a/bemade_sports_clinic/tests/test_patient.py b/bemade_sports_clinic/tests/test_patient.py index ece8524..69c3c52 100644 --- a/bemade_sports_clinic/tests/test_patient.py +++ b/bemade_sports_clinic/tests/test_patient.py @@ -1,9 +1,8 @@ -from odoo.tests import TransactionCase, tagged +from odoo.tests import TransactionCase, tagged, Form from odoo import fields, Command from datetime import timedelta - @tagged("-at_install", "post_install") class TestPatient(TransactionCase): @classmethod @@ -211,3 +210,13 @@ class TestPatient(TransactionCase): .partner_id ) return team2, therapist, coach + + def test_changing_patient_name_changes_on_partner(self): + new_last_name = "New last name" + new_first_name = "New first name" + with Form(self.patient1) as patient: + patient.last_name = new_last_name + patient.first_name = new_first_name + self.assertEqual( + self.patient1.partner_id.name, " ".join([new_first_name, new_last_name]) + )