Make CurrencyFieldRenderer inherit from FloatFieldRenderer

Also cleanup some code generally..
This commit is contained in:
Lance Edgar 2016-12-12 12:13:59 -06:00
parent 06dee96af6
commit 79e63571e3

View file

@ -28,20 +28,17 @@ from __future__ import unicode_literals, absolute_import
import datetime import datetime
import pytz from rattail.time import localtime, make_utc
from rattail.time import localtime import formalchemy as fa
from formalchemy import fields as fa_fields, helpers as fa_helpers
import formalchemy
from formalchemy import helpers
from formalchemy.fields import FieldRenderer, SelectFieldRenderer, CheckBoxFieldRenderer
from pyramid.renderers import render from pyramid.renderers import render
from webhelpers.html import HTML from webhelpers.html import HTML
from tailbone.util import pretty_datetime, raw_datetime from tailbone.util import pretty_datetime, raw_datetime
class StrippedTextFieldRenderer(formalchemy.TextFieldRenderer): class StrippedTextFieldRenderer(fa.TextFieldRenderer):
""" """
Standard text field renderer, which strips whitespace from either end of Standard text field renderer, which strips whitespace from either end of
the input value on deserialization. the input value on deserialization.
@ -53,7 +50,7 @@ class StrippedTextFieldRenderer(formalchemy.TextFieldRenderer):
return value.strip() return value.strip()
class CodeTextAreaFieldRenderer(formalchemy.TextAreaFieldRenderer): class CodeTextAreaFieldRenderer(fa.TextAreaFieldRenderer):
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):
value = self.raw_value value = self.raw_value
@ -66,7 +63,7 @@ class CodeTextAreaFieldRenderer(formalchemy.TextAreaFieldRenderer):
return super(CodeTextAreaFieldRenderer, self).render(**kwargs) return super(CodeTextAreaFieldRenderer, self).render(**kwargs)
class AutocompleteFieldRenderer(FieldRenderer): class AutocompleteFieldRenderer(fa.FieldRenderer):
""" """
Custom renderer for an autocomplete field. Custom renderer for an autocomplete field.
""" """
@ -109,7 +106,7 @@ class AutocompleteFieldRenderer(FieldRenderer):
return unicode(value) return unicode(value)
class DateTimeFieldRenderer(formalchemy.DateTimeFieldRenderer): class DateTimeFieldRenderer(fa.DateTimeFieldRenderer):
""" """
This renderer assumes the datetime field value is in UTC, and will convert This renderer assumes the datetime field value is in UTC, and will convert
it to the local time zone before rendering it in the standard "raw" format. it to the local time zone before rendering it in the standard "raw" format.
@ -122,7 +119,7 @@ class DateTimeFieldRenderer(formalchemy.DateTimeFieldRenderer):
return raw_datetime(self.request.rattail_config, value) return raw_datetime(self.request.rattail_config, value)
class DateTimePrettyFieldRenderer(formalchemy.DateTimeFieldRenderer): class DateTimePrettyFieldRenderer(fa.DateTimeFieldRenderer):
""" """
Custom date/time field renderer, which displays a "pretty" value in Custom date/time field renderer, which displays a "pretty" value in
read-only mode, leveraging config to show the correct timezone. read-only mode, leveraging config to show the correct timezone.
@ -135,7 +132,7 @@ class DateTimePrettyFieldRenderer(formalchemy.DateTimeFieldRenderer):
return pretty_datetime(self.request.rattail_config, value) return pretty_datetime(self.request.rattail_config, value)
class TimeFieldRenderer(formalchemy.TimeFieldRenderer): class TimeFieldRenderer(fa.TimeFieldRenderer):
""" """
Custom renderer for time fields. In edit mode, renders a simple text Custom renderer for time fields. In edit mode, renders a simple text
input, which is expected to become a 'timepicker' widget in the UI. input, which is expected to become a 'timepicker' widget in the UI.
@ -145,7 +142,7 @@ class TimeFieldRenderer(formalchemy.TimeFieldRenderer):
def render(self, **kwargs): def render(self, **kwargs):
kwargs.setdefault('class_', 'timepicker') kwargs.setdefault('class_', 'timepicker')
return helpers.text_field(self.name, value=self.value, **kwargs) return fa_helpers.text_field(self.name, value=self.value, **kwargs)
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):
return self.render_value(self.raw_value) return self.render_value(self.raw_value)
@ -159,7 +156,7 @@ class TimeFieldRenderer(formalchemy.TimeFieldRenderer):
def convert_value(self, value): def convert_value(self, value):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
if not value.tzinfo: if not value.tzinfo:
value = pytz.utc.localize(value) value = make_utc(value, tzinfo=True)
return localtime(self.request.rattail_config, value).time() return localtime(self.request.rattail_config, value).time()
return value return value
@ -180,7 +177,7 @@ class TimeFieldRenderer(formalchemy.TimeFieldRenderer):
pass pass
class EnumFieldRenderer(SelectFieldRenderer): class EnumFieldRenderer(fa_fields.SelectFieldRenderer):
""" """
Renderer for simple enumeration fields. Renderer for simple enumeration fields.
""" """
@ -211,10 +208,10 @@ class EnumFieldRenderer(SelectFieldRenderer):
opts = [(self.enumeration[x], x) for x in self.enumeration] opts = [(self.enumeration[x], x) for x in self.enumeration]
if not self.field.is_required(): if not self.field.is_required():
opts.insert(0, self.field._null_option) opts.insert(0, self.field._null_option)
return SelectFieldRenderer.render(self, opts, **kwargs) return fa_fields.SelectFieldRenderer.render(self, opts, **kwargs)
class DecimalFieldRenderer(formalchemy.FieldRenderer): class DecimalFieldRenderer(fa.FieldRenderer):
""" """
Sort of generic field renderer for decimal values. You must provide the 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 number of places after the decimal (scale). Note that this in turn relies
@ -237,7 +234,7 @@ class DecimalFieldRenderer(formalchemy.FieldRenderer):
return fmt.format(value) return fmt.format(value)
class CurrencyFieldRenderer(formalchemy.FieldRenderer): class CurrencyFieldRenderer(fa_fields.FloatFieldRenderer):
""" """
Sort of generic field renderer for currency values. Sort of generic field renderer for currency values.
""" """
@ -251,7 +248,7 @@ class CurrencyFieldRenderer(formalchemy.FieldRenderer):
return "${:0,.2f}".format(value) return "${:0,.2f}".format(value)
class YesNoFieldRenderer(CheckBoxFieldRenderer): class YesNoFieldRenderer(fa.CheckBoxFieldRenderer):
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):
value = self.raw_value value = self.raw_value