def _select(context, id, label, options, disabled, blank_option): option_elements = [] selected = unicode((context.get('data', False) or {}).get(id, '')) if blank_option: o = html.OPTION(value="") option_elements.append(o) for option in options: if isinstance(option, (list, tuple)): value, text = option else: value, text = (option, option) value = unicode(value) text = unicode(text) if value == selected: o = html.OPTION(text, value=value, selected="selected") else: o = html.OPTION(text, value=value) option_elements.append(o) if disabled or context.get('disabled_form', False): element = html.DIV( html.LABEL(label, {'for': id}), html.SELECT("\n", *option_elements, name=id, id=id, disabled="disabled")) else: element = html.DIV(html.LABEL(label, {'for': id}), html.SELECT("\n", *option_elements, name=id, id=id)) add_errors(context, element, id) return element
def _input(context, id, label, input_type, value=_default, class_=None, extra_attrs=None, name=None): if name is None: name = id if value is _default: value = unicode((context.get('data', False) or {}).get(id, '')) if not extra_attrs: extra_attrs = dict() if class_: extra_attrs['class'] = class_ if context.get('disabled_form', False): element = html.DIV( html.LABEL(label, {'for': id}), html.INPUT(extra_attrs, type=input_type, name=name, id=id, value=value, disabled="disabled")) else: element = html.DIV( html.LABEL(label, {'for': id}), html.INPUT(extra_attrs, type=input_type, name=name, id=id, value=value)) add_errors(context, element, id) return element
def _textarea(context, id, label): data = context.get('data', False) or {} text = data.get(id, '') if isinstance(text, str): text = text.decode('utf8') else: text = unicode(text) if context.get('disabled_form', False): element = html.DIV( html.LABEL(label, {'for': id}), html.TEXTAREA( text, name=id, id=id, disabled="disabled", )) else: element = html.DIV(html.LABEL(label, {'for': id}), html.TEXTAREA( text, name=id, id=id, )) add_errors(context, element, id) return element
def pagination(context, item_url, kwargs_filter=default_kwargs_filter, class_=None): prev = prev_li(context, item_url, kwargs_filter) next = next_li(context, item_url, kwargs_filter) total = context['total'] offset = context['offset'] limit = context['limit'] order = context.get('order', False) elements = [] elements.append(prev) for page in page_counter(context): href_kwargs = kwargs_filter(context=context, offset=page['offset'], limit=page['limit'], order=order) href = item_url(**href_kwargs) if page['active']: li_class = {'class': (page['active'] and "active" or "")} else: li_class = {} el = html.LI(html.A(str(page['page']), href=href), li_class) elements.append(el) elements.append(next) classes = ['pagination'] if class_: classes.append(class_) classes = " ".join(classes) div = html.DIV({'class': classes}, html.UL(*elements)) return lxml.html.tostring(div, pretty_print=True)
def _checkbox(context, id, label, value=_default, disabled=False, name=None): elements = _input(context, id, label, input_type="checkbox", value=value, name=name) data_value = (context.get('data', False) or {}).get(id, '') if isinstance(data_value, (list, tuple)): if value in data_value: elements[1].attrib['checked'] = "checked" if disabled or context.get('disabled_form', False): elements[1].attrib['disabled'] = "disabled" return html.DIV(*elements)
def _boostrapise(func, context, id, class_=None, controls_length=1, **kwargs): elements = func(context=context, id=id, **kwargs) label = elements[0] rest = elements[1:] label.attrib['class'] = "control-label" input = html.DIV({'class': 'controls'}, *rest) fieldset_classes = ['control-group'] if context.get('errors', None): if context['errors'].get(id, ''): fieldset_classes.append("error") if class_: fieldset_classes.append(class_) element = html.FIELDSET( label, input, {'class': " ".join(fieldset_classes)}, ) help = element.xpath("//span[contains(@class, 'error')]") if help: help[0].attrib['class'] = help[0].attrib['class'] + ' help-inline' return element
def _wysihtml5_toolbar(id): toolbar = html.DIV( { 'class': "btn-toolbar", 'id': "toolbar" }, html.DIV( {'class': "btn-group"}, _wysihtml5_button("Bold", "bold"), _wysihtml5_button("Italic", "Italic"), _wysihtml5_button("Insert Link", "createLink"), html.DIV( { 'data-wysihtml5-dialog': "createLink", 'style': "display: none;", 'class': "modal" }, html.DIV(html.H3("Insert Link"), {'class': "modal-header"}), html.DIV({"class": "modal-form"}, html.FIELDSET( {'class': "control-group"}, html.LABEL("Href:", { 'for': id + "-input", "class": "control-label" }), html.DIV( {'class': "controls"}, html.INPUT( {'data-wysihtml5-dialog-field': "href"}, id=id + "-input", value="http://")))), html.DIV( {"class": "modal-footer"}, html.A("Save", { 'data-wysihtml5-dialog-action': "save", 'class': "btn" }), html.A("Cancel", { 'data-wysihtml5-dialog-action': "cancel", 'class': "btn" }))))) return toolbar
def test_add_class(): d = html.DIV() wrappers._add_class(d, 'foo') assert d.attrib['class'] == 'foo'
def wysihtml5(context, id, label): elements = _wysihtml5(context, id, label) return html.DIV(*elements)
def ckeditor(context, id, label, ckeditor_config=None): elements = _ckeditor(context, id, label, ckeditor_config or dict()) return html.DIV(*elements)