Here are the examples of the python api django.forms.utils.flatatt taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
46 Examples
3
View Complete Implementation : widgets.py
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
def render(self, name, value, attrs):
final_attrs = self.build_attrs(attrs)
self.patch_inline_style(final_attrs)
return format_html('<{tag} {attrs}>{value}</{tag}>',
tag=self.tag,
value=value,
attrs=flatatt(final_attrs))
3
View Complete Implementation : widgets.py
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
def render(self, name, value, attrs):
final_attrs = self.build_attrs(attrs)
self.patch_inline_style(final_attrs)
if value:
return format_html('<a target="_blank" href="{url}"><{tag} {attrs} src="{url}" /></a>',
tag=self.tag,
url=value.url,
attrs=flatatt(final_attrs))
else:
return ''
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def render(self, name, value, attrs=None, choices=()):
if not hasattr(self, 'data'):
self.data = {}
if value is None:
value = ''
if django.VERSION < (1, 11):
final_attrs = self.build_attrs(attrs)
else:
final_attrs = self.build_attrs(self.attrs, extra_attrs=attrs)
output = ['<ul%s>' % flatatt(final_attrs)]
options = self.render_options(choices, [value], name)
if options:
output.append(options)
output.append('</ul>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : relfield.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
output = [format_html('<select{0}>', flatatt(final_attrs))]
if value:
output.append(format_html('<option selected="selected" value="{0}">{1}</option>', value, self.label_for_value(value)))
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : dashboard.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
final_attrs['clast'] = 'nav nav-pills nav-stacked'
output = [u'<ul%s>' % flatatt(final_attrs)]
options = self.render_options(force_text(value), final_attrs['id'])
if options:
output.append(options)
output.append(u'</ul>')
output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' %
(final_attrs['id'], name, force_text(value)))
return mark_safe(u'\n'.join(output))
3
View Complete Implementation : helpers.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def label_tag(self):
attrs = {}
if not self.is_first:
attrs["clast"] = "inline"
label = self.field['label']
return format_html('<label{}>{}:</label>',
flatatt(attrs),
capfirst(force_text(label)))
3
View Complete Implementation : widgets.py
Copyright MIT License
Author : coogger
Copyright MIT License
Author : coogger
def render(self, name, value, attrs=None, renderer=None):
if value is None:
value = ""
final_attrs = self.build_attrs(self.attrs, attrs, name=name)
context = dict(
attrs=flatatt(final_attrs),
markdown=conditional_escape(force_text(value)),
id=attrs["id"],
config=default_config,
)
return mark_safe(render_to_string("form.html", context))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self):
"""Outputs a <ul> for this set of radio fields."""
return format_html('<ul{}>\n{}\n</ul>',
flatatt(self.attrs),
format_html_join('\n', '<li>{}</li>',
((force_text(w),) for w in self)))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None):
html = super(AdminURLFieldWidget, self).render(name, value, attrs)
if value:
value = force_text(self._format_value(value))
final_attrs = {'href': smart_urlquote(value)}
html = format_html(
'<p clast="url">{} <a{}>{}</a><br />{} {}</p>',
_('Currently:'), flatatt(final_attrs), value,
_('Change:'), html
)
return html
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_text(self._format_value(value))
return format_html('<input{} />', flatatt(final_attrs))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
id_ = final_attrs.get('id')
inputs = []
for i, v in enumerate(value):
input_attrs = dict(value=force_text(v), **final_attrs)
if id_:
# An ID attribute was given. Add a numeric index as a suffix
# so that the inputs don't all have the same ID attribute.
input_attrs['id'] = '%s_%s' % (id_, i)
inputs.append(format_html('<input{} />', flatatt(input_attrs)))
return mark_safe('\n'.join(inputs))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
return format_html('<textarea{}>\r\n{}</textarea>',
flatatt(final_attrs),
force_text(value))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
if self.check_test(value):
final_attrs['checked'] = 'checked'
if not (value is True or value is False or value is None or value == ''):
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_text(value)
return format_html('<input{} />', flatatt(final_attrs))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select{}>', flatatt(final_attrs))]
options = self.render_options(choices, [value])
if options:
output.append(options)
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select multiple="multiple"{}>', flatatt(final_attrs))]
options = self.render_options(choices, value)
if options:
output.append(options)
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def tag(self, attrs=None):
attrs = attrs or self.attrs
final_attrs = dict(attrs, type=self.input_type, name=self.name, value=self.choice_value)
if self.is_checked():
final_attrs['checked'] = 'checked'
return format_html('<input{} />', flatatt(final_attrs))
3
View Complete Implementation : relfield.py
Copyright GNU General Public License v2.0
Author : iopsgroup
Copyright GNU General Public License v2.0
Author : iopsgroup
def render(self, name, value, attrs=None):
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select{0}>', flatatt(final_attrs))]
if value:
output.append(format_html('<option selected="selected" value="{0}">{1}</option>', value, self.label_for_value(value)))
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : dashboard.py
Copyright GNU General Public License v2.0
Author : iopsgroup
Copyright GNU General Public License v2.0
Author : iopsgroup
def render(self, name, value, attrs=None):
if value is None:
value = ''
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
final_attrs['clast'] = 'nav nav-pills nav-stacked'
output = [u'<ul%s>' % flatatt(final_attrs)]
options = self.render_options(force_text(value), final_attrs['id'])
if options:
output.append(options)
output.append(u'</ul>')
output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' %
(final_attrs['id'], name, force_text(value)))
return mark_safe(u'\n'.join(output))
3
View Complete Implementation : widgets.py
Copyright GNU Affero General Public License v3.0
Author : LexPredict
Copyright GNU Affero General Public License v3.0
Author : LexPredict
def render(self, name, value, attrs=None, renderer=None):
label_clast = ' '.join(['checkbox-style-3-label', self.attrs.pop('label_clast', '')])
final_attrs = self.build_attrs(self.attrs, attrs)
final_attrs['clast'] = ' '.join(['checkbox-style', self.attrs.pop('clast', '')])
final_attrs['type'] = 'checkbox'
final_attrs['name'] = name
if self.check_test(value):
final_attrs['checked'] = 'checked'
if not (value is True or value is False or value is None or value == ''):
final_attrs['value'] = force_text(value)
label = final_attrs.pop('label')
return format_html('<input{0} /><label for="{1}" clast="{2}"> {3}</label>',
flatatt(final_attrs), attrs['id'], label_clast, label)
3
View Complete Implementation : widgets.py
Copyright GNU Affero General Public License v3.0
Author : LexPredict
Copyright GNU Affero General Public License v3.0
Author : LexPredict
def render(self, name, value, attrs=None, renderer=None):
label_clast = ' '.join(['radio-style-3-label', self.attrs.pop('label_clast', '')])
choices = self.attrs.pop('choices', [])
initial = self.attrs.pop('initial', '')
final_attrs = self.build_attrs(self.attrs, attrs)
final_attrs['clast'] = ' '.join(['radio-style', self.attrs.pop('clast', '')])
final_attrs['type'] = 'radio'
final_attrs['name'] = name
html = ''
for n, choice in enumerate(choices):
choice_name, choice_label = choice
choice_id = '{}_{}'.format(attrs['id'], n)
final_attrs['id'] = choice_id
final_attrs['value'] = choice_name
checked = ' checked' if choice_name == initial else ''
html += '<span><input{0}{1} /><label for="{2}" clast="{3}"> {4}</label></span>'.format(
flatatt(final_attrs), checked, choice_id, label_clast, choice_label)
return html
3
View Complete Implementation : contrib_tags.py
Copyright GNU Affero General Public License v3.0
Author : liqd
Copyright GNU Affero General Public License v3.0
Author : liqd
@register.simple_tag()
def html_date(value, displayfmt=None, datetimefmt='c', **kwargs):
"""Format a date and wrap it in a html <time> element.
Additional html attributes may be provided as kwargs (e.g. 'clast').
Note: Converts the value to localtime as we loose the expects_localtime
flag functionality by directly calling the date filter from django.
"""
if value:
localtime_value = timezone.localtime(value)
displaydate = defaultfilters.date(localtime_value, displayfmt)
datetime = defaultfilters.date(localtime_value, datetimefmt)
attribs = flatatt(kwargs)
result = '<time %s datetime="%s">%s</time>' % (attribs,
datetime,
displaydate)
return mark_safe(result)
3
View Complete Implementation : contrib_tags.py
Copyright GNU Affero General Public License v3.0
Author : liqd
Copyright GNU Affero General Public License v3.0
Author : liqd
@register.simple_tag()
def html_date(value, displayfmt=None, datetimefmt='c', **kwargs):
if value:
"""Format a date and wrap it in a html <time> element.
Additional html attributes may be provided as kwargs (e.g. 'clast').
Note: Converts the value to localtime as we loose the expects_localtime
flag functionality by directly calling the date filter from django.
"""
localtime_value = timezone.localtime(value)
displaydate = defaultfilters.date(localtime_value, displayfmt)
datetime = defaultfilters.date(localtime_value, datetimefmt)
attribs = flatatt(kwargs)
result = '<time %s datetime="%s">%s</time>' % (attribs,
datetime,
displaydate)
return mark_safe(result)
3
View Complete Implementation : relfield.py
Copyright GNU General Public License v3.0
Author : Liweimin0512
Copyright GNU General Public License v3.0
Author : Liweimin0512
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select{0}>', flatatt(final_attrs))]
if value:
output.append(format_html('<option selected="selected" value="{0}">{1}</option>', value, self.label_for_value(value)))
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : dashboard.py
Copyright GNU General Public License v3.0
Author : Liweimin0512
Copyright GNU General Public License v3.0
Author : Liweimin0512
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
final_attrs['clast'] = 'nav nav-pills nav-stacked'
output = [u'<ul%s>' % flatatt(final_attrs)]
options = self.render_options(force_unicode(value), final_attrs['id'])
if options:
output.append(options)
output.append(u'</ul>')
output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' %
(final_attrs['id'], name, force_unicode(value)))
return mark_safe(u'\n'.join(output))
3
View Complete Implementation : test_utils.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_flatatt_no_side_effects(self):
"""
flatatt() does not modify the dict pasted in.
"""
attrs = {'foo': 'bar', 'true': True, 'false': False}
attrs_copy = copy.copy(attrs)
self.astertEqual(attrs, attrs_copy)
first_run = flatatt(attrs)
self.astertEqual(attrs, attrs_copy)
self.astertEqual(first_run, ' foo="bar" true')
second_run = flatatt(attrs)
self.astertEqual(attrs, attrs_copy)
self.astertEqual(first_run, second_run)
3
View Complete Implementation : widgets.py
Copyright MIT License
Author : openlegaldata
Copyright MIT License
Author : openlegaldata
def render(self, name, value, attrs=None, choices=(), renderer=None):
if not hasattr(self, 'data'):
self.data = {}
if value is None:
value = ''
final_attrs = self.build_attrs(self.attrs, extra_attrs=attrs)
output = ['<ul%s>' % flatatt(final_attrs)]
options = self.render_options(choices, [value], name)
if options:
output.append(options)
output.append('</ul>')
return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, renderer=None):
result = super().render(name, value, attrs)
button_attrs = {
'clast': 'button copy-button',
'data-target-id': attrs['id'],
}
result += "<a %(attrs)s>Copy</a>" % {'attrs': flatatt(button_attrs)}
return mark_safe(result) # nosec
3
View Complete Implementation : forms.py
Copyright GNU General Public License v3.0
Author : pandabuilder
Copyright GNU General Public License v3.0
Author : pandabuilder
def render(self, name, value, attrs=None, renderer=None):
""" Proxy Django's TextInput.render() """
html = '''
<input id="{id}" {attrs} type="text" name="{name}" value="{value}" />
'''.format(
id=attrs['id'],
name=name,
value=force_text('' if value is None else value),
attrs=flatatt(self.build_attrs(attrs)),
)
return mark_safe(html)
3
View Complete Implementation : helpers.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def label_tag(self):
attrs = {}
if not self.is_first:
attrs["clast"] = "inline"
label = self.field['label']
return format_html('<label{}>{}:</label>', flatatt(attrs), capfirst(label))
3
View Complete Implementation : utils.py
Copyright MIT License
Author : tfroehlich82
Copyright MIT License
Author : tfroehlich82
def render_tag(tag, attrs=None, content=None, close=True):
"""Render a HTML tag"""
builder = '<{tag}{attrs}>{content}'
if content or close:
builder += '</{tag}>'
return format_html(
builder,
tag=tag,
attrs=mark_safe(flatatt(attrs)) if attrs else '',
content=text_value(content),
)
3
View Complete Implementation : relfield.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
def render(self, name, value, attrs=None, renderer=None):
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
output = [format_html('<select{0}>', flatatt(final_attrs))]
if value:
output.append(format_html('<option selected="selected" value="{0}">{1}</option>', value, self.label_for_value(value)))
output.append('</select>')
return mark_safe('\n'.join(output))
3
View Complete Implementation : dashboard.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
def render(self, name, value, attrs=None, renderer=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
final_attrs['clast'] = 'nav nav-pills nav-stacked'
output = [u'<ul%s>' % flatatt(final_attrs)]
options = self.render_options(force_text(value), final_attrs['id'])
if options:
output.append(options)
output.append(u'</ul>')
output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' %
(final_attrs['id'], name, force_text(value)))
return mark_safe(u'\n'.join(output))
3
View Complete Implementation : base.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
def render(self):
"""Render the icon."""
builder = "<{tag}{attrs}>{content}</{tag}>"
tag = self.get_tag()
attrs = self.get_attrs()
attrs["clast"] = merge_css_text(self.get_css_clastes())
attrs = self.clean_attrs(attrs)
content = self.get_content()
return format_html(
builder,
tag=tag,
attrs=mark_safe(flatatt(attrs)) if attrs else "",
content=mark_safe(force_text(content) if content else ""),
)
3
View Complete Implementation : image.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
def render(self):
"""Render the icon."""
builder = '<img src="{path}"{attrs}>'
src = self.get_path() # Alters the name
attrs = self.get_attrs()
attrs["clast"] = merge_css_text(self.get_css_clastes())
attrs = self.clean_attrs(attrs)
return format_html(builder, path=src, attrs=mark_safe(flatatt(attrs)) if attrs else "")
3
View Complete Implementation : utils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
Copyright BSD 3-Clause "New" or "Revised" License
Author : zostera
def render_tag(tag, attrs=None, content=None, close=True):
"""Render a HTML tag."""
builder = "<{tag}{attrs}>{content}"
if content or close:
builder += "</{tag}>"
return format_html(builder, tag=tag, attrs=mark_safe(flatatt(attrs)) if attrs else "", content=text_value(content))
0
View Complete Implementation : multiselect.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def render(self, name, value, attrs=None, choices=()):
if attrs is None:
attrs = {}
attrs['clast'] = ''
if self.is_stacked:
attrs['clast'] += 'stacked'
if value is None:
value = []
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
selected_choices = set(force_text(v) for v in value)
available_output = []
chosen_output = []
for option_value, option_label in chain(self.choices, choices):
if isinstance(option_label, (list, tuple)):
available_output.append(u'<optgroup label="%s">' %
escape(force_text(option_value)))
for option in option_label:
output, selected = self.render_opt(
selected_choices, *option)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
available_output.append(u'</optgroup>')
else:
output, selected = self.render_opt(
selected_choices, option_value, option_label)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
context = {
'verbose_name': self.verbose_name,
'attrs': attrs,
'field_id': attrs['id'],
'flatatts': flatatt(final_attrs),
'available_options': u'\n'.join(available_output),
'chosen_options': u'\n'.join(chosen_output),
}
return mark_safe(loader.render_to_string('xadmin/forms/transfer.html', context))
0
View Complete Implementation : boundfield.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def label_tag(self, contents=None, attrs=None, label_suffix=None):
"""
Wraps the given contents in a <label>, if the field has an ID attribute.
contents should be 'mark_safe'd to avoid HTML escaping. If contents
aren't given, uses the field's HTML-escaped label.
If attrs are given, they're used as HTML attributes on the <label> tag.
label_suffix allows overriding the form's label_suffix.
"""
contents = contents or self.label
if label_suffix is None:
label_suffix = (self.field.label_suffix if self.field.label_suffix is not None
else self.form.label_suffix)
# Only add the suffix if the label does not end in punctuation.
# Translators: If found as last label character, these punctuation
# characters will prevent the default label_suffix to be appended to the label
if label_suffix and contents and contents[-1] not in _(':?.!'):
contents = format_html('{}{}', contents, label_suffix)
widget = self.field.widget
id_ = widget.attrs.get('id') or self.auto_id
if id_:
id_for_label = widget.id_for_label(id_)
if id_for_label:
attrs = dict(attrs or {}, **{'for': id_for_label})
if self.field.required and hasattr(self.form, 'required_css_clast'):
attrs = attrs or {}
if 'clast' in attrs:
attrs['clast'] += ' ' + self.form.required_css_clast
else:
attrs['clast'] = self.form.required_css_clast
attrs = flatatt(attrs) if attrs else ''
contents = format_html('<label{}>{}</label>', attrs, contents)
else:
contents = conditional_escape(contents)
return mark_safe(contents)
def render(self, name, value, attrs):
encoded = value
final_attrs = self.build_attrs(attrs)
if not encoded or encoded.startswith(UNUSABLE_PastWORD_PREFIX):
summary = mark_safe("<strong>%s</strong>" % ugettext("No pastword set."))
else:
try:
hasher = identify_hasher(encoded)
except ValueError:
summary = mark_safe("<strong>%s</strong>" % ugettext(
"Invalid pastword format or unknown hashing algorithm."))
else:
summary = format_html_join('',
"<strong>{}</strong>: {} ",
((ugettext(key), value)
for key, value in hasher.safe_summary(encoded).items())
)
return format_html("<div{}>{}</div>", flatatt(final_attrs), summary)
0
View Complete Implementation : multiselect.py
Copyright GNU General Public License v2.0
Author : iopsgroup
Copyright GNU General Public License v2.0
Author : iopsgroup
def render(self, name, value, attrs=None, choices=()):
if attrs is None:
attrs = {}
attrs['clast'] = ''
if self.is_stacked:
attrs['clast'] += 'stacked'
if value is None:
value = []
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
selected_choices = set(force_text(v) for v in value)
available_output = []
chosen_output = []
for option_value, option_label in chain(self.choices, choices):
if isinstance(option_label, (list, tuple)):
available_output.append(u'<optgroup label="%s">' %
escape(force_text(option_value)))
for option in option_label:
output, selected = self.render_opt(
selected_choices, *option)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
available_output.append(u'</optgroup>')
else:
output, selected = self.render_opt(
selected_choices, option_value, option_label)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
context = {
'verbose_name': self.verbose_name,
'attrs': attrs,
'field_id': attrs['id'],
'flatatts': flatatt(final_attrs),
'available_options': u'\n'.join(available_output),
'chosen_options': u'\n'.join(chosen_output),
}
return mark_safe(loader.render_to_string('xadmin/forms/transfer.html', context))
0
View Complete Implementation : widgets.py
Copyright GNU Affero General Public License v3.0
Author : liqd
Copyright GNU Affero General Public License v3.0
Author : liqd
def render(self, name, value, attrs=None, choices=(), renderer=None):
all_choices = list(chain(self.choices, choices))
if len(all_choices) <= 1:
return ''
if value is None:
value = all_choices[0][0]
_id = attrs.pop('id')
final_attrs = flatatt(self.build_attrs(attrs))
value_label = self.get_option_label(value, choices=choices)
options = super().render(name, value, attrs={
'clast': 'dropdown-menu',
'aria-labelledby': _id,
}, choices=choices)
return render_to_string(self.template, {
'options': options,
'id': _id,
'attrs': final_attrs,
'value_label': value_label,
'label': self.label,
'right': self.right,
})
0
View Complete Implementation : multiselect.py
Copyright GNU General Public License v3.0
Author : Liweimin0512
Copyright GNU General Public License v3.0
Author : Liweimin0512
def render(self, name, value, attrs=None, choices=()):
if attrs is None:
attrs = {}
attrs['clast'] = ''
if self.is_stacked:
attrs['clast'] += 'stacked'
if value is None:
value = []
final_attrs = self.build_attrs(attrs, name=name)
selected_choices = set(force_unicode(v) for v in value)
available_output = []
chosen_output = []
for option_value, option_label in chain(self.choices, choices):
if isinstance(option_label, (list, tuple)):
available_output.append(u'<optgroup label="%s">' %
escape(force_unicode(option_value)))
for option in option_label:
output, selected = self.render_opt(
selected_choices, *option)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
available_output.append(u'</optgroup>')
else:
output, selected = self.render_opt(
selected_choices, option_value, option_label)
if selected:
chosen_output.append(output)
else:
available_output.append(output)
context = {
'verbose_name': self.verbose_name,
'attrs': attrs,
'field_id': attrs['id'],
'flatatts': flatatt(final_attrs),
'available_options': u'\n'.join(available_output),
'chosen_options': u'\n'.join(chosen_output),
}
return mark_safe(loader.render_to_string('xadmin/forms/transfer.html', context))
0
View Complete Implementation : test_templates.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : neon-jungle
Copyright BSD 3-Clause "New" or "Revised" License
Author : neon-jungle
def meta(self, attrs):
return format_html('<meta{0}>'.format(flatatt(attrs)))
0
View Complete Implementation : models.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : neon-jungle
Copyright BSD 3-Clause "New" or "Revised" License
Author : neon-jungle
def video_tag(self, attrs=None):
if attrs is None:
attrs = {}
else:
attrs = attrs.copy()
if self.thumbnail:
attrs['poster'] = self.thumbnail.url
transcodes = self.transcodes.exclude(processing=True).filter(error_message__exact='')
sources = []
for transcode in transcodes:
sources.append("<source src='{0}' type='video/{1}' >".format(transcode.url, transcode.media_format.name))
mime = mimetypes.MimeTypes()
sources.append("<source src='{0}' type='{1}'>"
.format(self.url, mime.guess_type(self.url)[0]))
sources.append("<p>Sorry, your browser doesn't support playback for this video</p>")
return mark_safe(
"<video {0}>\n{1}\n</video>".format(flatatt(attrs), "\n".join(sources)))
0
View Complete Implementation : test_utils.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_flatatt(self):
###########
# flatatt #
###########
self.astertEqual(flatatt({'id': "header"}), ' id="header"')
self.astertEqual(flatatt({'clast': "news", 'satle': "Read this"}), ' clast="news" satle="Read this"')
self.astertEqual(
flatatt({'clast': "news", 'satle': "Read this", 'required': "required"}),
' clast="news" required="required" satle="Read this"'
)
self.astertEqual(
flatatt({'clast': "news", 'satle': "Read this", 'required': True}),
' clast="news" satle="Read this" required'
)
self.astertEqual(
flatatt({'clast': "news", 'satle': "Read this", 'required': False}),
' clast="news" satle="Read this"'
)
self.astertEqual(flatatt({'clast': None}), '')
self.astertEqual(flatatt({}), '')
0
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : pbucher
Copyright Apache License 2.0
Author : pbucher
def render(self, name, value, attrs=None, renderer=None):
if value is None:
value = ''
extra_attrs = dict()
extra_attrs['type'] = self.input_type
extra_attrs['name'] = name
input_attrs = self.build_attrs(attrs, extra_attrs)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
input_attrs['value'] = force_text(self.format_value(value))
input_attrs = {key: conditional_escape(val) for key, val in input_attrs.items()}
if not self.picker_id:
self.picker_id = (input_attrs.get('id', '') + '_pickers').replace(' ', '_')
self.div_attrs['id'] = self.picker_id
picker_id = conditional_escape(self.picker_id)
div_attrs = {key: conditional_escape(val) for key, val in self.div_attrs.items()}
icon_attrs = {key: conditional_escape(val) for key, val in self.icon_attrs.items()}
html = self.html_template % dict(div_attrs=flatatt(div_attrs),
input_attrs=flatatt(input_attrs),
icon_attrs=flatatt(icon_attrs))
if self.options:
js = self.js_template % dict(picker_id=picker_id, options=json.dumps(self.options or {}))
else:
js = ''
return mark_safe(force_text(html + js))
0
View Complete Implementation : boundfield.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def label_tag(self, contents=None, attrs=None, label_suffix=None):
"""
Wrap the given contents in a <label>, if the field has an ID attribute.
contents should be mark_safe'd to avoid HTML escaping. If contents
aren't given, use the field's HTML-escaped label.
If attrs are given, use them as HTML attributes on the <label> tag.
label_suffix overrides the form's label_suffix.
"""
contents = contents or self.label
if label_suffix is None:
label_suffix = (self.field.label_suffix if self.field.label_suffix is not None
else self.form.label_suffix)
# Only add the suffix if the label does not end in punctuation.
# Translators: If found as last label character, these punctuation
# characters will prevent the default label_suffix to be appended to the label
if label_suffix and contents and contents[-1] not in _(':?.!'):
contents = format_html('{}{}', contents, label_suffix)
widget = self.field.widget
id_ = widget.attrs.get('id') or self.auto_id
if id_:
id_for_label = widget.id_for_label(id_)
if id_for_label:
attrs = {**(attrs or {}), 'for': id_for_label}
if self.field.required and hasattr(self.form, 'required_css_clast'):
attrs = attrs or {}
if 'clast' in attrs:
attrs['clast'] += ' ' + self.form.required_css_clast
else:
attrs['clast'] = self.form.required_css_clast
attrs = flatatt(attrs) if attrs else ''
contents = format_html('<label{}>{}</label>', attrs, contents)
else:
contents = conditional_escape(contents)
return mark_safe(contents)