TF: améliore check min/max

This commit is contained in:
Emmanuel Viennet 2022-02-14 16:05:43 +01:00
parent 37fde88a74
commit 84a25ae15b

View File

@ -254,13 +254,13 @@ class TF(object):
continue # allowed empty field, skip continue # allowed empty field, skip
# type # type
typ = descr.get("type", "string") typ = descr.get("type", "string")
if val != "" and val != None: if val != "" and val is not None:
# check only non-null values # check only non-null values
if typ[:3] == "int": if typ[:3] == "int":
try: try:
val = int(val) val = int(val)
self.values[field] = val self.values[field] = val
except: except ValueError:
msg.append( msg.append(
"La valeur du champ '%s' doit être un nombre entier" % field "La valeur du champ '%s' doit être un nombre entier" % field
) )
@ -270,30 +270,24 @@ class TF(object):
try: try:
val = float(val.replace(",", ".")) # allow , val = float(val.replace(",", ".")) # allow ,
self.values[field] = val self.values[field] = val
except: except ValueError:
msg.append( msg.append(
"La valeur du champ '%s' doit être un nombre" % field "La valeur du champ '%s' doit être un nombre" % field
) )
ok = 0 ok = 0
if typ[:3] == "int" or typ == "float" or typ == "real":
if ( if (
val != "" ok
and (typ[:3] == "int" or typ == "float" or typ == "real")
and val != ""
and val != None and val != None
and "min_value" in descr
and val < descr["min_value"]
): ):
if "min_value" in descr and self.values[field] < descr["min_value"]:
msg.append( msg.append(
"La valeur (%d) du champ '%s' est trop petite (min=%s)" "La valeur (%d) du champ '%s' est trop petite (min=%s)"
% (val, field, descr["min_value"]) % (val, field, descr["min_value"])
) )
ok = 0 ok = 0
if "max_value" in descr and self.values[field] > descr["max_value"]:
if (
val != ""
and val != None
and "max_value" in descr
and val > descr["max_value"]
):
msg.append( msg.append(
"La valeur (%s) du champ '%s' est trop grande (max=%s)" "La valeur (%s) du champ '%s' est trop grande (max=%s)"
% (val, field, descr["max_value"]) % (val, field, descr["max_value"])