[FIX] resource: prevent traceback with false start date

This traceback arises when  the user tries to remove the start date

Steps to produce

1. Install 'resource'
2. Open 'Settings/Technical/resource/Resource Time Off'
3. Create a new record and remove start date

AttributeError
'bool' object has no attribute 'tzinfo'

when the user tries to remove the start date, an error will be
produced because _compute_date_to seems to be computing the date_to
based on the date_from field. when removing the date_from  from calculations
on empty or none

which leads to traceback from here
https://github.com/odoo/odoo/blob/322e7ea19b7c069fdb92d3b86e5615c55489ca21/addons/resource/models/resource_calendar_leaves.py#L54-L59

This commit solves the above issue by computing `date_to` for records that have
`date_from`. Apart from that, this commit also removes `# -*- coding: utf-8 -*-`
from the first line of the modified file.

sentry-4983497879

closes odoo/odoo#163311

X-original-commit: 5032a8ffed10bfbd619c85a266092f129716ceca
Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
This commit is contained in:
vevi-odoo
2024-04-25 17:06:57 +00:00
committed by Mahamadasif Ansari
parent 27cea9d84c
commit 1d1dcefc06
2 changed files with 28 additions and 2 deletions
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime, time
@@ -55,6 +54,8 @@ class ResourceCalendarLeaves(models.Model):
def _compute_date_to(self):
user_tz = timezone(self.env.user.tz or self._context.get('tz') or self.company_id.resource_calendar_id.tz or 'UTC')
for leave in self:
if not leave.date_from:
continue
date_to_tz = user_tz.localize(leave.date_from) + relativedelta(hour=23, minute=59, second=59)
leave.date_to = date_to_tz.astimezone(utc).replace(tzinfo=None)
+26 -1
View File
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from dateutil.relativedelta import relativedelta
from odoo.fields import Datetime
from odoo.tests.common import TransactionCase
from odoo.osv.expression import normalize_domain
from odoo.addons.resource.models import utils
from odoo.tests import Form
class TestExpression(TransactionCase):
@@ -57,3 +59,26 @@ class TestExpression(TransactionCase):
field_name_mapping={'field3': 'field4'},
))
)
def test_resource_creation_with_date_from(self):
"""
Test resource creation with a date_from.
AssertError is raised when date_from is not provided.
"""
with self.assertRaises(AssertionError):
with Form(self.env['resource.calendar.leaves']) as res:
res.date_from = False
res.date_to = Datetime.now()
with Form(self.env['resource.calendar.leaves']) as res:
date_from = Datetime.now()
date_to = Datetime.now() + relativedelta(hours=24)
res.date_from = date_from
res.date_to = date_to
self.assertFalse(res.id, 'The resource does not have an id before saving')
res.save()
self.assertTrue(res.id, 'The resource was successfully created')
self.assertEqual(res.date_from, Datetime.to_string(date_from))
self.assertEqual(res.date_to, Datetime.to_string(date_to))