def slider_edit_renderer(widget, data):
    value = fetch_value(widget, data)
    content = ''
    range = attr_value('range', widget, data)
    if range is True:
        lower_input_attrs = {
            'type': 'text',
            'name': '%s.lower' % widget.dottedpath,
            'id': cssid(widget, 'input-lower'),
            'style': 'display:none;',
            'class': 'lower_value',
            'value': value and value[0],
        }
        content += data.tag('input', **lower_input_attrs)
        upper_input_attrs = {
            'type': 'text',
            'name': '%s.upper' % widget.dottedpath,
            'id': cssid(widget, 'input-upper'),
            'style': 'display:none;',
            'class': 'upper_value',
            'value': value and value[1],
        }
        content += data.tag('input', **upper_input_attrs)
    else:
        input_attrs = {
            'type': 'text',
            'name': widget.dottedpath,
            'id': cssid(widget, 'input'),
            'style': 'display:none;',
            'class': 'slider_value',
            'value': value,
        }
        content += data.tag('input', **input_attrs)
    show_value = attr_value('show_value', widget, data)
    if show_value:
        unit = attr_value('unit', widget, data)
        if unit:
            content += data.tag('span', '%s: ' % unit, **{'class': 'unit'})
        if range is True:
            content += data.tag('span', value[0], **{'class': 'lower_value'})
            content += ' - '
            content += data.tag('span', value[1], **{'class': 'upper_value'})
        else:
            content += data.tag('span', value, **{'class': 'slider_value'})
    slider_attrs = {'class': 'slider'}
    if attr_value('orientation', widget, data) == 'vertical':
        height = attr_value('height', widget, data)
        if height:
            slider_attrs['style'] = 'height:%spx;' % height
    content += data.tag('div', ' ', **slider_attrs)
    wrapper_attrs = data_attrs_helper(widget, data, js_options)
    wrapper_attrs['class'] = cssclasses(widget, data)
    html_data = widget.attrs['data']
    data_keys = html_data.keys()
    for key in data_keys:
        if key in js_options:
            raise ValueError(u"Additional data dict contains reserved "
                             u"attribute name '%s'" % key)
        wrapper_attrs['data-%s' % key] = html_data[key]
    return data.tag('div', content, **wrapper_attrs)
Example #2
0
def select2_edit_renderer(widget, data):
    if attr_value('inputtag', widget, data):
        extracted = UNSET
        reset_extracted = False
        if data.extracted is not UNSET:
            extracted = data.extracted
            if isinstance(extracted, list) or isinstance(extracted, tuple):
                reset_extracted = True
                data.extracted = u','.join(extracted)
        value = UNSET
        reset_value = False
        if data.value is not UNSET:
            value = data.value
            if isinstance(value, list) or isinstance(value, tuple):
                reset_value = True
                data.value = u','.join(value)
        else:
            reset_value = True
            data.value = u''
        # force widget vocabulary as dict to make JS happy
        vocab = vocabulary(attr_value('vocabulary', widget, data, []))
        if vocab:
            # Use ordered dict to ensure order in tests
            dict_vocab = OrderedDict()
            for key, term in vocab:
                dict_vocab[key] = term
            widget.attrs['vocabulary'] = dict_vocab
        custom_attrs = data_attrs_helper(
            widget, data, ['ajaxurl', 'vocabulary'] + select2_options)
        renderer = input_generic_renderer
        rendered = renderer(widget, data, custom_attrs=custom_attrs)
        if reset_extracted:
            data.extracted = extracted
        if reset_value:
            data.value = value
    else:
        multiple = attr_value('multiple', widget, data)
        if multiple:
            widget.attrs['multivalued'] = True
            del widget.attrs['multiple']
        custom_attrs = data_attrs_helper(widget, data,
                                         ['ajaxurl'] + select2_options)
        renderer = select_edit_renderer
        rendered = renderer(widget, data, custom_attrs=custom_attrs)
    return rendered
