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
This commit is contained in:
Cédric Krier committed 2024-04-14 10:56:27 +02:00
1 parent 6f06be1681
commit 3478483b5d
2 files changed
+16 -2

No files matched your search

+3 -1
View File
@@ -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
@@ -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):