From fe54b6eb211937b1580f81f3e66de465a81bbdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Wed, 9 Aug 2023 07:07:36 +0000 Subject: [PATCH] [FIX] website_event_exhibitor: fix sponsor 'midnight' opening hours MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, configuring opening hours for event sponsors 'at midnight' is not handled properly. If you set either the start time or the end time opening hours to "00:00", the sponsor will be considered as always open. We fix that by correctly taking into account that configuration. (Note: if the user configures a sponsor to be open 'until midnight', we assume it means midnight 'the next day'). Tests have been altered to take this into account. Task-3459777 closes odoo/odoo#132605 X-original-commit: 721b996685c8f6943832acfce52b05c8773522f9 Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Warnon Aurélien (awa) --- .../models/event_sponsor.py | 7 +++-- .../tests/test_sponsor_internals.py | 31 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/addons/website_event_exhibitor/models/event_sponsor.py b/addons/website_event_exhibitor/models/event_sponsor.py index 293854d1646..9a08a723e09 100644 --- a/addons/website_event_exhibitor/models/event_sponsor.py +++ b/addons/website_event_exhibitor/models/event_sponsor.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from datetime import datetime +from datetime import datetime, timedelta from pytz import timezone, utc from odoo import api, fields, models, _ @@ -153,7 +153,7 @@ class Sponsor(models.Model): for sponsor in self: if not sponsor.event_id.is_ongoing: sponsor.is_in_opening_hours = False - elif not sponsor.hour_from or not sponsor.hour_to: + elif sponsor.hour_from is False or sponsor.hour_to is False: sponsor.is_in_opening_hours = True else: event_tz = timezone(sponsor.event_id.date_tz) @@ -166,6 +166,9 @@ class Sponsor(models.Model): # compute opening hours opening_from_tz = event_tz.localize(datetime.combine(now_tz.date(), float_to_time(sponsor.hour_from))) opening_to_tz = event_tz.localize(datetime.combine(now_tz.date(), float_to_time(sponsor.hour_to))) + if sponsor.hour_to == 0: + # when closing 'at midnight', we consider it's at midnight the next day + opening_to_tz = opening_to_tz + timedelta(days=1) opening_from = max([dt_begin, opening_from_tz]) opening_to = min([dt_end, opening_to_tz]) diff --git a/addons/website_event_exhibitor/tests/test_sponsor_internals.py b/addons/website_event_exhibitor/tests/test_sponsor_internals.py index 8021c3c4e7c..ef923de9bb0 100644 --- a/addons/website_event_exhibitor/tests/test_sponsor_internals.py +++ b/addons/website_event_exhibitor/tests/test_sponsor_internals.py @@ -13,7 +13,7 @@ class TestSponsorData(TestEventExhibitorCommon): @users('user_eventmanager') def test_event_date_computation(self): """ Test date computation. Pay attention that mocks returns UTC values, meaning - we have to take into account Europe/Brussels offset """ + we have to take into account Europe/Brussels offset (+2 in July) """ with freeze_time(self.reference_now): event = self.env['event.event'].browse(self.event_0.id) sponsor = self.env['event.sponsor'].browse(self.sponsor_0.id) @@ -62,3 +62,32 @@ class TestSponsorData(TestEventExhibitorCommon): sponsor.invalidate_model(['is_in_opening_hours']) self.assertFalse(sponsor.is_in_opening_hours) self.assertFalse(event.is_ongoing) + + # Use "00:00" as opening hours for sponsor -> should still work + event.invalidate_model(fnames=['is_ongoing']) + sponsor.hour_from = 0.0 # 0 -> 18 + + # Inside opening hours (17 < 18) + with freeze_time(datetime(2020, 7, 6, 15, 0, 1)): + sponsor.invalidate_model(fnames=['is_in_opening_hours']) + self.assertTrue(sponsor.is_in_opening_hours) + + # Outside opening hours (21 > 18) + with freeze_time(datetime(2020, 7, 6, 19, 0, 1)): + sponsor.invalidate_model(fnames=['is_in_opening_hours']) + self.assertFalse(sponsor.is_in_opening_hours) + + # Use "00:00" as closing hours for sponsor -> should still work + # (considered 'at midnight the next day') + sponsor.hour_from = 10.0 + sponsor.hour_to = 0.0 + + # Inside opening hours (11 > 10) + with freeze_time(datetime(2020, 7, 6, 9, 0, 1)): + sponsor.invalidate_model(fnames=['is_in_opening_hours']) + self.assertTrue(sponsor.is_in_opening_hours) + + # Outside opening hours (7 < 10) + with freeze_time(datetime(2020, 7, 6, 5, 0, 1)): + sponsor.invalidate_model(fnames=['is_in_opening_hours']) + self.assertFalse(sponsor.is_in_opening_hours)