def select2_edit_renderer(widget, data):
    if attr_value('inputtag', widget, data):
        extracted = UNSET
        reset_extracted = False
        if data.extracted is not UNSET:
            extracted = data.extracted
            if isinstance(extracted, list) or isinstance(extracted, tuple):
                reset_extracted = True
                data.extracted = u','.join(extracted)
        value = UNSET
        reset_value = False
        if data.value is not UNSET:
            value = data.value
            if isinstance(value, list) or isinstance(value, tuple):
                reset_value = True
                data.value = u','.join(value)
        else:
            reset_value = True
            data.value = u''
        # force widget vocabulary as dict to make JS happy
        vocab = vocabulary(attr_value('vocabulary', widget, data, []))
        if vocab:
            # Use ordered dict to ensure order in tests
            dict_vocab = OrderedDict()
            for key, term in vocab:
                dict_vocab[key] = term
            widget.attrs['vocabulary'] = dict_vocab
        custom_attrs = data_attrs_helper(
            widget, data, ['ajaxurl', 'vocabulary'] + select2_options)
        renderer = input_generic_renderer
        rendered = renderer(widget, data, custom_attrs=custom_attrs)
        if reset_extracted:
            data.extracted = extracted
        if reset_value:
            data.value = value
    else:
        multiple = attr_value('multiple', widget, data)
        if multiple:
            widget.attrs['multivalued'] = True
            del widget.attrs['multiple']
        custom_attrs = data_attrs_helper(
            widget, data, ['ajaxurl'] + select2_options)
        renderer = select_edit_renderer
        rendered = renderer(widget, data, custom_attrs=custom_attrs)
    return rendered
def recaptcha_edit_renderer(widget, data):
    recaptcha_attrs = {
        'id': cssid(widget, 'recaptcha'),
        'class': ' '.join([cssclasses(widget, data)]),
    }
    data_attrs = ['theme', 'lang', 'public_key']
    recaptcha_attrs.update(data_attrs_helper(widget, data, data_attrs))
    recaptcha = data.tag('div', ' ', **recaptcha_attrs)
    public_key = attr_value('public_key', widget, data)
    return recaptcha + NO_SCRIPT_TEMPLATE.format(public_key=public_key)
Example #5
0
    def test_data_attrs_helper(self):
        data = AttributedNode()
        widget = AttributedNode()
        widget.attrs['testattr1'] = 'value'
        widget.attrs['testattr2'] = True
        widget.attrs['testattr3'] = False
        widget.attrs['testattr4'] = None
        widget.attrs['testattr5'] = ['item1', 'item2', 'item3']
        widget.attrs['testattr6'] = {
            'key1': 'item1',
            'key2': 'item2',
            'key3': 'item3'
        }
        widget.attrs['testattr7'] = 1234
        widget.attrs['testattr8'] = 1234.5678
        widget.attrs['testattr9'] = UNSET
        widget.attrs['camelAttrName'] = 'camelValue'
        data_attrs_keys = [
            'testattr1', 'testattr2', 'testattr3', 'testattr4', 'testattr5',
            'testattr6', 'testattr7', 'testattr8', 'camelAttrName'
        ]
        data_attrs = data_attrs_helper(widget, data, data_attrs_keys)

        self.assertEqual(data_attrs['data-testattr1'], 'value')
        self.assertEqual(data_attrs['data-testattr2'], 'true')
        self.assertEqual(data_attrs['data-testattr3'], 'false')
        self.assertFalse('data-testattr4' in data_attrs)
        self.assertEqual(
            data_attrs['data-testattr5'],
            '["item1", "item2", "item3"]'
        )
        self.assertTrue(data_attrs['data-testattr6'].find('"key1": "item1"'))
        self.assertTrue(data_attrs['data-testattr6'].find('"key2": "item2"'))
        self.assertTrue(data_attrs['data-testattr6'].find('"key3": "item3"'))
        self.assertTrue(data_attrs['data-testattr6'].startswith('{'))
        self.assertTrue(data_attrs['data-testattr6'].endswith('}'))
        self.assertEqual(data_attrs['data-testattr7'], '1234')
        self.assertEqual(data_attrs['data-testattr8'], '1234.5678')
        self.assertFalse('data-testattr9' in data_attrs)
        self.assertEqual(data_attrs['data-camel-attr-name'], 'camelValue')

        # Test with Tag renderer
        tag = Tag(lambda msg: msg)
        self.check_output("""
        <dummy
          data-camel-attr-name='camelValue'
          data-testattr1='value'
          data-testattr2='true'
          data-testattr3='false'
          data-testattr5='["item1", "item2", "item3"]'
          data-testattr6='{"..."}'
          data-testattr7='1234'
          data-testattr8='1234.5678'
          name="foo" />
        """, tag('dummy', name='foo', **data_attrs))
