Skip to content

Commit

Permalink
[MIG] hr_employee_lastnames: Migration to 17.0
Browse files Browse the repository at this point in the history
- 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].
- Rename hooks file to follow guidelines.

[1] odoo/odoo@b4a7996
[2] odoo/odoo@f5e6494
  • Loading branch information
andreagidaltig committed Nov 16, 2023
1 parent 3542976 commit 4b4c2d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion hr_employee_lastnames/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import models
from .hook import post_init_hook
from .hooks import post_init_hook
2 changes: 1 addition & 1 deletion hr_employee_lastnames/__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.1",
"version": "17.0.1.0.0",
"author": "Vauxoo, Odoo Community Association (OCA)",
"maintainer": "Vauxoo",
"website": "https://github.com/OCA/hr",
Expand Down
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")
18 changes: 14 additions & 4 deletions hr_employee_lastnames/tests/test_hr_employee_lastnames.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from odoo import exceptions
from odoo.tests.common import TransactionCase
from odoo.tools import submap


class TestEmployeeLastnames(TransactionCase):
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 4b4c2d0

Please sign in to comment.