changeset 5ce1d6984748 in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset;node=5ce1d6984748
description:
Support PYSON comparison of date and datetime
issue4879
review270061002
diffstat:
CHANGELOG | 1 +
tryton/pyson.py | 24 +++++++++++++++++++-----
2 files changed, 20 insertions(+), 5 deletions(-)
diffs (61 lines):
diff -r 7f3c66ac7b81 -r 5ce1d6984748 CHANGELOG
--- a/CHANGELOG Sun Aug 16 16:31:23 2020 +0200
+++ b/CHANGELOG Sat Sep 05 22:59:38 2020 +0200
@@ -1,3 +1,4 @@
+* Support PYSON comparison of date and datetime
* Position copied records based on order
* Allow configuration of default colors for graph and calendar
* Use existing context for get_preferences
diff -r 7f3c66ac7b81 -r 5ce1d6984748 tryton/pyson.py
--- a/tryton/pyson.py Sun Aug 16 16:31:23 2020 +0200
+++ b/tryton/pyson.py Sat Sep 05 22:59:38 2020 +0200
@@ -302,11 +302,15 @@
super(Greater, self).__init__()
for i in (statement1, statement2):
if isinstance(i, PYSON):
- assert i.types().issubset({int, float, type(None)}), \
- 'statement must be an integer or a float'
+ assert i.types().issubset({
+ int, float, type(None),
+ datetime.datetime, datetime.date}), \
+ 'statement must be an integer, float, date or datetime'
else:
- assert isinstance(i, (int, float, type(None))), \
- 'statement must be an integer or a float'
+ assert isinstance(i, (
+ int, float, type(None),
+ datetime.datetime, datetime.date)), \
+ 'statement must be an integer, float, date or datetime'
if isinstance(equal, PYSON):
if equal.types() != {bool}:
equal = Bool(equal)
@@ -338,11 +342,19 @@
dct[i] = 0.0
if not isinstance(dct[i], (int, float)):
dct = dct.copy()
- dct[i] = float(dct[i])
+ stmt = dct[i]
+ if isinstance(stmt, datetime.datetime):
+ stmt = stmt.timestamp()
+ elif isinstance(stmt, datetime.date):
+ time = datetime.time(0, 0)
+ stmt = datetime.datetime.combine(stmt, time).timestamp()
+ dct[i] = float(stmt)
return dct
@staticmethod
def eval(dct, context):
+ if dct['s1'] is None or dct['s2'] is None:
+ return False
dct = Greater._convert(dct)
if dct['e']:
return dct['s1'] >= dct['s2']
@@ -359,6 +371,8 @@
@staticmethod
def eval(dct, context):
+ if dct['s1'] is None or dct['s2'] is None:
+ return False
dct = Less._convert(dct)
if dct['e']:
return dct['s1'] <= dct['s2']