[FIX] sports_clinic: patient name updates on partner.
This commit is contained in:
parent
2c4e197c2b
commit
4eb7f14505
3 changed files with 32 additions and 11 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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.")
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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])
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue