2014-07-29 14:29:59 +00:00
|
|
|
import re
|
2014-11-11 12:05:47 +00:00
|
|
|
import textwrap
|
2014-07-29 14:29:59 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
|
|
|
from django.utils.safestring import mark_safe
|
2014-07-08 15:19:15 +00:00
|
|
|
|
2015-04-26 13:53:00 +00:00
|
|
|
from django.contrib.admin.templatetags.admin_static import static
|
|
|
|
|
2015-04-27 12:24:17 +00:00
|
|
|
|
2015-04-26 13:53:00 +00:00
|
|
|
class SpanWidget(forms.Widget):
|
|
|
|
"""
|
|
|
|
Renders a value wrapped in a <span> tag.
|
|
|
|
Requires use of specific form support. (see ReadonlyForm or ReadonlyModelForm)
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.tag = kwargs.pop('tag', '<span>')
|
2015-04-27 12:24:17 +00:00
|
|
|
self.original = kwargs.pop('original', '')
|
|
|
|
self.display = kwargs.pop('display', None)
|
2015-04-26 13:53:00 +00:00
|
|
|
super(SpanWidget, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def render(self, name, value, attrs=None):
|
|
|
|
final_attrs = self.build_attrs(attrs, name=name)
|
2015-04-27 12:24:17 +00:00
|
|
|
original = self.original or value
|
|
|
|
display = original if self.display is None else self.display
|
2015-04-26 13:53:00 +00:00
|
|
|
# Display icon
|
2015-04-27 12:24:17 +00:00
|
|
|
if isinstance(original, bool):
|
2016-05-03 12:14:59 +00:00
|
|
|
icon = static('admin/img/icon-%s.svg' % ('yes' if original else 'no',))
|
2015-10-05 13:31:08 +00:00
|
|
|
return mark_safe('<img src="%s" alt="%s">' % (icon, display))
|
2015-04-26 13:53:00 +00:00
|
|
|
tag = self.tag[:-1]
|
|
|
|
endtag = '/'.join((self.tag[0], self.tag[1:]))
|
2015-05-03 17:44:46 +00:00
|
|
|
return mark_safe('%s%s >%s%s' % (tag, forms.utils.flatatt(final_attrs), display, endtag))
|
2015-04-26 13:53:00 +00:00
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
2015-04-27 12:24:17 +00:00
|
|
|
return self.original
|
2014-07-09 16:17:43 +00:00
|
|
|
|
2014-07-08 15:19:15 +00:00
|
|
|
def _has_changed(self, initial, data):
|
|
|
|
return False
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-07-29 14:29:59 +00:00
|
|
|
def paddingCheckboxSelectMultiple(padding):
|
|
|
|
""" Ugly hack to render this widget nicely on Django admin """
|
|
|
|
widget = forms.CheckboxSelectMultiple()
|
|
|
|
old_render = widget.render
|
|
|
|
def render(self, *args, **kwargs):
|
|
|
|
value = old_render(self, *args, **kwargs)
|
2015-04-23 14:34:04 +00:00
|
|
|
value = re.sub(r'^<ul id=([^>]+)>',
|
|
|
|
r'<ul id=\1 style="padding-left:%ipx">' % padding, value, 1)
|
2014-07-29 14:29:59 +00:00
|
|
|
return mark_safe(value)
|
|
|
|
widget.render = render
|
|
|
|
return widget
|
2014-11-11 12:05:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DynamicHelpTextSelect(forms.Select):
|
|
|
|
def __init__(self, target, help_text, *args, **kwargs):
|
|
|
|
help_text = self.get_dynamic_help_text(target, help_text)
|
2015-03-04 21:06:16 +00:00
|
|
|
attrs = kwargs.get('attrs', {})
|
|
|
|
attrs.update({
|
2014-11-11 12:05:47 +00:00
|
|
|
'onClick': help_text,
|
|
|
|
'onChange': help_text,
|
2015-03-04 21:06:16 +00:00
|
|
|
})
|
2014-11-11 12:05:47 +00:00
|
|
|
attrs.update(kwargs.get('attrs', {}))
|
|
|
|
kwargs['attrs'] = attrs
|
|
|
|
super(DynamicHelpTextSelect, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_dynamic_help_text(self, target, help_text):
|
|
|
|
return textwrap.dedent("""\
|
|
|
|
siteoptions = {help_text};
|
|
|
|
valueelement = $("#" + {target});
|
2015-03-04 21:06:16 +00:00
|
|
|
help_text = siteoptions[this.options[this.selectedIndex].value] || ""
|
2014-11-11 12:05:47 +00:00
|
|
|
valueelement.parent().find('p').remove();
|
2015-03-04 21:06:16 +00:00
|
|
|
valueelement.parent().append("<p class='help'>" + help_text + "</p>");\
|
|
|
|
""".format(target=target, help_text=str(help_text))
|
2014-11-11 12:05:47 +00:00
|
|
|
)
|