Skip to content

Commit ecd7443

Browse files
[16.0][ADD] hr_timesheet_sheet_warning: fixed problems and improved testing
1 parent 04e5c4a commit ecd7443

9 files changed

+102
-29
lines changed

hr_timesheet_sheet_warning/README.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ HR Timesheet Sheet Warning
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:43890a1c4867dee1335ee92a4afeb0de109e14ee98a3d4b2b822ce1d4daca1e3
10+
!! source digest: sha256:5f86aee8d8ec3d3e4e473059d8e8094b0df0fd98ceda8ab78209c24ba5b92192
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -28,10 +28,12 @@ HR Timesheet Sheet Warning
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
3030

31-
This module allows the creation warning definitions, to be able to check
32-
certain aspects related to Timesheet Sheets.
33-
Once the warnings are defined, it can check their criteria against the
34-
actual Timesheet Sheets and raise individual warnings for each one if necessary.
31+
This module provides functionality for defining custom warning criteria related to
32+
Timesheet Sheets using Python expressions. Users can establish specific conditions
33+
for evaluating certain aspects of the Sheets.
34+
Once the warnings are defined, the module facilitates a systematic evaluation of
35+
Timesheet Sheets against these conditions. It generates individual warnings for each
36+
Sheet, if the warnings criteria are met.
3537

3638
**Table of contents**
3739

hr_timesheet_sheet_warning/models/hr_timesheet_sheet.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Sheet(models.Model):
1414
)
1515

1616
def _generate_hr_timesheet_sheet_warnings(self):
17-
definitions = self.env["hr_timesheet.sheet.warning.definition"].search([])
17+
definitions = self.env["hr_timesheet.sheet.warning.definition"].search(
18+
[("active", "in", [True, False])]
19+
)
1820
item_model = self.env["hr_timesheet.sheet.warning.item"]
1921

2022
for rec in self:
@@ -25,9 +27,12 @@ def _generate_hr_timesheet_sheet_warnings(self):
2527
("warning_definition_id", "=", definition.id),
2628
]
2729
)
28-
warning_applicable = definition._is_warning_applicable(rec)
30+
warning_applicable = definition.is_warning_applicable(rec)
2931
if warning_applicable:
3032
warning_raised = definition.evaluate_definition(rec)
33+
if not definition.active:
34+
warning_raised = False
35+
3136
if warning_raised and not existing:
3237
item_model.create(
3338
{"sheet_id": rec.id, "warning_definition_id": definition.id}

hr_timesheet_sheet_warning/models/hr_timesheet_sheet_warning_definition.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _eval_warning_domain(self, sheet, domain):
4141
)
4242
)
4343

44-
def _is_warning_applicable(self, sheet):
44+
def is_warning_applicable(self, sheet):
4545
domain = safe_eval(self.warning_domain) or []
4646
if domain:
4747
return self._eval_warning_domain(sheet, domain)

hr_timesheet_sheet_warning/models/hr_timesheet_sheet_warning_item.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class SheetWarningItem(models.Model):
88
_name = "hr_timesheet.sheet.warning.item"
99
_description = "Timesheet Sheet Warning Item"
10-
_order = "name desc"
10+
_order = "id desc"
1111

1212
warning_definition_id = fields.Many2one(
1313
comodel_name="hr_timesheet.sheet.warning.definition",
@@ -21,6 +21,14 @@ class SheetWarningItem(models.Model):
2121
store=True,
2222
)
2323

