From 3478483b5db1ecf4e21d0e93d6a6418540ca2cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 14 Apr 2024 10:56:27 +0200 Subject: [PATCH] Parse text as datetime when it is a string The on_paste method is calling value_from_text for each widget using the string from the clipboard. So textual value needs to be supported even for date and time widgets which uses a cell renderer that provides datetime value. Closes #12860 (grafted from 06448d7bdb976b7057a465ca332bbc638e91653e) --HG-- branch : 6.0 --- tryton/tryton/common/datetime_.py | 4 +++- .../gui/window/view_form/view/list_gtk/widget.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tryton/tryton/common/datetime_.py b/tryton/tryton/common/datetime_.py index 41c49a116c..2dae89b154 100644 --- a/tryton/tryton/common/datetime_.py +++ b/tryton/tryton/common/datetime_.py @@ -10,7 +10,9 @@ from dateutil.parser import parse from .common import IconFactory -__all__ = ['Date', 'CellRendererDate', 'Time', 'CellRendererTime', 'DateTime'] +__all__ = [ + 'Date', 'CellRendererDate', 'Time', 'CellRendererTime', 'DateTime', + 'date_parse'] _ = gettext.gettext diff --git a/tryton/tryton/gui/window/view_form/view/list_gtk/widget.py b/tryton/tryton/gui/window/view_form/view/list_gtk/widget.py index 076f1642aa..382c94329c 100644 --- a/tryton/tryton/gui/window/view_form/view/list_gtk/widget.py +++ b/tryton/tryton/gui/window/view_form/view/list_gtk/widget.py @@ -28,7 +28,8 @@ from tryton.common import data2pixbuf from tryton.common.completion import get_completion, update_completion from tryton.common.selection import ( SelectionMixin, PopdownMixin, selection_shortcuts) -from tryton.common.datetime_ import CellRendererDate, CellRendererTime +from tryton.common.datetime_ import ( + CellRendererDate, CellRendererTime, date_parse) from tryton.common.domain_parser import quote from tryton.config import CONFIG @@ -474,6 +475,17 @@ class Date(GenericText): else: return '' + def value_from_text(self, record, text, callback=None): + if isinstance(text, str): + field = record[self.attrs['name']] + try: + # Use a datetime instance and rely on field to convert to the + # proper type + text = date_parse(text, self.get_format(record, field)) + except (ValueError, OverflowError): + text = None + return super().value_from_text(record, text, callback=callback) + class Time(Date):