Example #6
0
    def test_data_attrs_helper(self):
        data = AttributedNode()
        widget = AttributedNode()
        widget.attrs['testattr1'] = 'value'
        widget.attrs['testattr2'] = True
        widget.attrs['testattr3'] = False
        widget.attrs['testattr4'] = None
        widget.attrs['testattr5'] = ['item1', 'item2', 'item3']
        widget.attrs['testattr6'] = {
            'key1': 'item1',
            'key2': 'item2',
            'key3': 'item3'
        }
        widget.attrs['testattr7'] = 1234
        widget.attrs['testattr8'] = 1234.5678
        widget.attrs['testattr9'] = UNSET
        widget.attrs['camelAttrName'] = 'camelValue'
        data_attrs_keys = [
            'testattr1', 'testattr2', 'testattr3', 'testattr4', 'testattr5',
            'testattr6', 'testattr7', 'testattr8', 'camelAttrName'
        ]
        data_attrs = data_attrs_helper(widget, data, data_attrs_keys)

        self.assertEqual(data_attrs['data-testattr1'], 'value')
        self.assertEqual(data_attrs['data-testattr2'], 'true')
        self.assertEqual(data_attrs['data-testattr3'], 'false')
        self.assertFalse('data-testattr4' in data_attrs)
        self.assertEqual(data_attrs['data-testattr5'],
                         '["item1", "item2", "item3"]')
        self.assertTrue(data_attrs['data-testattr6'].find('"key1": "item1"'))
        self.assertTrue(data_attrs['data-testattr6'].find('"key2": "item2"'))
        self.assertTrue(data_attrs['data-testattr6'].find('"key3": "item3"'))
        self.assertTrue(data_attrs['data-testattr6'].startswith('{'))
        self.assertTrue(data_attrs['data-testattr6'].endswith('}'))
        self.assertEqual(data_attrs['data-testattr7'], '1234')
        self.assertEqual(data_attrs['data-testattr8'], '1234.5678')
        self.assertFalse('data-testattr9' in data_attrs)
        self.assertEqual(data_attrs['data-camel-attr-name'], 'camelValue')

        # Test with Tag renderer
        tag = Tag(lambda msg: msg)
        self.check_output(
            """
        <dummy
          data-camel-attr-name='camelValue'
          data-testattr1='value'
          data-testattr2='true'
          data-testattr3='false'
          data-testattr5='["item1", "item2", "item3"]'
          data-testattr6='{"..."}'
          data-testattr7='1234'
          data-testattr8='1234.5678'
          name="foo" />
        """, tag('dummy', name='foo', **data_attrs))
def wysihtml5_textarea_renderer(widget, data):
    custom_attrs = data_attrs_helper(widget, data, wysihtml5_options)
    return textarea_renderer(widget, data, custom_attrs=custom_attrs)
def chosen_edit_renderer(widget, data):
    custom_attrs = data_attrs_helper(widget, data, chosen_options)
    return select_edit_renderer(widget, data, custom_attrs=custom_attrs)