Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multicurrency convert function #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions INSTALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Or if you're using ``pip``::

pip install django-currencies

For install latest Git version use this command:

pip install -e git+git://github.com/Adys/django-currencies.git#egg=django-currencies

Or if you'd prefer you can simply place the included ``currencies``
directory somewhere on your Python path, or symlink to it from
somewhere on your Python path; this is useful if you're working from a
Expand Down
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,50 @@ Introduction

django-currencies allows you to define different currencies, and includes
template tags/filters to allow easy conversion between them.
With In-app cache with Guacamole https://github.com/stucchio/Guacamole

Usage

Once you have everything set up (read the included INSTALL.txt and
docs/), you will be able to use the following code in your templates::

{% change_currency [price] [currency_code] %}
{% change_currency [price] [currency_code] %}

# i.e:

{% change_currency product.price "USD" %}
{% change_currency product.price "USD" %}

# or if we have the currencies.context_processors.currencies
# available:

{% change_currency product.price CURRENCY.code %}
{% change_currency product.price CURRENCY.code %}

or use the filter::

{{ [price]|currency:[currency] }}
{{ [price]|currency:[currency] }}

# i.e.:

{{ product.price|currency:"USD" }}
{{ product.price|currency:"USD" }}

or set the CURRENCY context variable with a POST to the included view::

{% url currencies_set_currency [currency] %}


Helpers functions
-----------------

Convert multicurrency function

Example of use:

from currencies.utils import convert
price=convert(100, "RUB", "UAH")

Convert 100 Russian Rubles to Ukrainian Hryvnia (via base USD currency)


OpenExchangeRates integration
-----------------------------

Expand Down
3 changes: 2 additions & 1 deletion currencies/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from guacamole import InMemoryCachingManager

class Currency(models.Model):
code = models.CharField(_('code'), max_length=3)
Expand All @@ -14,6 +14,7 @@ class Currency(models.Model):
help_text=_('Make this the base currency against which rates are calculated.'))
is_default = models.BooleanField(_('default'), default=False,
help_text=_('Make this the default user currency.'))
objects = InMemoryCachingManager(lookup_fields=['code'])

class Meta:
ordering = ('name', )
Expand Down
9 changes: 7 additions & 2 deletions currencies/templatetags/currency.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import template
from django.template.defaultfilters import stringfilter
from currencies.models import Currency
from currencies.utils import calculate_price
from currencies.utils import calculate_price, convert

register = template.Library()

Expand All @@ -25,7 +25,6 @@ def render(self, context):
except template.VariableDoesNotExist:
return ''


@register.tag(name='change_currency')
def change_currency(parser, token):
try:
Expand All @@ -34,3 +33,9 @@ def change_currency(parser, token):
tag_name = token.contents.split()[0]
raise template.TemplateSyntaxError('%r tag requires exactly two arguments' % (tag_name))
return ChangeCurrencyNode(current_price, new_currency)


@register.simple_tag(name='currency_convert')
def my_tag(amount, from_, to_, *args, **kwargs):
return convert(amount, from_, to_)

15 changes: 15 additions & 0 deletions currencies/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ def calculate_price(price, currency):
price = price * currency.factor

return price.quantize(Decimal("0.01"), rounding=ROUND_UP)


def convert(amount, from_, to):
if from_==to:
return amount
if amount != None:
amount = Decimal(amount)
else:
return None
amount = Decimal(amount)
from_currency = Currency.objects.get(code__exact=from_)
to_currency = Currency.objects.get(code__exact=to)
amount = amount * (to_currency.factor / from_currency.factor)

return amount.quantize(Decimal("0.01"), rounding=ROUND_UP)