From 5deb198846512073eb25b053b687070ce18b0fd7 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 26 Jul 2012 14:03:20 -0700 Subject: [PATCH] add price field renderers --- rattail/pyramid/forms.py | 52 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/rattail/pyramid/forms.py b/rattail/pyramid/forms.py index 0272815a..7f9f4bae 100644 --- a/rattail/pyramid/forms.py +++ b/rattail/pyramid/forms.py @@ -34,7 +34,7 @@ from edbob.pyramid import Session import rattail -__all__ = ['UpcFieldRenderer'] +__all__ = ['PriceFieldRenderer', 'RegularPriceFieldRenderer', 'UpcFieldRenderer'] class BatchIdFieldRenderer(formalchemy.FieldRenderer): @@ -58,6 +58,56 @@ class BatchIdFieldRenderer(formalchemy.FieldRenderer): # def render(self, options, **kwargs): +class PriceFieldRenderer(formalchemy.fields.FloatFieldRenderer): + """ + Generic renderer for price (currency) fields. + """ + + def render_readonly(self, **kwargs): + value = self.raw_value + if value is None: + return '' + return '$ %0.2f' % value + + +class RegularPriceFieldRenderer(PriceFieldRenderer): + """ + Custom renderer for the :attr:`rattail.Product.regular_price` field. + """ + + def render_readonly(self, **kwargs): + prod = self.field.model + + if prod.regular_price and prod.package_price: + return '$ %0.2f  ($ %0.2f / %u)' % ( + prod.regular_price, prod.package_price, prod.package_price_quantity) + + elif prod.regular_price: + return '$ %0.2f' % prod.regular_price + + elif prod.package_price: + return '$ %0.2f / %u' % ( + prod.package_price, prod.package_price_quantity) + + return '' + + +class SalePriceFieldRenderer(PriceFieldRenderer): + """ + Custom renderer for the :attr:`rattail.Product.sale_price` field. + """ + + def render_readonly(self, **kwargs): + prod = self.field.model + + if prod.sale_price: + if prod.sale_price_quantity and prod.sale_price_quantity != 1: + return '$ %0.2f / %u' % ( + prod.sale_price, prod.sale_price_quantity) + return '$ %0.2f' % prod.sale_price + + return '' + class UpcFieldRenderer(formalchemy.TextFieldRenderer): """