tw2.bootstrap.forms

tw2.bootstrap.forms is a drop-in replacement for tw2.forms enabled to work with twitter bootstrap.

bootstrap_css
  • location
    • Location on the page where the resource should be placed.This can be one of: head, headbottom, bodytop or bodybottom. None means the resource will not be injected, which is still useful, e.g. static images.
  • modname
    • Name of Python module that contains the file.
  • filename
    • Path to file, relative to module base.
  • no_inject
    • Don't inject this link. (Default: False)
  • whole_dir
    • Make the whole directory available. (Default: False)
  • media
    • Media tag

bootstrap_responsive_css
  • location
    • Location on the page where the resource should be placed.This can be one of: head, headbottom, bodytop or bodybottom. None means the resource will not be injected, which is still useful, e.g. static images.
  • modname
    • Name of Python module that contains the file.
  • filename
    • Path to file, relative to module base.
  • no_inject
    • Don't inject this link. (Default: False)
  • whole_dir
    • Make the whole directory available. (Default: False)
  • media
    • Media tag

bootstrap_js
  • location
    • Location on the page where the resource should be placed.This can be one of: head, headbottom, bodytop or bodybottom. None means the resource will not be injected, which is still useful, e.g. static images.
  • modname
    • Name of Python module that contains the file.
  • filename
    • Path to file, relative to module base.
  • no_inject
    • Don't inject this link. (Default: False)
  • whole_dir
    • Make the whole directory available. (Default: False)

BootstrapMixin
Abstract base class for tw2.bootstrap.forms widgets.

TextField
  • size
    • Size of the field
  • placeholder
    • Placeholder text (HTML5 Only)
class TextField(InputField, twf.TextField):
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

TextArea
  • rows
    • Number of rows
  • cols
    • Number of columns
  • placeholder
    • Placeholder text (HTML5 Only)
class TextArea(BootstrapMixin, twf.TextArea):
    css_class = 'input-xlarge'
<%namespace name="tw" module="tw2.core.mako_util"/>\
<textarea ${tw.attrs(attrs=w.attrs)}>${w.value or ''}</textarea>

CheckBox
class CheckBox(_BoolControl, twf.CheckBox):
    css_class = "checkbox"
<%namespace name="tw" module="tw2.core.mako_util"/>\
<label class="${w.css_class}">
  <input ${tw.attrs(attrs=w.attrs)}/>
</label>

RadioButton
  • checked
    • Whether the field is selected
class RadioButton(_BoolControl, twf.RadioButton):
    css_class = "radio"
<%namespace name="tw" module="tw2.core.mako_util"/>\
<label class="${w.css_class}">
  <input ${tw.attrs(attrs=w.attrs)}/>
</label>

PasswordField
class PasswordField(InputField, twf.PasswordField):
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

FileField
class FileField(BootstrapMixin, twf.FileField):
    css_class = "input-file"
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

HiddenField
class HiddenField(twf.HiddenField):
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

IgnoredField
class IgnoredField(twf.HiddenField):
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

LabelField
class LabelField(BootstrapMixin, twf.LabelField):
    template = "tw2.bootstrap.forms.templates.label_field"
    css_class = "input-medium uneditable-input"
    def prepare(self):
        super(LabelField, self).prepare()
        self.safe_modify('attrs')
        del self.attrs['class']
<%namespace name="tw" module="tw2.core.mako_util"/>\
<span class="${w.css_class}">${unicode(w.value or '')}<input ${tw.attrs(attrs=w.attrs)}/></span>

LinkField

TODO -- not sure how to take this one on.

It doesn't seem to nicely fit the bootstrap paradigm. Do you have any ideas?
  • link
    • Link target
  • text
    • Link text
class LinkField(BootstrapMixin, twf.LinkField):
    """ TODO -- not sure how to take this one on.
    It doesn't seem to nicely fit the bootstrap paradigm.  Do you have
    any ideas?
    """
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<a ${tw.attrs(attrs=w.attrs)}>${w.text}</a>

Button
class Button(BootstrapMixin, twf.Button):
    css_class = 'btn'
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

SubmitButton
class SubmitButton(Button, twf.SubmitButton):
    css_class = 'btn btn-primary'
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

ResetButton
class ResetButton(Button, twf.ResetButton):
    pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

HorizontalLayout

Arrange widgets and labels horizontally: Float left, right-aligned labels on same line as controls

The following CSS classes are used, on the element containing both a child widget and its label.

odd / even
On alternating rows. The first row is odd.
required
If the field is a required field.
error
If the field contains a validation error.
  • hover_help
    • Whether to display help text as hover tips

HorizontalForm

Equivalent to a Form containing a HorizontalLayout.

  • id_suffix
    • Suffix to append to compound IDs
  • help_msg
    • This message displays as a div inside the form
  • action
    • URL to submit form data to. If this is None, the form submits to the same URL it was displayed on.
  • method
    • HTTP method used for form submission.
  • submit
    • Submit button widget. If this is None, no submit button is generated.
  • buttons
    • List of additional buttons to be placed at the bottom of the form
  • legend
    • Legend text for the form.

