Skip to content

Commit

Permalink
[IMP] hr_job_category: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Arroyo committed Sep 29, 2020
1 parent eb65632 commit d858a37
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 73 deletions.
30 changes: 13 additions & 17 deletions hr_job_category/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'HR Job Employee Categories',
'version': '12.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>, "
"Savoir-faire Linux, "
"Fekete Mihai (FBSR), "
"Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/hr',
'license': 'AGPL-3',
'depends': [
'hr_contract',
],
'data': [
'views/hr_view.xml',
],
'installable': True,
"name": "HR Job Employee Categories",
"version": "12.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>, "
"Savoir-faire Linux, "
"Fekete Mihai (FBSR), "
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr",
"license": "AGPL-3",
"depends": ["hr_contract",],
"data": ["views/hr_view.xml",],
"installable": True,
}
57 changes: 30 additions & 27 deletions hr_job_category/models/hr.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
# Copyright 2013 Michael Telahun Makonnen <mmakonnen@gmail.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api
import logging

from odoo import api, fields, models

_logger = logging.getLogger(__name__)


class HRJob(models.Model):
_inherit = 'hr.job'
_inherit = "hr.job"

category_ids = fields.Many2many(
'hr.employee.category',
'job_category_rel',
'job_id',
'category_id',
string='Associated Tags'
"hr.employee.category",
"job_category_rel",
"job_id",
"category_id",
string="Associated Tags",
)


class HRContract(models.Model):
_inherit = 'hr.contract'
_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)
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)
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)]})
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)
employee = self.env["hr.employee"].browse(employee_id)
empl_tags = employee.category_ids
job = self.env['hr.job'].browse(job_id)
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)]})
_logger.debug(
"Adding employee tag if job tag doesn't " "exists: %s", tag.name
)
employee.write({"category_ids": [(4, tag.id)]})

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

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

res = super().write(vals)

Expand All @@ -67,10 +69,11 @@ def write(self, vals):
#
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 (
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)
return res
2 changes: 1 addition & 1 deletion hr_job_category/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* Michael Telahun Makonnen <mmakonnen@gmail.com>
* Savoir-faire Linux
* Fekete Mihai <feketemihai@gmail.com>
* Nikul Chaudhary <nikulchaudhary2112@gmail.com>
* Nikul Chaudhary <nikulchaudhary2112@gmail.com>
2 changes: 1 addition & 1 deletion hr_job_category/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ to support tagging employees based on their job positions.
For example, all Supervisors could be attached to the Supervisors category.
Define which categories a job belongs to in the configuration for the job.
When an employee is assigned a particular job the categories attached to that
job will be attached to the employee record as well.
job will be attached to the employee record as well.
2 changes: 1 addition & 1 deletion hr_job_category/readme/INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ To install this module, you need to:
* add the path to this repository in your configuration (addons-path)
* update the module list
* search for "HR Job Employee Categories" in your addons
* install the module
* install the module
2 changes: 1 addition & 1 deletion hr_job_category/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Once a contract is defined for an employee, the tags assigned to the job
position selected are copied to the employee.

Note: If the job position is changed on the same contract, the tags from
old job position will be removed from employee.
old job position will be removed from employee.
35 changes: 16 additions & 19 deletions hr_job_category/tests/test_hr_job_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,32 @@
class TestHrJobCategories(common.TransactionCase):
def setUp(self):
super(TestHrJobCategories, self).setUp()
self.employee_model = self.env['hr.employee']
self.employee_categ_model = self.env['hr.employee.category']
self.user_model = self.env['res.users']
self.job_model = self.env['hr.job']
self.contract_model = self.env['hr.contract']
self.employee_model = self.env["hr.employee"]
self.employee_categ_model = self.env["hr.employee.category"]
self.user_model = self.env["res.users"]
self.job_model = self.env["hr.job"]
self.contract_model = self.env["hr.contract"]

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

# Create two employee categories
self.categ_id = self.employee_categ_model.create(
{'name': 'Category 1'})
self.categ_2_id = self.employee_categ_model.create(
{'name': 'Category 2'})
self.categ_id = self.employee_categ_model.create({"name": "Category 1"})
self.categ_2_id = self.employee_categ_model.create({"name": "Category 2"})

# Create two jobs
self.job_id = self.job_model.create(
{'name': 'Job 1',
'category_ids': [(6, 0, [self.categ_id.id])]})
{"name": "Job 1", "category_ids": [(6, 0, [self.categ_id.id])]}
)

self.job_2_id = self.job_model.create(
{'name': 'Job 2',
'category_ids': [(6, 0, [self.categ_2_id.id])]})
{"name": "Job 2", "category_ids": [(6, 0, [self.categ_2_id.id])]}
)

# 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.id, "wage": 50000}
)

def test_write_computes_with_normal_args(self):
"""
Expand All @@ -45,14 +42,14 @@ def test_write_computes_with_normal_args(self):
Check if the job categories are written to the employee.
"""
# Check if job categories are written to the employee
self.contract_id.write({'job_id': self.job_id.id})
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]

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

# Check if job2 categories are written to the employee
self.contract_id.write({'job_id': self.job_2_id.id})
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]

Expand Down
19 changes: 13 additions & 6 deletions hr_job_category/views/hr_view.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_job_form" model="ir.ui.view">
<field name="name">hr.job.category</field>
<field name="model">hr.job</field>
<field name="inherit_id" ref="hr.view_hr_job_form"/>
<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"/>
<field name="category_ids" widget="many2many_tags" placeholder="e.g. Part Time" groups="hr.group_hr_manager"/>
<label
for="category_ids"
class="oe_edit_only"
groups="hr.group_hr_manager"
/>
<field
name="category_ids"
widget="many2many_tags"
placeholder="e.g. Part Time"
groups="hr.group_hr_manager"
/>
</xpath>
</field>
</record>

</odoo>

0 comments on commit d858a37

Please sign in to comment.