from ll.xist.ns import html
from ll.xist import xsc
from datetime import datetime, date
html.button = button
html.input = input
[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