5454
[auf_rh_dae.git] / project / dae / widgets.py
1 from django.utils.safestring import mark_safe
2 from django.forms.widgets import Select, TextInput
3 from django.db.models.query import QuerySet
4
5
6 class ReadOnlyChoiceWidget(TextInput):
7 def __init__(self, *a, **kw):
8 choices = kw.pop('choices', [])
9 super(ReadOnlyChoiceWidget, self).__init__(*a, **kw)
10 self.choices = choices
11
12 def render(self, name, value, attrs=None):
13 if isinstance(self.choices, QuerySet) and value:
14 display = self.choices.get(id=value)
15 elif value:
16 display = dict(self.choices)[value]
17 else:
18 display = ''
19 return mark_safe(
20 '%(display)s<input id="id_%(name)s" type="hidden" '
21 'name="%(name)s" value="%(value)s" />' % {
22 'display': display,
23 'name': name,
24 'value': value,
25 })
26
27
28 class ReadOnlyWidget(TextInput):
29 def render(self, name, value, attrs=None):
30 return mark_safe(
31 '%(display)s<input id="id_%(name)s" type="hidden" '
32 'name="%(name)s" value="%(value)s" />' % {
33 'display': value,
34 'name': name,
35 'value': value,
36 })