Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] hr_holidays_leave_auto_approve: Auto-approve for everyone #663

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions hr_holidays_leave_auto_approve/README.rst
Expand Up @@ -37,15 +37,16 @@ belonging to that leave type.
Configuration
=============

Get sure that the administrative user belongs at least to the group
Leaves/Officer.

If you wish that the system automatically validates all the leave requests
If you wish that the system automatically validates the leave requests
belonging to a specific leave type, please follow the steps below.

#. Go on the leave type configuration menu
#. Select the leave type you wish to setup
#. Mark the flag 'Auto Validate'.
#. Select the auto approve policy for that leave type.

The option 'Auto Validated by HR' will auto validate leave requests created by
HR Officers while option 'Auto Validated by Everyone' will auto validate all
leave requests of the selected type, no matter who requested it.

Bug Tracker
===========
Expand All @@ -70,6 +71,7 @@ Contributors

* Andrea Stirpe <a.stirpe@onestein.nl>
* Antonio Esposito <a.esposito@onestein.nl>
* Jaime Arroyo <jaime.arroyo@creublanca.es>

Maintainers
~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion hr_holidays_leave_auto_approve/__manifest__.py
Expand Up @@ -3,7 +3,7 @@

{
'name': 'Auto Approve Leaves',
'version': '12.0.1.0.0',
'version': '12.0.2.0.0',
'license': 'AGPL-3',
'summary': 'Leave type for auto-validation of Leaves',
'author': 'Onestein, Odoo Community Association (OCA)',
Expand Down
@@ -0,0 +1,17 @@
# Copyright 2019 Eficent <http://www.eficent.com>
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
cr = env.cr
if openupgrade.column_exists(cr, 'hr_leave_type', 'auto_approve'):
openupgrade.logged_query(
cr, """
UPDATE hr_leave_type
SET auto_approve_policy = 'hr'
WHERE auto_approve
"""
)
20 changes: 16 additions & 4 deletions hr_holidays_leave_auto_approve/models/hr_leave.py
Expand Up @@ -12,6 +12,18 @@ def _check_approval_update(self, state):
return
return super()._check_approval_update(state)

@api.multi
def _should_auto_approve(self):
self.ensure_one()
policy = self.holiday_status_id.auto_approve_policy
return (self.can_approve and policy == 'hr') or policy == 'all'

@api.multi
def _apply_auto_approve_policy(self):
self.filtered(
lambda r: r._should_auto_approve()
).sudo().action_approve()

@api.model
def create(self, values):
auto_approve = self._get_auto_approve_on_creation(values)
Expand All @@ -21,15 +33,15 @@ def create(self, values):
HrLeave, self.with_context(
tracking_disable=tracking_disable)
).create(values)
if res.can_approve and res.holiday_status_id.auto_approve:
res.action_approve()
res._apply_auto_approve_policy()
return res

@api.model
def _get_auto_approve_on_creation(self, values):
auto_approve = False
if values.get('holiday_status_id'):
auto_approve = self.env['hr.leave.type'].browse(
leave_type = self.env['hr.leave.type'].browse(
values.get('holiday_status_id')
).auto_approve
)
auto_approve = leave_type.auto_approve_policy != 'no'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract as def _should_auto_approve method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think we can just delete the _get_auto_approve_on_creation() function? It is used to disable the creation message in case the leave can be auto approved, which I dont undersand why. IMHO it is not necessary to do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're self-approving, we don't want message about user approved own request being sent to himself, BUT there can be other watchers by default. Probably if "only user will receive message" then don't send it, or exclude self from followers, something like that. We can address that as separate PR

return auto_approve
11 changes: 7 additions & 4 deletions hr_holidays_leave_auto_approve/models/hr_leave_type.py
Expand Up @@ -7,7 +7,10 @@
class HrLeaveType(models.Model):
_inherit = "hr.leave.type"

auto_approve = fields.Boolean(
string='Auto Validate',
help="If True, leaves belonging to this leave type will be"
" automatically validated")
auto_approve_policy = fields.Selection(
selection=[
('no', 'No auto Validation'),
('hr', 'Auto Validated by HR'),
('all', 'Auto Validated by Everyone'),
], default='no', required=True,
)
11 changes: 6 additions & 5 deletions hr_holidays_leave_auto_approve/readme/CONFIGURE.rst
@@ -1,9 +1,10 @@
Get sure that the administrative user belongs at least to the group
Leaves/Officer.

If you wish that the system automatically validates all the leave requests
If you wish that the system automatically validates the leave requests
belonging to a specific leave type, please follow the steps below.

