-
Notifications
You must be signed in to change notification settings - Fork 81
/
utils.py
55 lines (41 loc) · 1.58 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
from decimal import Decimal as D, InvalidOperation, ROUND_UP
from .models import Currency as C
from .conf import SESSION_KEY
def get_active_currencies_qs():
return C.active.defer('info').all()
def calculate(price, to_code, **kwargs):
"""Converts a price in the default currency to another currency"""
qs = kwargs.get('qs', get_active_currencies_qs())
kwargs['qs'] = qs
default_code = qs.default().code
return convert(price, default_code, to_code, **kwargs)
def convert(amount, from_code, to_code, decimals=2, qs=None):
"""Converts from any currency to any currency"""
if from_code == to_code:
return amount
if qs is None:
qs = get_active_currencies_qs()
from_, to = qs.get(code=from_code), qs.get(code=to_code)
amount = D(amount) * (to.factor / from_.factor)
return price_rounding(amount, decimals=decimals)
def get_currency_code(request):
for attr in ('session', 'COOKIES'):
if hasattr(request, attr):
try:
return getattr(request, attr)[SESSION_KEY]
except KeyError:
continue
# fallback to default...
try:
return C.active.default().code
except C.DoesNotExist:
return None # shit happens...
def price_rounding(price, decimals=2):
"""Takes a decimal price and rounds to a number of decimal places"""
try:
exponent = D('.' + decimals * '0')
except InvalidOperation:
# Currencies with no decimal places, ex. JPY, HUF
exponent = D()
return price.quantize(exponent, rounding=ROUND_UP)