1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from itertools import chain
5
from django.forms.widgets import TextInput, Select, SelectMultiple
6
from django.conf import settings
7
from django.utils.safestring import mark_safe
10
def read_only_markup(value):
13
markup = '<span class="rofield">%s</span>' % value
14
return mark_safe(markup)
17
class ROAwareTextInput(TextInput):
18
def render(self, name, value, attrs=None):
19
if getattr(settings, 'READ_ONLY_MODE', False):
20
return read_only_markup(value)
22
return super(ROAwareTextInput, self).render(name, value, attrs)
25
class ROAwareSelect(Select):
26
def render(self, name, value, attrs=None, choices=()):
27
if getattr(settings, 'READ_ONLY_MODE', False):
29
for option_value, option_label in chain(self.choices, choices):
30
if option_value == value:
33
return read_only_markup(label)
35
return super(ROAwareSelect, self).render(name, value, attrs,
39
class CommaSeparatedWidget(SelectMultiple):
41
def render(self, name, value, attrs=None, choices=()):
44
if isinstance(value, list):
47
vals = value.split(',')
48
return super(CommaSeparatedWidget, self).render(
49
name, vals, attrs, choices)