Source code for uw.web.html.bootstrapform


from ll.xist.ns import html
from ll.xist import xsc
from datetime import datetime, date

[docs]class button (html.button): """Monkeypatch html.button to allow use of the data-clipboard-text and data-toggles attributes. This class replaces html.button. It differs only in allowing these two HTML5 attributes to be specified. """
[docs] class Attrs (html.button.Attrs):
[docs] class data_clipboard_text (xsc.TextAttr): xmlname = "data-clipboard-text"
[docs] class data_toggle (xsc.TextAttr): xmlname = "data-toggle"
html.button = button
[docs]class input (html.input): """Monkeypatch html.button to allow use of specific data attributes for the calendar. This class replaces html.button. It differs only in allowing these two HTML5 attributes to be specified. The attributes were taken from the datepicker documentation found at this url: http://bootstrap-datepicker.readthedocs.io/en/latest/options.html """
[docs] class Attrs (html.input.Attrs):
[docs] class data_date_start_date (xsc.TextAttr): xmlname = "data-date-start-date"
[docs] class data_date_end_date (xsc.TextAttr): xmlname = "data-date-end-date"
[docs] class data_date_dates_disabled (xsc.TextAttr): xmlname = "data-date-dates-disabled"
[docs] class data_date_days_of_week_disabled (xsc.TextAttr): xmlname = "data-date-days-of-week-disabled"
[docs] class data_date_default_view_date (xsc.TextAttr): xmlname = "data-date-default-view-date"
html.input = input
[docs]def render_copy_button(name, copytext, value=None, class_=None): """Returns the html for a button that has the ability to enter copytext into the clipboard when clicked. :param name: Text to be displayed on button :param copytext: Text to be copied on clicked :param value: Value attribute for button (needed for ZeroClipboard) :param class_: CSS class(es) to apply to button """ btn = html.button (name, class_=class_, value=value) btn["data_clipboard_text"] = copytext btn["data_toggle"] = "tooltip" return btn
[docs]def render_icon(glyphicon_icon): """Parse form results for a combined date and time. :param glyphicon_icon: a string with the final component of the name, e.g. 'pencil' for glyphicon-pencil. See full list: http://glyphicons.com/ Uses the specified parse_date and parse_time to parse the date and time, respectively, from the form results, using the specified form result name with "d" and "t" appended. The result is a datetime.datetime. """ return html.span (class_="glyphicon glyphicon-" + glyphicon_icon)
[docs]def render_bootstrap_upload_submit(submit_name="", upload_name="", accept=""): """Render a file upload with a submit button. :param submit_name: form name for the submit button :param upload_name: form name for the submitted file :param accept: the accept property of the file input element e.g. 'image/\*' limiting what types of file the user can select """ return html.span (render_bootstrap_upload (accept=accept, name=upload_name), ' ', render_bootstrap_submit ("Upload!", name=submit_name), class_ = "btn-file")
[docs]def render_bootstrap_upload(accept="", name=""): """Make a select element with a given name and given option values. :param name: the form control name to use. :param accept: the accept property of the file input element e.g. 'image/\*' limiting what types of file the user can select """ return [html.label ((u"Browse…", html.input (type="file", style="display: none;", name=name, accept=accept)), class_="btn btn-input"), html.span (" No file chosen ")]
[docs]def render_bootstrap_submit(value, class_ = "", name = ""): """Returns a submit button styled with bootstrap. :param value: the name and value of the submit button :param name: the form control name to use. :param class_: CSS class to apply to the button """ classes = "btn btn-input" + ((" " + class_) if class_ else "") return html.button (value, type="submit", class_=classes, value=value, name=name if name else None)
[docs]def render_bootstrap_date_selector (name, start=None, current=None, months=4, class_=None, disable_days=[], days_of_week_disabled="06"): """Return form controls for choosing a date with a single input and a visual calendar upon clicking the input. :param name: the form control name to use. :param start: the earliest selectable date value :param current: the initial value of the form control. :param months: the number of months to include. :param class_: CSS class to apply to the input :param disable_dates: a list of dates to disable from selecting (date objects) :param days_of_week_disabled: a string of week days to disable from selecting (e.g. '06' means weekends are disabled) Returns a HTML input menu controls, for entry of date in the isoformat (yyyy-mm-dd). User can either manually write date or select date from calendar. """ classes = "datepicker form-control" + ("" if class_ is None else (" " + class_)) if current is None: current = date.today () if start is None: start = date.today () date_select = html.input (value=current.isoformat (), type="text", name=name, class_=classes) date_select["data_date_start_date"] = start.isoformat () date_select["data_date_default_view_date"] = current.isoformat () date_select["data_date_end_date"] = '+{}m'.format (months) if days_of_week_disabled: date_select["data_date_days_of_week_disabled"] = "06" if disable_days: # convert the list of disabled days to a JSON array string disable_days = [d.isoformat () for d in disable_days] date_select["data_date_dates_disabled"] = '["' + '","'.join(disable_days) + '"]' return date_select
[docs]def parse_bootstrap_date (form, name): """Parse form results from a one-menu date selector. :param uw.web.wsgi.form.CGIFormResults form: form results. :param name: the form result name to use. """ try: field_value = form.optional_field_value (name) if field_value is None: return None return datetime.strptime (field_value, '%Y-%m-%d').date () except ValueError: return None
[docs]def render_bootstrap_form(rows, class_="", method="get"): """Returns a submit button styled with bootstrap. :param rows: the content of the form, usually created with a list of :func:`render_bootstrap_form_row` :param name: the form control name to use. :param class_: CSS class to apply to the button """ return html.form ( rows, action="", method=method, class_=class_ )
[docs]def render_bootstrap_form_row (form_tuples, head_class="", tail_class=""): """ Wrapper functions intended for creating a bootstrap 'row' of elements with optional labels. Renders a tuples contraining a tuple consisting of an optional label followed by a form div. :param form_tuples: a list in the form [(label_name, input, class)] where in each tuple: label_name: Is a string that is used as a label, if False, label is unsed A pre_rendered input object (e.g. input, select, checkmark), assumes it has the 'form-control' class Classes to add to individual bootstrap column sizes (e.g. 'col-xs-6') used for the label and input :param head_class: a class to be applied to the label div (first) :param tail_class: a class to be applied to the inputs div (last) """ labels = html.div (class_="row " + head_class) inputs = html.div (class_="row " + tail_class) return_labels = True for element in form_tuples: if element is None: continue label_name, obj, label_class = element if return_labels and label_name and "name=" in obj.string (): obj_name = obj.attrs["name"][0].content # extract name given to object labels.append ( html.div ( html.label(label_name, for_=obj_name), class_ = label_class ) ) else: return_labels = False inputs.append( html.div( obj, class_ = label_class ) ) if return_labels: return (labels, inputs) else: return inputs