X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Fmodel%2Ftypes.py;h=59f3d35bf8d150e0cc368e939db1058f46b0378a;hb=fed6d11d2cbd6d617d18bc01a78196865da4155b;hp=30c2ce31e93127e5ea88ad3444819e6a0cad2e32;hpb=e9671e5adfe5f1c5366c34cd9df98f77b115f7d8;p=bluechips.git diff --git a/bluechips/model/types.py b/bluechips/model/types.py index 30c2ce3..59f3d35 100644 --- a/bluechips/model/types.py +++ b/bluechips/model/types.py @@ -2,11 +2,63 @@ Define special types used in BlueChips """ +import locale +from decimal import Decimal, InvalidOperation + import sqlalchemy as sa +from formencode import validators, Invalid from bluechips.lib.subclass import SmartSubclass from weakref import WeakValueDictionary +def localeconv(): + "Manually install en_US for systems that don't have it." + d = {'currency_symbol': '$', + 'decimal_point': '.', + 'frac_digits': 2, + 'grouping': [3, 3, 0], + 'int_curr_symbol': 'USD ', + 'int_frac_digits': 2, + 'mon_decimal_point': '.', + 'mon_grouping': [3, 3, 0], + 'mon_thousands_sep': ',', + 'n_cs_precedes': 1, + 'n_sep_by_space': 0, + 'n_sign_posn': 1, + 'negative_sign': '-', + 'p_cs_precedes': 1, + 'p_sep_by_space': 0, + 'p_sign_posn': 1, + 'positive_sign': '', + 'thousands_sep': ','} + return d +locale.localeconv = localeconv + + +class CurrencyValidator(validators.FancyValidator): + "A validator to convert to Currency objects." + messages = {'amount': "Please enter a valid currency amount", + 'precision': "Only two digits after the decimal, please", + 'nonzero': "Please enter a non-zero amount"} + + def _to_python(self, value, state): + try: + dec = Decimal(value) + except InvalidOperation: + raise Invalid(self.message('amount', state), + value, state) + else: + ret = dec.quantize(Decimal('1.00')) + if ret == 0: + raise Invalid(self.message('nonzero', state), + value, state) + elif ret != dec: + raise Invalid(self.message('precision', state), + value, state) + else: + return Currency(int(ret * 100)) + + class Currency(object): """ Store currency values as an integral number of cents @@ -14,7 +66,9 @@ class Currency(object): __metaclass__ = SmartSubclass(int) __old_values__ = WeakValueDictionary() def __new__(cls, value): - if isinstance(value, str): + if value is None: + value = 0 + elif isinstance(value, str): value = int(float(value) * 100) else: value = int(value) @@ -68,20 +122,24 @@ class Currency(object): argument to an int """ return self.__mul__(other) - - def __str_no_dollar__(self): + def __div__(self, other): """ - Get to the formatted string without the dollar sign + If I don't define this, SmartSubclass will convert the other + argument to an int """ - return str(self)[1:] + return Currency(self.value / other) + def __truediv__(self, other): + """ + If I don't define this, SmartSubclass will convert the other + argument to an int + """ + return Currency(self.value / other) def __repr__(self): return '%s("%s")' % (self.__class__.__name__, str(self)) def __str__(self): - sign = '-' if self.value < 0 else '' - cents = abs(self.value) % 100 - dollars = (abs(self.value) - cents) / 100 - return '$%s%s.%.02d' % (sign, dollars, cents) + return locale.currency(self.value / 100., grouping=True) + class DBCurrency(sa.types.TypeDecorator): """ @@ -97,3 +155,4 @@ class DBCurrency(sa.types.TypeDecorator): def convert_result_value(self, value, engine): return Currency(value) + process_result_value = convert_result_value