def render(self): attributes = self.attributes.copy() if self.value: attributes['value'] = str(self.value) return ( self.html.format(flatten_attributes(attributes), self.label.text) )
def __render_input(self, id, attributes, label_text): element = self.html.format(attributes) output = '{0}{1}' attrs = self.label.attributes.copy() attrs['for'] = id flat_attrs = flatten_attributes(attrs) if self.wrapped: if self.label_position == 'left': return ( self.label.html.format( flat_attrs, output.format( label_text, element)) ) return ( self.label.html.format( flat_attrs, output.format(element, label_text)) ) else: label = self.label.html.format(flat_attrs, label_text) if self.label_position == 'left': return output.format(label, element) return output.format(element, label)
def render(self): multiple_elements = self.has_multiple_elements() elements = [] for index, label_value_pair in enumerate(self.values): attributes = self.attributes.copy() label_text, value = label_value_pair if multiple_elements: element_id = '{0}_{1}'.format(self.name, index) else: element_id = self.name attributes.update({ 'name': self.name, 'id': element_id }) if value: attributes['value'] = value if isinstance(self.value, (list, tuple)) and value in self.value: attributes['checked'] = 'checked' elif self.value and value == self.value: attributes['checked'] = 'checked' flat_attributes = flatten_attributes(attributes) element = self.__render_input( element_id, flat_attributes, label_text) elements.append(element) return ''.join(elements)
def render(self, **kwargs): multiple_elements = self.has_multiple_elements() elements = [] for index, label_value_pair in enumerate(self.values): attributes = self.attributes.copy() label_text, value = label_value_pair if multiple_elements: element_id = '{0}_{1}'.format(self.name, index) else: element_id = self.name attributes.update({ 'name': self.name, 'id': element_id }) attributes.update(kwargs) if value: attributes['value'] = value if isinstance(self.value, (list, tuple)) and value in self.value: attributes['checked'] = 'checked' elif self.value and value == self.value: attributes['checked'] = 'checked' flat_attributes = flatten_attributes(attributes) element = self.__render_input( element_id, flat_attributes, label_text) elements.append(element) return ''.join(elements)
def render(self, **kwargs): attributes = self.attributes.copy() attributes.update(kwargs) if self.value: attributes['value'] = str(self.value) label = kwargs.get('label', self.label.text) return self.html.format(flatten_attributes(attributes), label)
def render(self): attributes = self.attributes.copy() return ( self.html.format( flatten_attributes(attributes), self._options_render()) )
def render(self, field): attrs = self.attributes.copy() if not 'id' in field.attributes and field.name: # inject id based on field name id = field.name field.attributes['id'] = id attrs['for'] = id return self.html.format(flatten_attributes(attrs), self.text)
def render(self, **kwargs): attributes = self.attributes.copy() attributes.update(kwargs) return ( self.html.format( flatten_attributes(attributes), self._options_render()) )
def open(self, **kwargs): """Render the start tag of the form. Any addition kwargs will be used within the attributes. """ attrs = self.attributes.copy() if kwargs: attrs.update(kwargs) return '<form {0}>'.format(flatten_attributes(attrs))
def render(self): if self.button_mode: attributes = self.attributes.copy() return ( self.html.format( flatten_attributes(attributes), self.label.text) ) return super(Submit, self).render()
def render(self): """Render the element as html. Does not need to be called directly, as will be called by __str__ natively. """ attributes = self.attributes.copy() if self.value: attributes['value'] = str(self.value) return self.html.format(flatten_attributes(attributes))
def render(self, **kwargs): label = kwargs.pop('label') if 'label' in kwargs else self.label.text if self.button_mode: attributes = self.attributes.copy() attributes.update(kwargs) return ( self.html.format( flatten_attributes(attributes), label) ) return super(Submit, self).render()
def render(self, field=None, **kwargs): attrs = self.attributes.copy() if 'text' in kwargs: self.text = kwargs['text'] del kwargs['text'] if 'for_' in kwargs: attrs['for'] = kwargs['for_'] del kwargs['for_'] attrs.update(kwargs) if field and 'id' not in field.attributes and field.name: # inject id based on field name id = field.name field.attributes['id'] = id attrs['for'] = id return self.html.format(flatten_attributes(attrs), self.text)
def render(self, **kwargs): """Render the element as html. Does not need to be called directly, as will be called by __str__ natively. """ attributes = self.attributes.copy() if '_class' in kwargs: kwargs['class'] = kwargs.get('_class') del kwargs['_class'] attributes.update(kwargs) if self.value: attributes['value'] = str(self.value) return self.html.format(flatten_attributes(attributes))
def render(self, **kwargs): multiple_elements = self.has_multiple_elements() elements = [] id = self.attributes.get('id', self.name) values = self.values for index, label_value_pair in enumerate(values): attributes = self.attributes.copy() label_text, value = label_value_pair if multiple_elements: element_id = '{0}_{1}'.format(id, index) else: element_id = id attributes.update({ 'name': self.name, 'id': element_id }) attributes.update(kwargs) if value: attributes['value'] = value value = str(value) checked = False if isinstance(self.value, (list, tuple)) and value in (str(val) for val in self.value): checked = True elif self.value and value == str(self.value): checked = True elif isinstance(self.value, enum.Enum) and value == self.value.name: checked = True if checked: attributes['checked'] = 'checked' flat_attributes = flatten_attributes(attributes) element = self.__render_input( element_id, flat_attributes, label_text) elements.append(element) return ''.join(elements)
def render(self): """Overridden to prevent value from being put back into the field. """ attributes = self.attributes.copy() return self.html.format(flatten_attributes(attributes))
def render(self, **kwargs): attributes = self.attributes.copy() attributes.update(kwargs) value = self.value if self.value else '' return self.html.format(flatten_attributes(attributes), value)
def open(self): """Render the start tag of the form. """ return '<form {0}>'.format(flatten_attributes(self.attributes))
def render(self): attributes = self.attributes.copy() value = self.value if self.value else '' return self.html.format(flatten_attributes(attributes), value)
def test_flatten_forget_empty(self): attrs = {'class': 'menu', 'id': None} assert flatten_attributes(attrs) == 'class="menu"'
def test_flatten_keep_empty(self): attrs = {'class': 'menu', 'id': None} assert flatten_attributes(attrs, True) == 'class="menu" id=""'
def test_flatten(self): attrs = {'class': 'menu', 'id': 'MainMenu'} assert flatten_attributes(attrs) == 'class="menu" id="MainMenu"'