5454
[auf_rh_dae.git] / project / dae / widgets.py
CommitLineData
4718c21c 1from django.utils.safestring import mark_safe
661da766 2from django.forms.widgets import Select, TextInput
bc8dcc0e 3from django.db.models.query import QuerySet
4718c21c
BS
4
5
bc8dcc0e
BS
6class 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 = ''
4718c21c 19 return mark_safe(
bc8dcc0e
BS
20 '%(display)s<input id="id_%(name)s" type="hidden" '
21 'name="%(name)s" value="%(value)s" />' % {
22 'display': display,
4718c21c
BS
23 'name': name,
24 'value': value,
25 })
661da766
BS
26
27
28class ReadOnlyWidget(TextInput):
29 def render(self, name, value, attrs=None):
30 return mark_safe(
bc8dcc0e 31 '%(display)s<input id="id_%(name)s" type="hidden" '
661da766
BS
32 'name="%(name)s" value="%(value)s" />' % {
33 'display': value,
34 'name': name,
35 'value': value,
36 })