]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/model/types.py
a81a8ae6ce09db566aaf395d0341c6ef7c6b0055
[bluechips.git] / bluechips / model / types.py
1 """
2 Define special types used in BlueChips
3 """
4
5 import sqlalchemy as sa
6 from bluechips.lib.helpers import round_currency
7 from decimal import Decimal
8
9 class Currency(sa.types.TypeDecorator):
10     """
11     A type which represents monetary amounts internally as integers.
12     
13     This avoids binary/decimal float conversion issues
14     """
15     
16     impl = sa.types.Integer
17     
18     def process_bind_param(self, value, engine):
19         return int(value * 100)
20     
21     def convert_result_value(self, value, engine):
22         return round_currency(Decimal(value) / 100)