django-orchestra/orchestra/forms/widgets.py

120 lines
4.2 KiB
Python
Raw Normal View History

import re
import textwrap
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
from django.utils.encoding import force_text
2015-04-26 13:53:00 +00:00
from django.contrib.admin.templatetags.admin_static import static
# TODO rename readonlywidget
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>')
super(SpanWidget, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, name=name)
original_value = self.original_value
# Display icon
2015-04-26 15:52:40 +00:00
if isinstance(original_value, bool):
icon = static('admin/img/icon-%s.gif' % ('yes' if original_value else 'no',))
2015-04-26 13:53:00 +00:00
return mark_safe('<img src="%s" alt="%s">' % (icon, str(original_value)))
tag = self.tag[:-1]
endtag = '/'.join((self.tag[0], self.tag[1:]))
return mark_safe('%s%s >%s%s' % (tag, forms.util.flatatt(final_attrs), original_value, endtag))
def value_from_datadict(self, data, files, name):
return self.original_value
def _has_changed(self, initial, data):
return False
2014-07-08 15:19:15 +00:00
class ShowTextWidget(forms.Widget):
2014-07-09 16:17:43 +00:00
def __init__(self, *args, **kwargs):
2015-04-26 13:53:00 +00:00
for kwarg in ['tag', 'warning', 'hidden']:
2014-07-09 16:17:43 +00:00
setattr(self, kwarg, kwargs.pop(kwarg, False))
super(ShowTextWidget, self).__init__(*args, **kwargs)
2014-07-08 15:19:15 +00:00
def render(self, name, value, attrs):
value = force_text(value)
if value is None:
return ''
if hasattr(self, 'initial'):
value = self.initial
2015-04-26 13:53:00 +00:00
if self.tag:
endtag = '/'.join((self.tag[0], self.tag[1:]))
final_value = ''.join((self.tag, value, endtag))
2014-07-08 15:19:15 +00:00
else:
final_value = '<br/>'.join(value.split('\n'))
if self.warning:
final_value = (
2015-04-02 16:14:55 +00:00
'<ul class="messagelist"><li class="warning">%s</li></ul>'
% final_value)
2014-07-08 15:19:15 +00:00
if self.hidden:
final_value = (
2015-04-02 16:14:55 +00:00
'%s<input type="hidden" name="%s" value="%s"/>'
% (final_value, name, value))
2014-07-08 15:19:15 +00:00
return mark_safe(final_value)
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
class ReadOnlyWidget(forms.Widget):
def __init__(self, *args):
if len(args) == 1:
args = (args[0], args[0])
self.original_value = args[0]
self.display_value = args[1]
super(ReadOnlyWidget, self).__init__()
def render(self, name, value, attrs=None):
if self.display_value is not None:
return mark_safe(self.display_value)
return mark_safe(self.original_value)
def value_from_datadict(self, data, files, name):
return self.original_value
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)
return mark_safe(value)
widget.render = render
return widget
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({
'onClick': help_text,
'onChange': help_text,
2015-03-04 21:06:16 +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] || ""
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))
)