Source code for uw.web.html.bootstrapform


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

[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 (("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_range_selector (name, end, start=date.today(), current=None, 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 end: the last selectable date value. :param start: the earlest selectable date value (defaults to today). :param current: the initial value of the form controls (defaults to today). :param class_: CSS class to apply to the input. :param disable_days: 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) :return: an HTML input menu controls, for entry of date in the isoformat (yyyy-mm-dd). User can either manually write date or select date from calender. """ if current is None: current = start classes = "datepicker form-control" + ("" if class_ is None else (" " + class_)) 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"] = end.isoformat () 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 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 ( (html.table (row) for row in rows), action="", method=method, class_=class_ )
[docs]def render_bootstrap_form_row (form_tuples, head_class=None, tail_class=None): """ 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.tr (class_=head_class) inputs = html.tr (class_=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.th ( html.label(label_name, for_=obj_name), class_ = label_class ) ) else: return_labels = False inputs.append( html.td ( obj, class_ = label_class ) ) if return_labels: return (labels, inputs) else: return inputs