CalendarDatePicker
  • size
    • Size of the field
  • placeholder
    • Placeholder text (HTML5 Only)
  • style
    • Specify the template to use. [field, component]
  • format
    • the date format, combination of d, dd, m, mm, yy, yyyy.
  • weekStart
    • day of the week start. 0 for Sunday - 6 for Saturday
  • default
    • Default value (datetime) for the widget. If set to a function, it will be called each time before displaying.
class CalendarDatePicker(TextField):
    resources = TextField.resources + [datepicker_js, datepicker_css]
    template = "mako:tw2.bootstrap.forms.templates.datepicker"
    style = twc.Param(
        'Specify the template to use. [field, component]',
        default='field')
    format = twc.Param(
        "the date format, combination of d, dd, m, mm, yy, yyyy.",
        default="mm/dd/yyyy")
    date_format = twc.Variable()
    weekStart = twc.Param(
        "day of the week start.  0 for Sunday - 6 for Saturday",
        default=0)
    default = twc.Param(
        'Default value (datetime) for the widget.  If set to a function, ' +
        'it will be called each time before displaying.',
        default=datetime.now)
    def __init__(self, *args, **kw):
        super(CalendarDatePicker, self).__init__(*args, **kw)
        # Convert the bootstrap-datepicker format string to
        # a python format string...
        self.date_format = replace_all(self.format, [
            ('dd', 'DAY'), ('d', 'DAY'),
            ('mm', 'MONTH'), ('m', 'MONTH'),
            ('yyyy', '4YEAR'), ('yy', '2YEAR')
            ])
        self.date_format = replace_all(self.date_format, [
            ('DAY', '%d'),
            ('MONTH', '%m'),
            ('2YEAR', '%y'), ('4YEAR', '%Y')
            ])
        if not self.validator:
            self.validator = twc.DateValidator(
            format=self.date_format,
            )
    def prepare(self):
        super(CalendarDatePicker, self).prepare()
        self.add_call(twj.jQuery(self.selector).datepicker(dict(
            format=self.format,
            weekStart=self.weekStart,
        )))
        if not self.value:
            if callable(self.default):
                self.value = self.default()
            else:
                self.value = self.default
        try:
            self.value = self.value.strftime(self.date_format)
        except:
            pass
<%namespace name="tw" module="tw2.core.mako_util"/>\
% if w.style == 'component':
  <div class="input-append date" id="${w.compound_id}" data-date="${w.value or ''}" data-date-format="${w.format}">
    <input ${tw.attrs(attrs=dict((k, w.attrs[k]) for k in w.attrs if k != 'id' and k != 'value' ))} value="${w.value or ''}" />
    <span class="add-on"><i class="icon-th"></i></span>
  </div>
% else:
  <input ${tw.attrs(attrs=dict((k, w.attrs[k]) for k in w.attrs if k != 'value'))} value="${w.value or ''}" data-date="${w.value or ''}" data-date-format="${w.format}" />
% endif

CalendarTimePicker
  • size
    • Size of the field
  • placeholder
    • Placeholder text (HTML5 Only)
  • style
    • Specify the template to use. [modal, dropdown]
  • minuteStep
    • Specify a step for the minute field.
  • defaultTime
    • Set the initial time value. Setting it to "current" sets it to the current time.
  • disableFocus
    • Disables the input from focusing. This is useful for touch screen devices that display a keyboard on input focus.
class CalendarTimePicker(TextField):
    resources = TextField.resources + [timepicker_js, timepicker_css]
    style = twc.Param(
        'Specify the template to use. [modal, dropdown]',
        default='modal')
    minuteStep = twc.Param(
        'Specify a step for the minute field.',
        default=15)
    defaultTime = twc.Param(
        'Set the initial time value. '
        'Setting it to "current" sets it to the current time.',
        default='current')
    disableFocus = twc.Param(
        'Disables the input from focusing. This is useful for touch screen '
        'devices that display a keyboard on input focus.',
        default=False)
    def prepare(self):
        super(CalendarTimePicker, self).prepare()
        self.add_call(twj.jQuery(self.selector).timepicker(dict(
            template=self.style,
            minuteStep=self.minuteStep,
            defaultTime=self.defaultTime,
            disableFocus=self.disableFocus
        )))
<%namespace name="tw" module="tw2.core.mako_util"/>\
<input ${tw.attrs(attrs=w.attrs)}/>

CalendarDateTimePicker
  • children
    • Children for this widget. This must be an iterable, each item of which is a Widget
class CalendarDateTimePicker(BootstrapMixin, twc.CompoundWidget):
    resources = set(
        CalendarDatePicker.resources +
        CalendarTimePicker.resources
    )
    date = CalendarDatePicker()
    time = CalendarTimePicker()
    def _validate(self, value, state=None):
        """
        Inner validation method; this is called by validate and should not be
        called directly. Overriding this method in widgets is discouraged; a
        custom validator should be coded instead. However, in some
        circumstances overriding is necessary.
        """
        self._validated = True
        result = ''
        for field in self.children:
            child_value = field.validator.to_python(field.value)
            field.validator.validate_python(child_value, state)
            result += child_value
        return result
    def prepare(self):
        super(CalendarDateTimePicker, self).prepare()
