def checkbox(label, name, checked=False, **kwargs): kwargs['type'] = 'checkbox' kwargs['name'] = name if checked: kwargs['checked'] = 'checked' kwargs.setdefault('id', name) return HTML.div(class_='formField checkbox', id='%s-field' % kwargs['id'], c=[HTML.label(for_=name, c=[ HTML.input(**kwargs), HTML.span(class_='labelText', c=[label]) ]), HTML.literal('<form:error name="%s" />' % name)])
def input_area(name, title, value='', cols='50', rows='5', help_text=None, disabled=False, **kwargs): expl = None if help_text is not None: expl = HTML.span(class_='helpText', c=help_text) if disabled: kwargs['disabled'] = 'disabled' kwargs.setdefault('id', name) return HTML.div(class_='formField', id='%s-field' % kwargs['id'], c=[HTML.label(for_=name, c=[ HTML.span(class_='labelText', c=[title]), HTML.span(class_='textField', c=[ HTML.textarea(name_=name, cols=cols, rows=rows, c=[value], **kwargs), ])]), HTML.literal('<form:error name="%s" />' % name), expl])
def select_radio(name, title, options, selected=[], help_text=None, **kwargs): expl = None if help_text is not None: expl = HTML.span(class_='helpText', c=help_text) radios = [] for value, label in options: checked = value in selected radios.append(radio(name, value, checked, label, **kwargs)) return HTML.div(class_='formField', id='%s-field' % name, c=[HTML.label(for_=name, c=[ HTML.span(class_='labelText', c=[title]), HTML.span(class_='radioField', c=radios)]), HTML.literal('<form:error name="%s" />' % name), expl])
def select_line(name, title, options, selected=[], help_text=None, right_next=None, **kwargs): expl = None if help_text is not None: expl = HTML.span(class_='helpText', c=help_text) next = None if right_next is not None: next = HTML.span(class_='rightNext', c=right_next) kwargs.setdefault('id', name) field = select(name, selected, options, **kwargs) return HTML.div(class_='formField', id='%s-field' % kwargs['id'], c=[HTML.label(for_=name, c=[ HTML.span(class_='labelText', c=[title]), HTML.span(class_='textField', c=[ field, ])]), next, HTML.literal('<form:error name="%s" />' % name), expl])
def input_line(name, title, value='', help_text=None, right_next=None, **kwargs): expl = None if help_text is not None: expl = HTML.span(class_='helpText', c=help_text) next = None if right_next is not None: next = HTML.span(class_='rightNext', c=right_next) kwargs.setdefault('id', name) kwargs.setdefault('type', 'text') return HTML.div(class_='formField', id='%s-field' % kwargs['id'], c=[HTML.label(for_=name, c=[ HTML.span(class_='labelText', c=[title]), HTML.span(class_='textField', c=[ HTML.input(value=value, name_=name, **kwargs), ])]), next, HTML.literal('<form:error name="%s" />' % name), expl])
def button_to(name, url='', **html_options): """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_options`` that you pass will be applied to the inner ``input`` element. In particular, pass disabled = True/False as part of ``html_options`` 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.) """ if html_options: convert_boolean_attrs(html_options, ['disabled']) method_tag = '' method = html_options.pop('method', '') if method.upper() in ['PUT', 'DELETE']: method_tag = HTML.input( type='hidden', id='_method', name_='_method', value=method) form_method = (method.upper() == 'GET' and method) or 'POST' url, name = url, name or url submit_type = html_options.get('type') img_source = html_options.get('src') if submit_type == 'image' and img_source: html_options["value"] = name html_options.setdefault("alt", name) else: html_options["type"] = "submit" html_options["value"] = name return HTML.form(method=form_method, action=url, class_="button-to", c=[HTML.div(method_tag, HTML.input(**html_options))])
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 form(url, method="post", multipart=False, hidden_fields=None, **attrs): """An open tag for a form that will submit to ``url``. You must close the form yourself by calling ``end_form()`` or outputting </form>. Options: ``method`` The method to use when submitting the form, usually either "GET" or "POST". If "PUT", "DELETE", or another verb is used, a hidden input with name _method is added to simulate the verb over POST. ``multipart`` If set to True, the enctype is set to "multipart/form-data". You must set it to true when uploading files, or the browser will submit the filename rather than the file. ``hidden_fields`` Additional hidden fields to add to the beginning of the form. It may be a dict or an iterable of key-value tuples. This is implemented by calling the object's ``.items()`` method if it has one, or just iterating the object. (This will successfuly get multiple values for the same key in WebOb MultiDict objects.) Because input tags must be placed in a block tag rather than directly inside the form, all hidden fields will be put in a '<div style="display:none">'. The style prevents the <div> from being displayed or affecting the layout. Examples: >>> form("/submit") literal(%(u)s'<form action="/submit" method="post">') >>> form("/submit", method="get") literal(%(u)s'<form action="/submit" method="get">') >>> form("/submit", method="put") literal(%(u)s'<form action="/submit" method="post"><div style="display:none">\\n<input name="_method" type="hidden" value="put" />\\n</div>\\n') >>> form("/submit", "post", multipart=True) literal(%(u)s'<form action="/submit" enctype="multipart/form-data" method="post">') Changed in WebHelpers 1.0b2: add <div> and ``hidden_fields`` arg. Changed in WebHelpers 1.2: don't add an "id" attribute to hidden tags generated by this helper; they clash if there are multiple forms on the page. """ fields = [] attrs["action"] = url if multipart: attrs["enctype"] = "multipart/form-data" if method.lower() in ['post', 'get']: attrs['method'] = method else: attrs['method'] = "post" field = hidden("_method", method, id=None) fields.append(field) if hidden_fields is not None: try: it = hidden_fields.items() except AttributeError: it = hidden_fields for name, value in it: field = hidden(name, value, id=None) fields.append(field) if fields: div = HTML.div(style="display:none", _nl=True, *fields) else: div = None return HTML.form(div, _closed=False, **attrs)
def icon(icon_type, description=None, class_=''): if not description and icon_type in icon_type_descriptions: description = icon_type_descriptions[icon_type] return HTML.div(HTML.span(description), class_=class_+" icon16 i_"+icon_type, title=description)
def form(url, method="post", multipart=False, hidden_fields=None, **attrs): """An open tag for a form that will submit to ``url``. You must close the form yourself by calling ``end_form()`` or outputting </form>. Options: ``method`` The method to use when submitting the form, usually either "GET" or "POST". If "PUT", "DELETE", or another verb is used, a hidden input with name _method is added to simulate the verb over POST. ``multipart`` If set to True, the enctype is set to "multipart/form-data". You must set it to true when uploading files, or the browser will submit the filename rather than the file. ``hidden_fields`` Additional hidden fields to add to the beginning of the form. It may be a dict or an iterable of key-value tuples. This is implemented by calling the object's ``.items()`` method if it has one, or just iterating the object. (This will successfuly get multiple values for the same key in WebOb MultiDict objects.) Because input tags must be placed in a block tag rather than directly inside the form, all hidden fields will be put in a '<div style="display:none">'. The style prevents the <div> from being displayed or affecting the layout. Examples: >>> form("/submit") literal(u'<form action="/submit" method="post">') >>> form("/submit", method="get") literal(u'<form action="/submit" method="get">') >>> form("/submit", method="put") literal(u'<form action="/submit" method="post"><div style="display:none">\\n<input id="_method" name="_method" type="hidden" value="put" />\\n</div>\\n') >>> form("/submit", "post", multipart=True) literal(u'<form action="/submit" enctype="multipart/form-data" method="post">') Changed in WebHelpers 2.0b2: add <div> and ``hidden_fields`` arg. """ fields = [] attrs["action"] = url if multipart: attrs["enctype"] = "multipart/form-data" if method.lower() in ['post', 'get']: attrs['method'] = method else: attrs['method'] = "post" field = hidden("_method", method) fields.append(field) if hidden_fields is not None: try: it = hidden_fields.items() except AttributeError: it = hidden_fields for name, value in it: field = hidden(name, value) fields.append(field) if fields: div = HTML.div(style="display:none", _nl=True, *fields) else: div = None return HTML.form(div, _closed=False, **attrs)
def field( label='', field='', required=False, label_desc='', field_desc='', help='', error='', field_pre='', ): """\ Format a field with a label. ``label`` The label for the field ``field`` The HTML representing the field, wrapped in ``literal()`` ``required`` Can be ``True`` or ``False`` depending on whether the label should be formatted as required or not. By default required fields have an asterix. ``label_desc`` Any text to appear underneath the label, level with ``field_desc`` ``field_desc`` Any text to appear underneath the field ``help`` Any HTML or JavaScript to appear imediately to the right of the field which could be used to implement a help system on the form ``error`` Any text to appear immediately before the HTML field, usually used for an error message. It should be noted that when used with FormEncode's ``htmlfill`` module, errors appear immediately before the HTML field in the position of the ``error`` argument. No ``<form:error>`` tags are added automatically by this helper because errors are placed there anyway and adding the tags would lead to this helper generating invalid HTML. ``field_pre`` Any HTML to appear immediately above the field. TIP: For future compatibility, always specify arguments explicitly and do not rely on their order in the function definition. Here are some examples: >>> print field('email >', literal('<input type="text" name="test" value="" />'), required=True) <tr class="field"> <td class="label" valign="top"><span class="required">*</span><label>email >:</label></td> <td class="field" colspan="2" valign="top"><input type="text" name="test" value="" /></td> </tr> >>> print field( ... label='email >', ... field=literal('<input type="text" name="test" value="" />'), ... label_desc='including the @ sign', ... field_desc='Please type your email carefully', ... error='This is an error message <br />', ... help = 'No help available for this field', ... required=True, ... ) ... <tr class="field"> <td class="label" valign="top"><span class="required">*</span><label>email >:</label></td> <td class="field" valign="top"><div class="error">This is an error message <br /></div><input type="text" name="test" value="" /></td> <td class="help" valign="top">No help available for this field</td> </tr> <tr class="description"> <td class="label_desc" valign="top"><span class="small">including the @ sign</span></td> <td class="field_desc" colspan="2" valign="top"><span class="small">Please type your email carefully</span></td> </tr> An appropriate stylesheet to use to style forms generated with field() when the table class is specified as "formbuild" would be:: table.formbuild span.error-message, table.formbuild div.error, table.formbuild span.required { font-weight: bold; color: #f00; } table.formbuild span.small { font-size: 85%; } table.formbuild form { margin-top: 20px; } table.formbuild form table td { padding-bottom: 3px; } """ if error: field = HTML.div(class_='error', c=error)+field if label: label = label + literal(':') if field_pre: field = HTML.div(c=field_pre) + field rows = [] if required: required_html = HTML.span(class_="required", c='*') else: required_html = HTML.span(style="visibility: hidden", c='*') label_html = HTML.td(valign="top", class_="label", c=required_html+HTML.label(c=label)) if help: field_html = HTML.td(valign="top", class_="field", c=field) help_html = HTML.td(valign="top", class_="help", c=help)+'\n' else: field_html = HTML.td(valign="top", class_="field", colspan="2", c=field) help_html = '' rows.append(HTML.tr(class_='field', c='\n'+label_html+'\n'+field_html+'\n'+help_html)) if label_desc or field_desc: label_desc_html = HTML.td(valign="top", class_="label_desc", c=HTML.span(class_="small", c=label_desc)) field_desc_html = HTML.td(valign="top", class_="field_desc", colspan="2", c=HTML.span(class_="small", c=field_desc)) rows.append(HTML.tr(class_="description", c='\n'+label_desc_html+'\n'+field_desc_html+'\n')) return literal('\n').join(rows)
def input_wysiwyg(name, title, value='', cols='60', rows='15'): return HTML.div(class_='form-field', c=[ HTML.label(for_=name, c=[title]), HTML.textarea(class_='ckeditor', name_=name, id_=name, cols=cols, rows=rows, c=[value]), HTML.literal('<form:error name="%s" />' % name) ])
def input_hidden(name, value='', **kwargs): kwargs.setdefault('id', name) return HTML.div(class_='formField', id='%s-field' % kwargs['id'], c=[HTML.input(type='hidden', value=value, name_=name, **kwargs), HTML.literal('<form:error name="%s" />' % name)])