Fix validation of numeric preference min/max values. Fixes #1567

pull/3/head
Akshay Joshi 2016-08-19 11:00:05 +01:00 committed by Dave Page
parent d5b0cd29d8
commit 86858b18f6
1 changed files with 12 additions and 4 deletions

View File

@ -149,10 +149,22 @@ class _Preference(object):
return False, gettext("Invalid value for a boolean option.") return False, gettext("Invalid value for a boolean option.")
elif self._type == 'integer': elif self._type == 'integer':
value = int(value) value = int(value)
if self.min_val is not None and value < self.min_val:
value = self.min_val
if self.max_val is not None and value > self.max_val:
value = self.max_val
if type(value) != int: if type(value) != int:
return False, gettext("Invalid value for an integer option.") return False, gettext("Invalid value for an integer option.")
elif self._type == 'numeric': elif self._type == 'numeric':
value = float(value) value = float(value)
if self.min_val is not None and value < self.min_val:
value = self.min_val
if self.max_val is not None and value > self.max_val:
value = self.max_val
t = type(value) t = type(value)
if t != float and t != int and t != decimal.Decimal: if t != float and t != int and t != decimal.Decimal:
return False, gettext("Invalid value for a numeric option.") return False, gettext("Invalid value for a numeric option.")
@ -526,10 +538,6 @@ class Preferences(object):
) )
try: try:
if pref.min_val is not None and int(value) < int(pref.min_val):
value = pref.min_val
if pref.max_val is not None and int(value) > int(pref.max_val):
value = pref.max_val
pref.set(value) pref.set(value)
except Exception as e: except Exception as e:
current_app.logger.exeception(e) current_app.logger.exeception(e)