Made EnumFieldRenderer a proper class.

This commit is contained in:
Lance Edgar 2013-09-27 08:16:22 -07:00
parent a11b8d9ff2
commit 3070c280cc

View file

@ -76,27 +76,33 @@ class AutocompleteFieldRenderer(FieldRenderer):
return unicode(value) return unicode(value)
def EnumFieldRenderer(enum): class EnumFieldRenderer(SelectFieldRenderer):
""" """
Adds support for enumeration fields. Renderer for simple enumeration fields.
""" """
class Renderer(SelectFieldRenderer): enumeration = {}
def __init__(self, arg):
if isinstance(arg, dict):
self.enumeration = arg
else:
self(arg)
def __call__(self, field):
super(EnumFieldRenderer, self).__init__(field)
return self
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):
value = self.raw_value value = self.raw_value
if value is None: if value is None:
return '' return u''
if value in enum: return self.enumeration.get(value, unicode(value))
return enum[value]
return str(value)
def render(self, **kwargs): def render(self, **kwargs):
opts = [(enum[x], x) for x in sorted(enum)] opts = [(self.enumeration[x], x) for x in sorted(self.enumeration)]
return SelectFieldRenderer.render(self, opts, **kwargs) return SelectFieldRenderer.render(self, opts, **kwargs)
return Renderer
class YesNoFieldRenderer(CheckBoxFieldRenderer): class YesNoFieldRenderer(CheckBoxFieldRenderer):