<%
attr_keys = w.attrs.keys()
if 'id' in attr_keys:
    attr_keys.remove('id')
if 'class' in attr_keys:
    attr_keys.remove('class')
%>
<%namespace name="tw" module="tw2.core.mako_util"/>\
<div ${tw.attrs(attrs=w.attrs)}>
    % for c in w.children:
     ${c.display() | n}
    % endfor
</div>

CheckBoxList
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item

CheckBoxTable
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item
  • cols
    • Number of columns

DataGrid
  • fields
    • Fields of the Grid

FieldSet
  • children
    • Children specified for this widget will be passed to the child
  • id_suffix
    • Suffix to append to compound IDs
  • legend
    • Text for the legend

Form
  • children
    • Children specified for this widget will be passed to the child
  • id_suffix
    • Suffix to append to compound IDs
  • help_msg
    • This message displays as a div inside the form
  • action
    • URL to submit form data to. If this is None, the form submits to the same URL it was displayed on.
  • method
    • HTTP method used for form submission.
  • submit
    • Submit button widget. If this is None, no submit button is generated.
  • buttons
    • List of additional buttons to be placed at the bottom of the form

FormPage
  • children
    • Children specified for this widget will be passed to the child
  • id_suffix
    • Suffix to append to compound IDs
  • content_type
    • Content type header

GridLayout
  • child
    • Child for this widget. The child must have no id.
  • repetitions
    • Fixed number of repetitions. If this is None, it dynamically determined, based on the length of the value list.
  • min_reps
    • Minimum number of repetitions
  • max_reps
    • Maximum number of repetitions
  • extra_reps
    • Number of extra repeitions, beyond the length of the value list.

ImageButton
  • location
    • Location on the page where the resource should be placed.This can be one of: head, headbottom, bodytop or bodybottom. None means the resource will not be injected, which is still useful, e.g. static images.
  • modname
    • Name of Python module that contains the file.
  • filename
    • Path to file, relative to module base.
  • no_inject
    • Don't inject this link. (Default: False)
  • whole_dir
    • Make the whole directory available. (Default: False)
  • width
    • Width of image in pixels
  • height
    • Height of image in pixels
  • alt
    • Alternate text

ListFieldSet
  • id_suffix
    • Suffix to append to compound IDs
  • legend
    • Text for the legend

ListForm
  • id_suffix
    • Suffix to append to compound IDs
  • help_msg
    • This message displays as a div inside the form
  • action
    • URL to submit form data to. If this is None, the form submits to the same URL it was displayed on.
  • method
    • HTTP method used for form submission.
  • submit
    • Submit button widget. If this is None, no submit button is generated.
  • buttons
    • List of additional buttons to be placed at the bottom of the form

ListLayout
  • hover_help
    • Whether to display help text as hover tips

MultipleSelectField
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item
  • size
    • Number of visible options

MultipleSelectionField
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item

PostlabeledCheckBox
  • text_attrs
    • Dict of attributes to inject into the label.

PostlabeledPartialRadioButton
  • text_attrs
    • Dict of attributes to inject into the label.
  • checked
    • None

RadioButtonList
  • prompt_text
    • Text to prompt user to select an option.

RadioButtonTable
  • prompt_text
    • Text to prompt user to select an option.
  • cols
    • Number of columns

RowLayout
  • hover_help
    • Whether to display help text as hover tips

SelectionField
  • prompt_text
    • Text to prompt user to select an option.

SeparatedCheckBoxTable
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item

SeparatedRadioButtonTable
  • prompt_text
    • Text to prompt user to select an option.
  • item_validator
    • Validator that applies to each item

SingleSelectField
  • prompt_text
    • Text to prompt user to select an option.

Spacer

class Spacer(BootstrapMixin, twf.Spacer):
    template = "tw2.bootstrap.forms.templates.spacer"
<%namespace name="tw" module="tw2.core.mako_util"/>\
<hr ${tw.attrs(attrs=w.attrs)}></hr>

TableFieldSet
  • id_suffix
    • Suffix to append to compound IDs
  • legend
    • Text for the legend

TableForm
  • id_suffix
    • Suffix to append to compound IDs
  • help_msg
    • This message displays as a div inside the form
  • action
    • URL to submit form data to. If this is None, the form submits to the same URL it was displayed on.
  • method
    • HTTP method used for form submission.
  • submit
    • Submit button widget. If this is None, no submit button is generated.
  • buttons
    • List of additional buttons to be placed at the bottom of the form

TableLayout
  • hover_help
    • Whether to display help text as hover tips

VerticalCheckBoxTable
  • prompt_text
    • Text to prompt user to select an option.
  • cols
    • Number of columns. If the options are grouped, this is overidden.

VerticalRadioButtonTable
  • prompt_text
    • Text to prompt user to select an option.
  • cols
    • Number of columns. If the options are grouped, this is overidden.