Add flag for rendering key value, for enum field renderers

Only valid during the readonly rendering.  Not sure how useful this will
be in the long run...
This commit is contained in:
Lance Edgar 2016-04-06 21:17:02 -05:00
parent 90bde4f2e0
commit 30e6b6e29c

View file

@ -158,12 +158,13 @@ class EnumFieldRenderer(SelectFieldRenderer):
"""
Renderer for simple enumeration fields.
"""
enumeration = {}
render_key = False
def __init__(self, arg):
def __init__(self, arg, render_key=False):
if isinstance(arg, dict):
self.enumeration = arg
self.render_key = render_key
else:
self(arg)
@ -174,8 +175,11 @@ class EnumFieldRenderer(SelectFieldRenderer):
def render_readonly(self, **kwargs):
value = self.raw_value
if value is None:
return u''
return self.enumeration.get(value, unicode(value))
return ''
rendered = self.enumeration.get(value, unicode(value))
if self.render_key:
rendered = '{} - {}'.format(value, rendered)
return rendered
def render(self, **kwargs):
opts = [(self.enumeration[x], x) for x in self.enumeration]