Skip to content

Commit

Permalink
[MIG] hr_employee_second_lastname: Migration to 17.0
Browse files Browse the repository at this point in the history
- Fix some new lints.
- Update incoming parameters of post_init_hook method since `cr, registry`
  were replaced by `env` in [1].
- Deprecate the use of _onchange_spec method and use the method new method
  _get_fields_spec to return the fields specification from a view
  description since it was introduced in [2].

[1] odoo/odoo@b4a7996
[2] odoo/odoo@f5e6494
  • Loading branch information
andreagidaltig committed Dec 22, 2023
1 parent 38ac848 commit 2c79f9d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hr_employee_second_lastname/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "HR Employee First Name and Two Last Names",
"version": "16.0.1.0.2",
"version": "17.0.1.0.0",
"author": "Vauxoo, Odoo Community Association (OCA)",
"maintainers": ["luisg123v"],
"website": "https://github.com/OCA/hr",
Expand Down
9 changes: 2 additions & 7 deletions hr_employee_second_lastname/hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from odoo import SUPERUSER_ID
from odoo.api import Environment


def post_init_hook(cr, _):
def post_init_hook(env):
# This SQL statement is necessary to call _install_employee_lastnames() and
# set name fields correctly.
#
Expand All @@ -23,7 +19,6 @@ def post_init_hook(cr, _):
# firstname = 'John'
# lastname = 'Peterson'
# lastname2 = 'Clinton'
cr.execute("UPDATE hr_employee SET firstname = NULL, lastname = NULL")
env = Environment(cr, SUPERUSER_ID, {})
env.cr.execute("UPDATE hr_employee SET firstname = NULL, lastname = NULL")
env["hr.employee"]._install_employee_lastnames()
env["ir.config_parameter"].sudo().set_param("employee_names_order", "first_last")
6 changes: 3 additions & 3 deletions hr_employee_second_lastname/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _get_name_lastnames(self, lastname, firstname, lastname2=None):

def _prepare_vals_on_create_firstname_lastname(self, vals):
values = vals.copy()
res = super(HrEmployee, self)._prepare_vals_on_create_firstname_lastname(values)
res = super()._prepare_vals_on_create_firstname_lastname(values)
if any([field in vals for field in ("firstname", "lastname", "lastname2")]):
vals["name"] = self._get_name_lastnames(
vals.get("lastname"), vals.get("firstname"), vals.get("lastname2")
Expand All @@ -50,7 +50,7 @@ def _prepare_vals_on_create_firstname_lastname(self, vals):

def _prepare_vals_on_write_firstname_lastname(self, vals):
values = vals.copy()
res = super(HrEmployee, self)._prepare_vals_on_write_firstname_lastname(values)
res = super()._prepare_vals_on_write_firstname_lastname(values)
if any([field in vals for field in ("firstname", "lastname", "lastname2")]):
if "lastname" in vals:
lastname = vals["lastname"]
Expand Down Expand Up @@ -97,7 +97,7 @@ def _get_inverse_name(self, name):
return result

Check warning on line 97 in hr_employee_second_lastname/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_employee_second_lastname/models/hr_employee.py#L97

Added line #L97 was not covered by tests

order = self._get_names_order()
result.update(super(HrEmployee, self)._get_inverse_name(name))
result.update(super()._get_inverse_name(name))

if order in ("first_last", "last_first_comma"):
parts = self._split_part("lastname", result)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from odoo import exceptions
from odoo.tests.common import TransactionCase
from odoo.tools import submap


class TestEmployeeLastnames(TransactionCase):
def setUp(self):
super(TestEmployeeLastnames, self).setUp()
super().setUp()
self.env["ir.config_parameter"].sudo().set_param(
"employee_names_order", "first_last"
)
Expand Down Expand Up @@ -45,9 +46,18 @@ def test_get_name_lastnames(self):

def test_onchange(self):
"""Validate the _get_name_lastnames method is not failing"""
field_onchange = self.employee_model.new({})._onchange_spec()
self.assertEqual(field_onchange.get("firstname"), "1")
self.assertEqual(field_onchange.get("lastname"), "1")
# Check that fields used to generate the name and also name field
# are empty before onchange
fields_spec = self.env["hr.employee"]._get_fields_spec()
self.assertEqual(
submap(fields_spec, ("firstname", "lastname", "lastname2", "name")),
{
"firstname": {},
"lastname": {},
"lastname2": {},
"name": {},
},
)
values = {
"firstname": "Pedro",
"lastname": "Perez",
Expand All @@ -61,7 +71,7 @@ def test_onchange(self):
new_record = self.employee_model.new(values)

updates = new_record.onchange(
values, ["firstname", "lastname", "lastname2"], field_onchange
values, ["firstname", "lastname", "lastname2"], fields_spec
)
values.update(updates.get("value", {}))
self.assertEqual(values["name"], "Pedro Perez Hernandez")
Expand Down

0 comments on commit 2c79f9d

Please sign in to comment.