From 3070c280ccc0f9e4b7d1c78969e54be6c0b05f35 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 27 Sep 2013 08:16:22 -0700 Subject: [PATCH] Made `EnumFieldRenderer` a proper class. --- tailbone/forms/renderers/common.py | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/tailbone/forms/renderers/common.py b/tailbone/forms/renderers/common.py index ca908701..f2e7743d 100644 --- a/tailbone/forms/renderers/common.py +++ b/tailbone/forms/renderers/common.py @@ -76,26 +76,32 @@ class AutocompleteFieldRenderer(FieldRenderer): return unicode(value) -def EnumFieldRenderer(enum): +class EnumFieldRenderer(SelectFieldRenderer): """ - Adds support for enumeration fields. + Renderer for simple enumeration fields. """ - class Renderer(SelectFieldRenderer): - - def render_readonly(self, **kwargs): - value = self.raw_value - if value is None: - return '' - if value in enum: - return enum[value] - return str(value) + enumeration = {} - def render(self, **kwargs): - opts = [(enum[x], x) for x in sorted(enum)] - return SelectFieldRenderer.render(self, opts, **kwargs) + def __init__(self, arg): + if isinstance(arg, dict): + self.enumeration = arg + else: + self(arg) - return Renderer + def __call__(self, field): + super(EnumFieldRenderer, self).__init__(field) + return self + + def render_readonly(self, **kwargs): + value = self.raw_value + if value is None: + return u'' + return self.enumeration.get(value, unicode(value)) + + def render(self, **kwargs): + opts = [(self.enumeration[x], x) for x in sorted(self.enumeration)] + return SelectFieldRenderer.render(self, opts, **kwargs) class YesNoFieldRenderer(CheckBoxFieldRenderer):