def _do_save(req, panel, form_fields): for field in form_fields: val = req.args.get(field, '').strip() if val: if field == 'ui.auto_preview_timeout': fval = as_float(val, default=None) if fval is None or math.isinf(fval) or math.isnan(fval) \ or fval < 0: add_warning( req, _( "Discarded invalid value \"%(val)s\" " "for auto preview timeout.", val=val)) continue if field == 'tz' and 'tz' in req.session and \ val not in all_timezones: del req.session[field] elif field == 'newsid': req.session.change_sid(val) else: req.session[field] = val elif (field in req.args or field + '_cb' in req.args) and \ field in req.session: del req.session[field] add_notice(req, _("Your preferences have been saved.")) req.redirect(req.href.prefs(panel))
def test_as_float(self): self.assertEqual(1.1, util.as_float('1.1')) self.assertEqual(1.1, util.as_float('1.1', None)) self.assertEqual(1, util.as_float('1', None)) self.assertIsNone(util.as_float('A', None)) self.assertEqual(2.2, util.as_float('A', 2.2)) self.assertEqual(2.2, util.as_float('1.1', None, min=2.2)) self.assertEqual(0.1, util.as_float('1.1', None, max=0.1))
def as_float(self, key, default=None, min=None, max=None): """Return the value as a float. Return `default` if if an exception is raised while converting the value to a float. :param key: the name of the session attribute :keyword default: the value to return if the parameter does not exist or an exception occurs converting the value to a float. :keyword min: lower bound to which the value is limited :keyword max: upper bound to which the value is limited :since: 1.3.6 """ if key not in self: return default return as_float(self[key], default, min, max)