24+
_sql_constraints = [
25+
(
26+
"unique_warning",
27+
"unique(sheet_id, warning_definition_id)",
28+
"Timesheet warning has to be unique for timesheet sheet.",
29+
)
30+
]
31+
2432
def _compute_name(self):
2533
for rec in self:
2634
rec.name = "{} in {}".format(
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
This module allows the creation warning definitions, to be able to check
2-
certain aspects related to Timesheet Sheets.
3-
Once the warnings are defined, it can check their criteria against the
4-
actual Timesheet Sheets and raise individual warnings for each one if necessary.
1+
This module provides functionality for defining custom warning criteria related to
2+
Timesheet Sheets using Python expressions. Users can establish specific conditions
3+
for evaluating certain aspects of the Sheets.
4+
Once the warnings are defined, the module facilitates a systematic evaluation of
5+
Timesheet Sheets against these conditions. It generates individual warnings for each
6+
Sheet, if the warnings criteria are met.

hr_timesheet_sheet_warning/static/description/index.html

+7-5
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ <h1 class="title">HR Timesheet Sheet Warning</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370-
!! source digest: sha256:43890a1c4867dee1335ee92a4afeb0de109e14ee98a3d4b2b822ce1d4daca1e3
370+
!! source digest: sha256:5f86aee8d8ec3d3e4e473059d8e8094b0df0fd98ceda8ab78209c24ba5b92192
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372372
<p><a class="reference external image-reference" 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 image-reference" 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 image-reference" href="https://github.com/OCA/timesheet/tree/16.0/hr_timesheet_sheet_warning"><img alt="OCA/timesheet" src="https://img.shields.io/badge/github-OCA%2Ftimesheet-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/timesheet-16-0/timesheet-16-0-hr_timesheet_sheet_warning"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/timesheet&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373-
<p>This module allows the creation warning definitions, to be able to check
374-
certain aspects related to Timesheet Sheets.
375-
Once the warnings are defined, it can check their criteria against the
376-
actual Timesheet Sheets and raise individual warnings for each one if necessary.</p>
373+
<p>This module provides functionality for defining custom warning criteria related to
374+
Timesheet Sheets using Python expressions. Users can establish specific conditions
375+
for evaluating certain aspects of the Sheets.
376+
Once the warnings are defined, the module facilitates a systematic evaluation of
377+
Timesheet Sheets against these conditions. It generates individual warnings for each
378+
Sheet, if the warnings criteria are met.</p>
377379
<p><strong>Table of contents</strong></p>
378380
<div class="contents local topic" id="contents">
379381
<ul class="simple">

hr_timesheet_sheet_warning/tests/test_hr_timesheet_sheet_warning.py

+54
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33

44

5+
from unittest.mock import patch
6+
7+
from odoo.exceptions import UserError
58
from odoo.tests.common import Form, TransactionCase
69

710

@@ -121,6 +124,13 @@ def test_02_timesheet_warnings(self):
121124
self.assertEqual(warning_item.warning_definition_id.id, warning_definition.id)
122125
self.assertEqual(warning_item.sheet_id.id, sheet.id)
123126

127+
warning_definition.write({"active": False})
128+
sheet.action_generate_warnings()
129+
self.assertEqual(len(sheet.hr_timesheet_sheet_warning_item_ids), 0)
130+
warning_definition.write({"active": True})
131+
sheet.action_generate_warnings()
132+
self.assertEqual(len(sheet.hr_timesheet_sheet_warning_item_ids), 1)
133+
124134
def test_03_timesheet_warnings_with_domain(self):
125135
sheet_form_1 = Form(self.sheet_model.with_user(self.user))
126136
sheet_form_1.date_start = "2024-02-05"
@@ -151,3 +161,47 @@ def test_03_timesheet_warnings_with_domain(self):
151161
sheet_2.action_generate_warnings()
152162
self.assertEqual(len(sheet_1.hr_timesheet_sheet_warning_item_ids), 1)
153163
self.assertEqual(len(sheet_2.hr_timesheet_sheet_warning_item_ids), 0)
164+
165+
def test_04_timesheet_warnings_on_confirm(self):
166+
sheet_form = Form(self.sheet_model.with_user(self.user))
167+
sheet = sheet_form.save()
168+
with patch.object(type(sheet), "action_generate_warnings") as patched:
169+
sheet.action_timesheet_confirm()
170+
patched.assert_called_once_with()
171+
172+
def test_05_timesheet_warning_items(self):
173+
sheet_form = Form(self.sheet_model.with_user(self.user))
174+
sheet = sheet_form.save()
175+
warning_definition = self.sheet_warning_definition_model.create(
176+
{
177+
"name": "Test Warning Definition",
178+
"python_code": "sheet.total_time > 40",
179+
}
180+
)
181+
sheet.write({"total_time": 45})
182+
sheet.action_generate_warnings()
183+
warning_item = sheet.hr_timesheet_sheet_warning_item_ids
184+
185+
default_display_name = (
186+
warning_definition.display_name + " in " + sheet.complete_name
187+
)
188+
self.assertEqual(warning_item.name, default_display_name)
189+
warning_item.write({"name": "Test Name"})
190+
self.assertEqual(warning_item.name, "Test Name")
191+
warning_item._compute_name()
192+
self.assertEqual(warning_item.name, default_display_name)
193+
194+
def test_06_timesheet_warning_definitions(self):
195+
sheet_form = Form(self.sheet_model.with_user(self.user))
196+
sheet = sheet_form.save()
197+
warning_definition = self.sheet_warning_definition_model.create(
198+
{
199+
"name": "Test Warning Definition",
200+
"python_code": "123ABC",
201+
}
202+
)
203+
204+
with self.assertRaises(UserError):
205+
warning_definition.evaluate_definition(sheet)
206+
warning_definition.write({"python_code": "True"})
207+
warning_definition.evaluate_definition(sheet)

hr_timesheet_sheet_warning/views/hr_timesheet_sheet.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/>
2020
<field
2121
name="hr_timesheet_sheet_warning_item_ids"
22-
widget="many2many"
22+
widget="one2many"
2323
/>
2424
</page>
2525
</xpath>

hr_timesheet_sheet_warning/views/hr_timesheet_sheet_warning_definition.xml

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<field name="arch" type="xml">
1111
<tree>
1212
<field name="name" />
13-
<field name="active" />
13+
<field name="active" optional="hide" />
1414
</tree>
1515
</field>
1616
</record>
@@ -54,7 +54,7 @@
5454
<field name="name" />
5555
<separator />
5656
<filter
57-
string="Archived"
57+
string="Inactive"
5858
name="inactive"
5959
domain="[('active', '=', False)]"
6060
/>
@@ -72,20 +72,20 @@
7272
</record>
7373

7474
<menuitem
75-
id="timesheet_menu_settings"
76-
name="Settings"
75+
id="timesheet_menu_warning_definitions"
76+
name="Warnings"
7777
parent="hr_timesheet.hr_timesheet_menu_configuration"
78-
action="hr_timesheet.hr_timesheet_config_settings_action"
79-
groups="base.group_system"
78+
action="hr_timesheet_sheet_warning_definition_action"
79+
groups="hr_timesheet.group_hr_timesheet_approver"
8080
sequence="80"
8181
/>
8282

8383
<menuitem
84-
id="timesheet_menu_warning_definitions"
85-
name="Warnings"
84+
id="timesheet_menu_settings"
85+
name="Settings"
8686
parent="hr_timesheet.hr_timesheet_menu_configuration"
87-
action="hr_timesheet_sheet_warning_definition_action"
88-
groups="hr_timesheet.group_hr_timesheet_approver"
87+
action="hr_timesheet.hr_timesheet_config_settings_action"
88+
groups="base.group_system"
8989
sequence="85"
9090
/>
9191

0 commit comments

Comments
 (0)