Add docs
This commit is contained in:
@@ -46,6 +46,10 @@ CurrencyType
|
|||||||
|
|
||||||
.. autoclass:: CurrencyType
|
.. autoclass:: CurrencyType
|
||||||
|
|
||||||
|
.. module:: sqlalchemy_utils.primitives.currency
|
||||||
|
|
||||||
|
.. autoclass:: Currency
|
||||||
|
|
||||||
|
|
||||||
EncryptedType
|
EncryptedType
|
||||||
^^^^^^^^^^^^^
|
^^^^^^^^^^^^^
|
||||||
|
@@ -8,6 +8,51 @@ from sqlalchemy_utils.utils import str_coercible
|
|||||||
|
|
||||||
@str_coercible
|
@str_coercible
|
||||||
class Currency(object):
|
class Currency(object):
|
||||||
|
"""
|
||||||
|
Currency class wraps a 3-letter currency code. It provides various
|
||||||
|
convenience properties and methods.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
from babel import Locale
|
||||||
|
from sqlalchemy_utils import Currency, i18n
|
||||||
|
|
||||||
|
|
||||||
|
# First lets add a locale getter for testing purposes
|
||||||
|
i18n.get_locale = lambda: Locale('en')
|
||||||
|
|
||||||
|
|
||||||
|
Currency('USD').name # US Dollar
|
||||||
|
Currency('USD').symbol # $
|
||||||
|
|
||||||
|
Currency(Currency('USD')).code # 'USD'
|
||||||
|
|
||||||
|
Currency always validates the given code.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
Currency(None) # raises TypeError
|
||||||
|
|
||||||
|
Currency('UnknownCode') # raises ValueError
|
||||||
|
|
||||||
|
|
||||||
|
Currency supports equality operators.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
Currency('USD') == Currency('USD')
|
||||||
|
Currency('USD') != Currency('EUR')
|
||||||
|
|
||||||
|
|
||||||
|
Currencies are hashable.
|
||||||
|
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
len(set([Currency('USD'), Currency('USD')])) # 1
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
def __init__(self, code):
|
def __init__(self, code):
|
||||||
if isinstance(code, Currency):
|
if isinstance(code, Currency):
|
||||||
self.code = code
|
self.code = code
|
||||||
@@ -54,4 +99,4 @@ class Currency(object):
|
|||||||
return '%s(%r)' % (self.__class__.__name__, self.code)
|
return '%s(%r)' % (self.__class__.__name__, self.code)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.code
|
||||||
|
@@ -8,8 +8,8 @@ from .scalar_coercible import ScalarCoercible
|
|||||||
|
|
||||||
class CurrencyType(types.TypeDecorator, ScalarCoercible):
|
class CurrencyType(types.TypeDecorator, ScalarCoercible):
|
||||||
"""
|
"""
|
||||||
Changes Currency objects to a string representation on the way in and
|
Changes :class:`.Currency` objects to a string representation on the way in
|
||||||
changes them back to Currency objects on the way out.
|
and changes them back to :class:`.Currency` objects on the way out.
|
||||||
|
|
||||||
In order to use CurrencyType you need to install Babel_ first.
|
In order to use CurrencyType you need to install Babel_ first.
|
||||||
|
|
||||||
|
@@ -55,11 +55,11 @@ class TestCurrency(object):
|
|||||||
|
|
||||||
def test_unicode(self):
|
def test_unicode(self):
|
||||||
currency = Currency('USD')
|
currency = Currency('USD')
|
||||||
assert six.text_type(currency) == u'US Dollar'
|
assert six.text_type(currency) == u'USD'
|
||||||
|
|
||||||
def test_str(self):
|
def test_str(self):
|
||||||
currency = Currency('USD')
|
currency = Currency('USD')
|
||||||
assert str(currency) == 'US Dollar'
|
assert str(currency) == 'USD'
|
||||||
|
|
||||||
def test_representation(self):
|
def test_representation(self):
|
||||||
currency = Currency('USD')
|
currency = Currency('USD')
|
||||||
|
Reference in New Issue
Block a user