def radio(name, value, checked=False, label=None, **attrs): """Create a radio button. Arguments: ``name`` -- the field's name. ``value`` -- the value returned to the application if the button is pressed. ``checked`` -- true if the button should be initially pressed. ``label`` -- a text label to display to the right of the button. The id of the radio button will be set to the name + '_' + value to ensure its uniqueness. An ``id`` keyword arg overrides this. (Note that this behavior is unique to the ``radio()`` helper.) To arrange multiple radio buttons in a group, see webhelpers2.containers.distribute(). """ _set_input_attrs(attrs, "radio", name, value) if checked: attrs["checked"] = "checked" if not "id" in attrs: attrs["id"] = '%s_%s' % (name, _make_safe_id_component(value)) widget = HTML.input(**attrs) if label: widget = HTML.label(widget, label) return widget
def password(name, value=None, id=NotGiven, **attrs): """Create a password field. Takes the same options as ``text()``. """ _set_input_attrs(attrs, "password", name, value) _set_id_attr(attrs, id, name) return HTML.input(**attrs)
def render_html(self): # have to override InputBase.render or it will put a value attribute # for a checkbox if self.displayval and self.displayval is not NotGiven: self.set_attr('checked', 'checked') else: try: self.del_attr('checked') except KeyError: pass return HTML.input(type=self.etype, **self.attributes)
def filtering_col_inputs2(self, col): filter = col.filter field_name = 'v2({0})'.format(col.key) field_name = self.grid.prefix_qs_arg_key(field_name) # field will get modified by JS inputs = '' if 'input2' in filter.input_types: ident = '{0}_input2'.format(col.key) inputs += _HTML.input(name=field_name, type='text', value=filter.value2_set_with, id=ident) return inputs
def render_html(self, **kwargs): if self.displayval or self.displayval == 0: self.set_attr('value', self.displayval) if self.chosen: self.set_attr(self.chosen_attr, self.chosen_attr) else: try: self.del_attr(self.chosen_attr) except KeyError: pass self.set_attrs(**kwargs) self.set_attr('name', self.lgroup.id) return HTML.input(type=self.etype, **self.attributes)
def file(name, value=None, id=NotGiven, **attrs): """Create a file upload field. If you are using file uploads then you will also need to set the multipart option for the form. Example:: >>> file('myfile') literal(u'<input id="myfile" name="myfile" type="file" />') """ _set_input_attrs(attrs, "file", name, value) _set_id_attr(attrs, id, name) return HTML.input(**attrs)
def filtering_col_inputs1(self, col): filter = col.filter field_name = 'v1({0})'.format(col.key) field_name = self.grid.prefix_qs_arg_key(field_name) inputs = '' if 'input' in filter.input_types: ident = '{0}_input1'.format(col.key) inputs += _HTML.input(name=field_name, type='text', value=filter.value1_set_with, id=ident) if 'select' in filter.input_types: ident = '{0}_select1'.format(col.key) current_selected = tolist(filter.value1) or [] multiple = None if len(current_selected) > 1: multiple = 'multiple' inputs += tags.select(field_name, current_selected, self.filtering_filter_options(filter), multiple=multiple) inputs += self.filtering_toggle_image() return inputs
def text(name, value=None, id=NotGiven, type="text", **attrs): """Create a standard text field. ``value`` is a string, the content of the text field. ``id`` is the HTML ID attribute, and should be passed as a keyword argument. By default the ID is the same as the name filtered through ``_make_safe_id_component()``. Pass None to suppress the ID attribute entirely. ``type`` is the input field type, normally "text". You can override it for HTML 5 input fields that don't have their own helper; e.g., "search", "email", "date". Options: * ``disabled`` - If set to True, the user will not be able to use this input. * ``size`` - The number of visible characters that will fit in the input. * ``maxlength`` - The maximum number of characters that the browser will allow the user to enter. The remaining keyword args will be standard HTML attributes for the tag. Example, a text input field:: >>> text("address") literal(u'<input id="address" name="address" type="text" />') HTML 5 example, a color picker: >>> text("color", type="color") literal(u'<input id="color" name="color" type="color" />') """ _set_input_attrs(attrs, type, name, value) _set_id_attr(attrs, id, name) convert_boolean_attrs(attrs, ["disabled"]) return HTML.input(**attrs)
def checkbox(name, value="1", checked=False, label=None, id=NotGiven, **attrs): """Create a check box. Arguments: ``name`` -- the widget's name. ``value`` -- the value to return to the application if the box is checked. ``checked`` -- true if the box should be initially checked. ``label`` -- a text label to display to the right of the box. ``id`` is the HTML ID attribute, and should be passed as a keyword argument. By default the ID is the same as the name filtered through ``_make_safe_id_component()``. Pass None to suppress the ID attribute entirely. The following HTML attributes may be set by keyword argument: * ``disabled`` - If true, checkbox will be grayed out. * ``readonly`` - If true, the user will not be able to modify the checkbox. To arrange multiple checkboxes in a group, see webhelpers2.containers.distribute(). Example:: >>> checkbox("hi") literal(u'<input id="hi" name="hi" type="checkbox" value="1" />') """ _set_input_attrs(attrs, "checkbox", name, value) _set_id_attr(attrs, id, name) if checked: attrs["checked"] = "checked" convert_boolean_attrs(attrs, ["disabled", "readonly"]) widget = HTML.input(**attrs) if label: widget = HTML.label(widget, label) return widget
def button_to(name, url='', **html_attrs): """Generate a form containing a sole button that submits to ``url``. Use this method instead of ``link_to`` for actions that do not have the safe HTTP GET semantics implied by using a hypertext link. The parameters are the same as for ``link_to``. Any ``html_attrs`` that you pass will be applied to the inner ``input`` element. In particular, pass disabled = True/False as part of ``html_attrs`` to control whether the button is disabled. The generated form element is given the class 'button-to', to which you can attach CSS styles for display purposes. The submit button itself will be displayed as an image if you provide both ``type`` and ``src`` as followed: type='image', src='icon_delete.gif' The ``src`` path should be the exact URL desired. A previous version of this helper added magical prefixes but this is no longer the case. Example 1:: # inside of controller for "feeds" >> button_to("Edit", url(action='edit', id=3)) <form method="post" action="/feeds/edit/3" class="button-to"> <div><input value="Edit" type="submit" /></div> </form> Example 2:: >> button_to("Destroy", url(action='destroy', id=3), .. method='DELETE') <form method="POST" action="/feeds/destroy/3" class="button-to"> <div> <input type="hidden" name="_method" value="DELETE" /> <input value="Destroy" type="submit" /> </div> </form> Example 3:: # Button as an image. >> button_to("Edit", url(action='edit', id=3), type='image', .. src='icon_delete.gif') <form method="POST" action="/feeds/edit/3" class="button-to"> <div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div> </form> .. note:: This method generates HTML code that represents a form. Forms are "block" content, which means that you should not try to insert them into your HTML where only inline content is expected. For example, you can legally insert a form inside of a ``div`` or ``td`` element or in between ``p`` elements, but not in the middle of a run of text, nor can you place a form within another form. (Bottom line: Always validate your HTML before going public.) Changed in WebHelpers 1.2: Preserve case of "method" arg for XHTML compatibility. E.g., "POST" or "PUT" causes *method="POST"*; "post" or "put" causes *method="post"*. """ if html_attrs: tags.convert_boolean_attrs(html_attrs, ['disabled']) method_tag = '' method = html_attrs.pop('method', '') if method.upper() in ['PUT', 'DELETE']: method_tag = HTML.input( type='hidden', id='_method', name_='_method', value=method) if method.upper() in ('GET', 'POST'): form_method = method elif method in ('put', 'delete'): # preserve lowercasing of verb form_method = 'post' else: form_method = 'POST' url, name = url, name or url submit_type = html_attrs.get('type') img_source = html_attrs.get('src') if submit_type == 'image' and img_source: html_attrs["value"] = name html_attrs.setdefault("alt", name) else: html_attrs["type"] = "submit" html_attrs["value"] = name return HTML.form(method=form_method, action=url, class_="button-to", c=[HTML.div(method_tag, HTML.input(**html_attrs))])
def render_html(self): if (self.displayval or self.displayval == 0) and self.displayval is not NotGiven: self.set_attr('value', self.displayval) return HTML.input(type=self.etype, **self.attributes)
def submit(name, value, id=NotGiven, **attrs): """Create a submit button with the text ``value`` as the caption.""" _set_input_attrs(attrs, "submit", name, value) _set_id_attr(attrs, id, name) return HTML.input(**attrs)
def hidden(name, value=None, id=NotGiven, **attrs): """Create a hidden field. """ _set_input_attrs(attrs, "hidden", name, value) _set_id_attr(attrs, id, name) return HTML.input(**attrs)
def paging_input(self): pp_qsk = self.grid.prefix_qs_arg_key('perpage') return _HTML.input(type='text', name=pp_qsk, value=self.grid.per_page)
def filtering_session_key(self): return _HTML.input( name='session_key', type='hidden', value=self.grid.session_key )