#. Go on the leave type configuration menu
#. Select the leave type you wish to setup
#. Mark the flag 'Auto Validate'.
#. Select the auto approve policy for that leave type.

The option 'Auto Validated by HR' will auto validate leave requests created by
HR Officers while option 'Auto Validated by Everyone' will auto validate all
leave requests of the selected type, no matter who requested it.
1 change: 1 addition & 0 deletions hr_holidays_leave_auto_approve/readme/CONTRIBUTORS.rst
@@ -1,2 +1,3 @@
* Andrea Stirpe <a.stirpe@onestein.nl>
* Antonio Esposito <a.esposito@onestein.nl>
* Jaime Arroyo <jaime.arroyo@creublanca.es>
12 changes: 7 additions & 5 deletions hr_holidays_leave_auto_approve/static/description/index.html
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.14: http://docutils.sourceforge.net/" />
<title>Auto Approve Leaves</title>
<style type="text/css">

Expand Down Expand Up @@ -386,15 +386,16 @@ <h1 class="title">Auto Approve Leaves</h1>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#id1">Configuration</a></h1>
<p>Get sure that the administrative user belongs at least to the group
Leaves/Officer.</p>
<p>If you wish that the system automatically validates all the leave requests
<p>If you wish that the system automatically validates the leave requests
belonging to a specific leave type, please follow the steps below.</p>
<ol class="arabic simple">
<li>Go on the leave type configuration menu</li>
<li>Select the leave type you wish to setup</li>
<li>Mark the flag ‘Auto Validate’.</li>
<li>Select the auto approve policy for that leave type.</li>
</ol>
<p>The option ‘Auto Validated by HR’ will auto validate leave requests created by
HR Officers while option ‘Auto Validated by Everyone’ will auto validate all
leave requests of the selected type, no matter who requested it.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>
Expand All @@ -417,6 +418,7 @@ <h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<ul class="simple">
<li>Andrea Stirpe &lt;<a class="reference external" href="mailto:a.stirpe&#64;onestein.nl">a.stirpe&#64;onestein.nl</a>&gt;</li>
<li>Antonio Esposito &lt;<a class="reference external" href="mailto:a.esposito&#64;onestein.nl">a.esposito&#64;onestein.nl</a>&gt;</li>
<li>Jaime Arroyo &lt;<a class="reference external" href="mailto:jaime.arroyo&#64;creublanca.es">jaime.arroyo&#64;creublanca.es</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Expand Up @@ -27,9 +27,9 @@ def setUp(self):

# Create 2 leave type
self.test_leave_type1_id = self.leave_type_model.create(
{'name': 'Test Leave Type1', 'auto_approve': True})
{'name': 'Test Leave Type1', 'auto_approve_policy': 'hr'})
self.test_leave_type2_id = self.leave_type_model.create(
{'name': 'Test Leave Type2', 'auto_approve': False})
{'name': 'Test Leave Type2', 'auto_approve_policy': 'no'})

# Create leave allocation requests for Test Leave Type1 and 2
self.leave_allocation1 = self.leave_allocation_model.create({
Expand Down Expand Up @@ -118,3 +118,35 @@ def test_leave_requests_state_employee_user(self):

# Check for leave2 state
self.assertEqual(leave2.state, 'confirm')

def test_leave_request_employee_validate_all(self):
self.test_user_id.groups_id = [
(6, 0, [self.env.ref('base.group_user').id])
]

today = datetime.today()
self.test_leave_type2_id.write({'auto_approve_policy': 'all'})

leave1 = self.leave_request_model.sudo(self.test_user_id).create({
'name': 'Test Leave Request 1',
'holiday_status_id': self.test_leave_type1_id.id,
'date_from': today + timedelta(days=10),
'date_to': today + timedelta(days=12),
'holiday_type': 'employee',
'employee_id': self.test_employee_id.id,
})

leave2 = self.leave_request_model.sudo(self.test_user_id).create({
'name': 'Test Leave Request 2',
'holiday_status_id': self.test_leave_type2_id.id,
'holiday_type': 'employee',
'date_from': today + timedelta(days=13),
'date_to': today + timedelta(days=14),
'employee_id': self.test_employee_id.id,
})

# Check for leave1 state
self.assertEqual(leave1.state, 'confirm')

# Check for leave2 state
self.assertEqual(leave2.state, 'validate')
Expand Up @@ -6,7 +6,7 @@
<field name="inherit_id" ref="hr_holidays.edit_holiday_status_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='validation']">
<field name="auto_approve"/>
<field name="auto_approve_policy"/>
</xpath>
</field>
</record>
Expand Down