from django import forms from django.utils.safestring import mark_safe from django.utils.encoding import force_text class ShowTextWidget(forms.Widget): def render(self, name, value, attrs): value = force_text(value) if value is None: return '' if hasattr(self, 'initial'): value = self.initial if self.bold: final_value = u'%s' % (value) else: final_value = '
'.join(value.split('\n')) if self.warning: final_value = u'' %(final_value) if self.hidden: final_value = u'%s' % (final_value, name, value) return mark_safe(final_value) def __init__(self, *args, **kwargs): for kwarg in ['bold', 'warning', 'hidden']: setattr(self, kwarg, kwargs.pop(kwarg, False)) super(ShowTextWidget, self).__init__(*args, **kwargs) def _has_changed(self, initial, data): return False 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