Add DecimalFieldRenderer.

This commit is contained in:
Lance Edgar 2015-07-20 12:47:37 -05:00
parent b6192b49f2
commit ab23a8067c
2 changed files with 24 additions and 0 deletions

View file

@ -27,6 +27,7 @@ FormAlchemy Field Renderers
from tailbone.forms.renderers.common import ( from tailbone.forms.renderers.common import (
AutocompleteFieldRenderer, AutocompleteFieldRenderer,
DateTimeFieldRenderer, DateTimeFieldRenderer,
DecimalFieldRenderer,
EnumFieldRenderer, EnumFieldRenderer,
YesNoFieldRenderer, YesNoFieldRenderer,
) )

View file

@ -124,6 +124,29 @@ class EnumFieldRenderer(SelectFieldRenderer):
return SelectFieldRenderer.render(self, opts, **kwargs) return SelectFieldRenderer.render(self, opts, **kwargs)
class DecimalFieldRenderer(formalchemy.FieldRenderer):
"""
Sort of generic field renderer for decimal values. You must provide the
number of places after the decimal (scale). Note that this in turn relies
on simple string formatting; the renderer does not attempt any mathematics
of its own.
"""
def __init__(self, scale):
self.scale = scale
def __call__(self, field):
super(DecimalFieldRenderer, self).__init__(field)
return self
def render_readonly(self, **kwargs):
value = self.raw_value
if value is None:
return ''
fmt = '{{0:0.{0}f}}'.format(self.scale)
return fmt.format(value)
class YesNoFieldRenderer(CheckBoxFieldRenderer): class YesNoFieldRenderer(CheckBoxFieldRenderer):
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):