From b3d2df460fca1395a92f558b5dda3b088e99a113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 6 Jul 2020 11:49:46 +0000 Subject: [PATCH] [IMP] website_event_online: add time-related computation on events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RATIONALE Events are sometimes held online, gathering a community. In this merge we improve Event application to better support full-online events with improved tracks, wishlists, chat rooms, ... PURPOSE Reduce complexity of frontend templates and ease time-related display by adding computed fields on event. SPECIFICATIONS Add following information to be used in future templates * is_ongoing: between start and end of event; * is_done: after end of event; * start_today: will start today, for example to display remaining hours before start; * start_remaining: remaining time before start; Ensure timezone are correctly taken into account. Base heuristic is * datetime are stored in UTC in database; * TZ of event is stored on date_tz of event; * make most computation based on UTC: compare UTC of current visitor to UTC of event datetime; LINKS Community PR #53540 Enterprise PR odoo/enterprise#11384 Task ID-2252655 (Main Online Event task) Task ID-2283796 (Event B2Basics / Registration Flow) Co-Authored-By: Aurélien Warnon Co-Authored-By: David Beguin Co-Authored-By: Thibault Delavallée --- .../models/event_event.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/addons/website_event_online/models/event_event.py b/addons/website_event_online/models/event_event.py index 1419d63f786..05064c3b929 100644 --- a/addons/website_event_online/models/event_event.py +++ b/addons/website_event_online/models/event_event.py @@ -1,8 +1,12 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from datetime import datetime +from pytz import timezone, utc + from odoo import api, fields, models, _ from odoo.addons.http_routing.models.ir_http import slug +from odoo.addons.resource.models.resource import float_to_time from odoo.osv import expression @@ -14,12 +18,42 @@ class Event(models.Model): menu_register_cta = fields.Boolean( 'Add Register Button', compute='_compute_menu_register_cta', readonly=False, store=True) + # live information + is_ongoing = fields.Boolean( + 'Is Ongoing', compute='_compute_time_data', search='_search_is_ongoing', + help="Whether event has begun") + is_done = fields.Boolean( + 'Is Done', compute='_compute_time_data', + help="Whether event is finished") + start_today = fields.Boolean( + 'Start Today', compute='_compute_time_data', + help="Whether event is going to start today if still not ongoing") + start_remaining = fields.Integer( + 'Remaining before start', compute='_compute_time_data', + help="Remaining time before event starts (minutes)") @api.depends('website_menu') def _compute_menu_register_cta(self): for event in self: event.menu_register_cta = event.website_menu + @api.depends('date_begin', 'date_end') + def _compute_time_data(self): + """ Compute start and remaining time. Do everything in UTC as we compute only + time deltas here. """ + now_utc = utc.localize(fields.Datetime.now().replace(microsecond=0)) + for event in self: + date_begin_utc = utc.localize(event.date_begin, is_dst=False) + date_end_utc = utc.localize(event.date_end, is_dst=False) + event.is_ongoing = date_begin_utc <= now_utc <= date_end_utc + event.is_done = now_utc > date_end_utc + event.start_today = date_begin_utc.date() == now_utc.date() + if date_begin_utc >= now_utc: + td = date_begin_utc - now_utc + event.start_remaining = int(td.total_seconds() / 60) + else: + event.start_remaining = 0 + def _compute_is_participating(self): """ Override is_participating to improve heuristic that is now