Skip to content

Commit

Permalink
[13.0][MIG] hr_job_category
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Arroyo committed Sep 30, 2020
1 parent d858a37 commit a0ff71a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 66 deletions.
10 changes: 5 additions & 5 deletions hr_job_category/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ HR Job Employee Categories
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github
:target: https://github.com/OCA/hr/tree/12.0/hr_job_category
:target: https://github.com/OCA/hr/tree/13.0/hr_job_category
:alt: OCA/hr
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-12-0/hr-12-0-hr_job_category
:target: https://translation.odoo-community.org/projects/hr-13-0/hr-13-0-hr_job_category
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/116/12.0
:target: https://runbot.odoo-community.org/runbot/116/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -64,7 +64,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/hr/issues/new?body=module:%20hr_job_category%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/hr/issues/new?body=module:%20hr_job_category%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -99,6 +99,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/12.0/hr_job_category>`_ project on GitHub.
This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/13.0/hr_job_category>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
6 changes: 3 additions & 3 deletions hr_job_category/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "HR Job Employee Categories",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"category": "Generic Modules/Human Resources",
"summary": "Adds tags to employee trough contract and job position",
"author": "Michael Telahun Makonnen <mmakonnen@gmail.com>, "
Expand All @@ -12,7 +12,7 @@
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr",
"license": "AGPL-3",
"depends": ["hr_contract",],
"data": ["views/hr_view.xml",],
"depends": ["hr_contract"],
"data": ["views/hr_view.xml"],
"installable": True,
}
65 changes: 20 additions & 45 deletions hr_job_category/models/hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,32 @@ class HRJob(models.Model):
class HRContract(models.Model):
_inherit = "hr.contract"

@api.multi
def _remove_tags(self, employee_id=None, job_id=None):
# TODO write tags only once
if not employee_id or not job_id:
return
employee = self.env["hr.employee"].browse(employee_id)
empl_tags = employee.category_ids
job = self.env["hr.job"].browse(job_id)
_logger.debug(
"Removing employee tags if tag exists on contract " "job: %s", empl_tags
)
for tag in job.category_ids:
if tag in empl_tags:
employee.write({"category_ids": [(3, tag.id)]})

@api.multi
def _tag_employees(self, employee_id=None, job_id=None):
if not employee_id or not job_id:
return
employee = self.env["hr.employee"].browse(employee_id)
empl_tags = employee.category_ids
job = self.env["hr.job"].browse(job_id)
for tag in job.category_ids:
if tag not in empl_tags:
_logger.debug(
"Adding employee tag if job tag doesn't " "exists: %s", tag.name
)
employee.write({"category_ids": [(4, tag.id)]})
def _tag_employees(self, job_id):
if job_id:
job = self.env["hr.job"].browse(job_id)
self.mapped("employee_id").write(
{"category_ids": [(6, 0, job.category_ids.ids)]}
)
else:
for contract in self:
categories = contract.job_id and contract.job_id.category_ids.ids or []
contract.employee_id.write({"category_ids": [(6, 0, categories)]})

@api.model
def create(self, vals):
res = super().create(vals)
self._tag_employees(vals.get("employee_id", False), vals.get("job_id", False))
if "job_id" in vals:
res._tag_employees(vals.get("job_id"))
return res

@api.multi
def write(self, vals):
prev_data = self.read(["job_id"])

if "employee_id" in vals:
self.mapped("employee_id").write({"category_ids": [(5,)]})
res = super().write(vals)

# Go through each record and delete tags associated with the previous
# job, then add the tags of the new job.
#
for contract in self:
for data in prev_data:
if (
data.get("id") == contract.id
and data["job_id"]
and data["job_id"][0] != contract.job_id.id
):
self._remove_tags(contract.employee_id.id, data["job_id"][0])
self._tag_employees(contract.employee_id.id, contract.job_id.id)
if "job_id" in vals or ("employee_id" in vals and vals["employee_id"]):
self._tag_employees(vals.get("job_id"))
return res

