def __setitem__(self, name, value): """Set the value of the named field. If there is 0 or multiple fields by that name, it is an error. Multiple checkboxes of the same name are special-cased; a list may be assigned to them to check the checkboxes whose value is present in the list (and uncheck all others). Setting the value of a ``<select>`` selects the given option (and confirms it is an option). Setting radio fields does the same. Checkboxes get boolean values. You cannot set hidden fields or buttons. Use ``.set()`` if there is any ambiguity and you must provide an index. """ fields = self.fields.get(name) assert fields is not None, ( "No field by the name %r found (fields: %s)" % (name, ', '.join(map(repr, self.fields.keys())))) all_checkboxes = all(isinstance(f, Checkbox) for f in fields) if all_checkboxes and isinstance(value, list): values = set(utils.stringify(v) for v in value) for f in fields: f.checked = f._value in values else: assert len(fields) == 1, ("Multiple fields match %r: %s" % (name, ', '.join(map(repr, fields)))) fields[0].value = value
def __setitem__(self, name, value): """Set the value of the named field. If there is 0 or multiple fields by that name, it is an error. Multiple checkboxes of the same name are special-cased; a list may be assigned to them to check the checkboxes whose value is present in the list (and uncheck all others). Setting the value of a ``<select>`` selects the given option (and confirms it is an option). Setting radio fields does the same. Checkboxes get boolean values. You cannot set hidden fields or buttons. Use ``.set()`` if there is any ambiguity and you must provide an index. """ fields = self.fields.get(name) assert fields is not None, ( "No field by the name %r found (fields: %s)" % (name, ', '.join(map(repr, self.fields.keys())))) all_checkboxes = all(isinstance(f, Checkbox) for f in fields) if all_checkboxes and isinstance(value, list): values = set(utils.stringify(v) for v in value) for f in fields: f.checked = f._value in values else: assert len(fields) == 1, ( "Multiple fields match %r: %s" % (name, ', '.join(map(repr, fields)))) fields[0].value = value
def _get_value_for_text(self, text): for i, (option_value, checked, option_text) in enumerate(self.options): if option_text == utils.stringify(text): return option_value raise ValueError( "Option with text %r not found (from %s)" % (text, ', '.join([repr(t) for o, c, t in self.options])))
def _get_value_for_text(self, text): for i, (option_value, checked, option_text) in enumerate(self.options): if option_text == utils.stringify(text): return option_value raise ValueError("Option with text %r not found (from %s)" % (text, ', '.join( [repr(t) for o, c, t in self.options])))
def value__set(self, value): if self._forced_value is not NoValue: self._forced_value = NoValue for i, (option, checked) in enumerate(self.options): if option == utils.stringify(value): self.selectedIndex = i break else: raise ValueError("Option %r not found (from %s)" % (value, ", ".join([repr(o) for o, c in self.options])))
def value__set(self, values): str_values = [utils.stringify(value) for value in values] self.selectedIndices = [] for i, (option, checked, text) in enumerate(self.options): if option in str_values: self.selectedIndices.append(i) str_values.remove(option) if str_values: raise ValueError("Option(s) %r not found (from %s)" % (', '.join(str_values), ', '.join( [repr(o) for o, c, t in self.options])))
def value__set(self, value): if self._forced_value is not NoValue: self._forced_value = NoValue for i, (option, checked, text) in enumerate(self.options): if option == utils.stringify(value): self.selectedIndex = i break else: raise ValueError( "Option %r not found (from %s)" % (value, ', '.join([repr(o) for o, c, t in self.options])))
def value__set(self, values): str_values = [utils.stringify(value) for value in values] self.selectedIndices = [] for i, (option, checked) in enumerate(self.options): if option in str_values: self.selectedIndices.append(i) str_values.remove(option) if str_values: raise ValueError( "Option(s) %r not found (from %s)" % (", ".join(str_values), ", ".join([repr(o) for o, c in self.options])) )
def _get_value_for_texts(self, texts): str_texts = [utils.stringify(text) for text in texts] value = [] for i, (option, checked, text) in enumerate(self.options): if text in str_texts: value.append(option) str_texts.remove(text) if str_texts: raise ValueError("Option(s) %r not found (from %s)" % (', '.join(str_texts), ', '.join( [repr(t) for o, c, t in self.options]))) return value
def _get_value_for_texts(self, texts): str_texts = [utils.stringify(text) for text in texts] value = [] for i, (option, checked, text) in enumerate(self.options): if text in str_texts: value.append(option) str_texts.remove(text) if str_texts: raise ValueError( "Option(s) %r not found (from %s)" % (', '.join(str_texts), ', '.join([repr(t) for o, c, t in self.options]))) return value
def test_stringify_other(self): self.assertEquals(utils.stringify(123), "123")
def test_stringify_binary(self): self.assertEquals(utils.stringify(b"foo"), "foo")
def test_stringify_text(self): self.assertEquals(utils.stringify("foo"), "foo")