How to install django-cuddlybuddly-ecb
- Download and install ActivePython
- Open Command Prompt
- Type
pypm install django-cuddlybuddly-ecb
Lastest release
A Django app to convert currencies using the feed from the European Central Bank. Includes manager methods and template tags.
Installation
- Add cuddlybuddly.ecb to your INSTALLED_APPS.
- Run manage.py syncdb
- Create a cron job to run the equivalent of manage.py cb_ecb_update once per day.
- Add {% load ecb_tags %} to the templates where you wish to use the template tags.
Settings
CUDDLYBUDDLY_ECB_BASE
Takes the three letter currency code (ISO 4217) of your base currency, i.e. the currency your are storing things in. It defaults to usd.
The value of this setting is used as the default currency to convert from when using the convert manager method or the ecb template tag. In these circumstances using this setting will save you some typing.
CUDDLYBUDDLY_ECB_NON2DP
Some currencies do not use two decimal places. The most obvious being the Japanese Yen with zero, but Wikipedia lists some that even use one or three decimal places.
When converting 1.00 USD to JPY you do not want it to come out as 98.00, but as 98. This setting solves that.
By default this setting comes setup for the Japanese Yen (JPY) and Chinese Renminbi (CNY). I was too lazy to find out exactly what needs to be done for every currency so if you need more just add them yourself.
The default:
{ 0: ('jpy', 'cny') }
This means to convert JPY and CNY to 0 decimal places.
While useless, you might set the following to also convert USD to two decimal places:
{ 0: ('jpy', 'cny'), 2: ('usd',) }
Manager Methods
The ExchangeRates model has the following manager methods available:
convert(value, to_currency, from_currency=CUDDLYBUDDLY_ECB_BASE)
Converts value to to_currency from from_currency.
If value cannot be successfully converted to a decimal.Decimal object or either to_currency or from_currency are invalid codes (i.e. they don't exist or the ECB feed doesn't support them) then 0.00 will be returned. Remember to take this into account if your are using this app for anything other than representational purposes. A user on a shopping site will be able to tell that 0.00 is most likely a mistake, but can your code?
get_updated_on()
Returns a datetime object of when the ECB feed was last accessed.
Template Tags
ecb
This is basically a wrapper to access the convert() method on the ExchangeRates manager.
Usage:
{% ecb value to_currency %} {% ecb value to_currency as var %} {% ecb value to_currency from_currency %} {% ecb value to_currency from_currency as var %}
If value cannot be successfully converted to a decimal.Decimal object or either to_currency or from_currency are invalid codes (i.e. they don't exist or the ECB feed doesn't support them) then 0.00 will be returned.
If CUDDLYBUDDLY_ECB_BASE is set to usd, then the following would output 0.61 (at the time of writing):
{% ecb 1.00 "gbp" %}
However, we can override the base currency and the following would output 0.62:
{% ecb 100 "gbp" "jpy" %}
ecb_last_update
This is basically a wrapper to accesss the get_updated_on method on the ExchangeRates manager.
Usage:
{% ecb_last_update %} {% ecb_last_update as var %}
You can supply it to the default Django date filter if you wish to format it differently.