Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #277 from OpenSPP/253-individual-name-compute-inverse
Browse files Browse the repository at this point in the history
[UPD] compute name on individual
  • Loading branch information
gonzalesedwin1123 authored Dec 7, 2023
2 parents 6d7a8e2 + bc46bd1 commit bc40d75
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 18 deletions.
46 changes: 46 additions & 0 deletions spp_registrant_import/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
class Registrant(models.Model):
_inherit = "res.partner"

name = fields.Char(
compute="_compute_name",
inverse="_inverse_name",
store=True,
)
registrant_id = fields.Char(
string="Registrant ID",
compute="_compute_registrant_id",
Expand Down Expand Up @@ -36,6 +41,8 @@ def _check_registrant_id(self):
raise ValidationError(not_correct_format)
if rec.is_group and rec.registrant_id.startswith("IND_"):
raise ValidationError(not_correct_format)
if not rec.is_group and rec.registrant_id.startswith("GRP_"):
raise ValidationError(not_correct_format)
if any(
[
char in rec.registrant_id.split("_")[-1]
Expand Down Expand Up @@ -67,3 +74,42 @@ def _generate_unique_id(self):
unique_id = "".join(random.choice(allowed_characters) for _ in range(length))

return unique_id

@api.onchange("is_group", "family_name", "given_name", "addl_name")
def name_change(self):
pass

@api.depends(
"is_registrant",
"is_group",
"family_name",
"given_name",
"addl_name",
)
def _compute_name(self):
for rec in self:
if not rec.is_registrant or rec.is_group:
continue
name = []
if rec.family_name:
name.append(rec.family_name)
if rec.given_name:
name.append(rec.given_name)
if rec.addl_name:
name.append(self.addl_name)
rec.name = ", ".join(name).upper()

def _inverse_name(self):
for rec in self:
if not rec.is_registrant or rec.is_group:
continue
name = list(map(lambda i: i.capitalize(), self.name.split(", ")))
if len(name) == 1:
rec.given_name = name[0]
elif len(name) == 2:
rec.family_name = name[0]
rec.given_name = name[1]
else:
rec.family_name = name[0]
rec.given_name = name[1]
rec.addl_name = name[2]
119 changes: 101 additions & 18 deletions spp_registrant_import/tests/test_registrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,37 @@ def setUp(self):
"is_group": True,
}
)
self._test_individuals = self.env["res.partner"].create(
[
{
"name": "Test Individual 1",
"is_registrant": True,
"is_group": False,
},
{
"name": "Test Individual 2",
"is_registrant": True,
"is_group": False,
},
]
)
self._test_individuals = [
self.create_registrant({"name": "TEST, INDIVIDUAL, 1"}),
self.create_registrant({"name": "TEST, INDIVIDUAL, 2"}),
]
self._partner = self.env["res.partner"].create(
{
"name": "Partner 1",
}
)

def create_registrant(self, vals):
assert type(vals) == dict
vals.update(
{
"is_group": False,
"is_registrant": True,
}
)
model = self.env["res.partner"]
# Since we gonna create res_partner records without required field `name`, which violate
# database constraint for this table.
# This is how we ensure everything won't be error:
# 1. Using new() function to create virtual record
# 2. Using virtual record to compute field value [just like Odoo frontend - backend operation]
# 3. Convert virtual record to param and create real record inside Database by create()
# See: https://github.com/odoo/odoo/blob/15.0/addons/stock/tests/common2.py#L13
virtual_rec = model.new(vals)
if "name" not in vals:
virtual_rec._compute_name()
return model.create(virtual_rec._convert_to_write(virtual_rec._cache))

def test_01_compute_registrant_id_normal_partner(self):
self.assertFalse(
bool(self._partner.registrant_id),
Expand Down Expand Up @@ -74,7 +85,7 @@ def test_03_compute_registrant_id_individual(self):

@mute_logger("odoo.sql_db")
def test_04_compute_registrant_id_unique_violation(self):
with self.assertRaises(UniqueViolation):
with (self.env.cr.savepoint(), self.assertRaises(UniqueViolation)):
self._test_individuals[0].write(
{
"registrant_id": self._test_individuals[1].registrant_id,
Expand All @@ -87,10 +98,82 @@ def test_05_check_registrant_id(self):
ValidationError, "^.*not following correct format.{1}$"
):
# 7 characters registrant_id
self._test_household.write({"registrant_id": "GRP_AaAaAa2"})
self._test_household.write({"registrant_id": "GRP_AAAAAA2"})
with self.assertRaisesRegex(
ValidationError, "^.*not following correct format.{1}$"
):
# '1' in registrant_id
self._test_individuals[0].write({"registrant_id": "IND_AaAaAa21"})
self._partner.write({"registrant_id": "IND_AaAaAa21"})
self._test_individuals[0].write({"registrant_id": "IND_AAAAAA21"})
with self.assertRaisesRegex(
ValidationError, "^.*not following correct format.{1}$"
):
# individual with registrant_id starts with GRP_
self._test_individuals[0].write({"registrant_id": "GRP_AAAAAA22"})
with self.assertRaisesRegex(
ValidationError, "^.*not following correct format.{1}$"
):
# group with registrant_id starts with IND_
self._test_household.write({"registrant_id": "IND_AAAAAA22"})
self._partner.write({"registrant_id": "IND_AAAAAA21"})

def test_06__inverse_name(self):
registrant_1 = self._test_individuals[0]
registrant_2 = self._test_individuals[1]
self.assertListEqual(
[registrant_2.given_name, registrant_2.family_name, registrant_2.addl_name],
["Individual", "Test", "2"],
"Set name should set everything correctly with individual!",
)
self.assertListEqual(
[registrant_1.given_name, registrant_1.family_name, registrant_1.addl_name],
["Individual", "Test", "1"],
"Set name should set everything correctly with individual!",
)
test_registrant = self.create_registrant({"name": "NGUYEN, NHAT"})
self.assertListEqual(
[
test_registrant.given_name,
test_registrant.family_name,
test_registrant.addl_name,
],
["Nhat", "Nguyen", False],
"Set name should set everything correctly with individual!",
)
test_registrant = self.create_registrant({"name": "NHAT"})
self.assertListEqual(
[
test_registrant.given_name,
test_registrant.family_name,
test_registrant.addl_name,
],
["Nhat", False, False],
"Set name should set everything correctly with individual!",
)

def test_07__compute_name(self):
test_registrant = self.create_registrant(
{
"given_name": "Nhat",
"family_name": "Nguyen",
"addl_name": "Minh",
}
)
self.assertEqual(
test_registrant.name,
"NGUYEN, NHAT, MINH",
"Compute name should compute for individual!",
)
test_group = self.env["res.partner"].create(
{
"name": "NGUYEN FAMILY",
"family_name": "Nguyen",
"is_registrant": True,
"is_group": True,
}
)
test_group._compute_name()
self.assertEqual(
test_group.name,
"NGUYEN FAMILY",
"Compute name should only compute for individual!",
)

0 comments on commit bc40d75

Please sign in to comment.