def unlink(self):
self.mapped("employee_id").write({"category_ids": [(5,)]})
return super().unlink()
8 changes: 4 additions & 4 deletions hr_job_category/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils 0.16: http://docutils.sourceforge.net/" />
<title>HR Job Employee Categories</title>
<style type="text/css">

Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">HR Job Employee Categories</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/hr/tree/12.0/hr_job_category"><img alt="OCA/hr" src="https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/hr-12-0/hr-12-0-hr_job_category"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/116/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/hr/tree/13.0/hr_job_category"><img alt="OCA/hr" src="https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/hr-13-0/hr-13-0-hr_job_category"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/116/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module was written to extend the functionality of jobs
to support tagging employees based on their job positions.
For example, all Supervisors could be attached to the Supervisors category.
Expand Down Expand Up @@ -412,7 +412,7 @@ <h1><a class="toc-backref" href="#id3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/hr/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/hr/issues/new?body=module:%20hr_job_category%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/hr/issues/new?body=module:%20hr_job_category%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -441,7 +441,7 @@ <h2><a class="toc-backref" href="#id7">Maintainers</a></h2>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr/tree/12.0/hr_job_category">OCA/hr</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr/tree/13.0/hr_job_category">OCA/hr</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
21 changes: 17 additions & 4 deletions hr_job_category/tests/test_hr_job_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def setUp(self):
self.contract_model = self.env["hr.contract"]

# Create a employee
self.employee_id = self.employee_model.create({"name": "Employee 1"})
self.employee_id_1 = self.employee_model.create({"name": "Employee 1"})
self.employee_id_2 = self.employee_model.create({"name": "Employee 2"})

# Create two employee categories
self.categ_id = self.employee_categ_model.create({"name": "Category 1"})
Expand All @@ -31,7 +32,7 @@ def setUp(self):

# Create one contract
self.contract_id = self.contract_model.create(
{"name": "Contract 1", "employee_id": self.employee_id.id, "wage": 50000}
{"name": "Contract 1", "employee_id": self.employee_id_1.id, "wage": 50000}
)

def test_write_computes_with_normal_args(self):
Expand All @@ -44,13 +45,25 @@ def test_write_computes_with_normal_args(self):
# Check if job categories are written to the employee
self.contract_id.write({"job_id": self.job_id.id})
job_categ = [categ.id for categ in self.job_id.category_ids]
empl_categ = [categ.id for categ in self.employee_id.category_ids]
empl_categ = [categ.id for categ in self.employee_id_1.category_ids]

self.assertTrue(all(x in empl_categ for x in job_categ))

self.contract_id.write({"job_id": False})
self.assertFalse(self.employee_id_1.category_ids)

# Check if job2 categories are written to the employee
self.contract_id.write({"job_id": self.job_2_id.id})
job_categ = [categ.id for categ in self.job_2_id.category_ids]
empl_categ = [categ.id for categ in self.employee_id.category_ids]
empl_categ = [categ.id for categ in self.employee_id_1.category_ids]

self.assertTrue(all(x in empl_categ for x in job_categ))

self.contract_id.write({"employee_id": self.employee_id_2.id})
self.assertFalse(self.employee_id_1.category_ids)
job_categ = [categ.id for categ in self.job_2_id.category_ids]
empl_categ = [categ.id for categ in self.employee_id_2.category_ids]
self.assertTrue(all(x in empl_categ for x in job_categ))

self.contract_id.unlink()
self.assertFalse(self.employee_id_2.category_ids)
6 changes: 1 addition & 5 deletions hr_job_category/views/hr_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
<field name="inherit_id" ref="hr.view_hr_job_form" />
<field name="arch" type="xml">
<xpath expr="//div[hasclass('oe_title')]" position="inside">
<label
for="category_ids"
class="oe_edit_only"
groups="hr.group_hr_manager"
/>
<label for="category_ids" groups="hr.group_hr_manager" />
<field
name="category_ids"
widget="many2many_tags"
Expand Down

0 comments on commit a0ff71a

Please sign in to comment.