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

Support for specifying currency symbol and frac_digits directly #193

Open
wants to merge 7 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
8 changes: 3 additions & 5 deletions cartridge/shop/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from __future__ import unicode_literals
from future.builtins import super

from locale import localeconv

from django.db.models import CharField, DecimalField
from django.utils.translation import ugettext_lazy as _

from cartridge.shop.utils import set_locale
from cartridge.shop.utils import get_frac_digits


class OptionField(CharField):
Expand Down Expand Up @@ -43,9 +41,9 @@ class MoneyField(DecimalField):
precision.
"""
def __init__(self, *args, **kwargs):
set_locale()
frac_digits = get_frac_digits()
defaults = {"null": True, "blank": True, "max_digits": 10,
"decimal_places": localeconv()["frac_digits"]}
"decimal_places": frac_digits}
defaults.update(kwargs)
super(MoneyField, self).__init__(*args, **defaults)

Expand Down
7 changes: 3 additions & 4 deletions cartridge/shop/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from copy import copy
from datetime import date
from itertools import dropwhile, takewhile
from locale import localeconv
from re import match

from django import forms
Expand All @@ -23,7 +22,7 @@
from cartridge.shop import checkout
from cartridge.shop.models import Product, ProductOption, ProductVariation
from cartridge.shop.models import Cart, CartItem, Order, DiscountCode
from cartridge.shop.utils import (make_choices, set_locale, set_shipping,
from cartridge.shop.utils import (make_choices, get_frac_digits, set_shipping,
clear_session)


Expand Down Expand Up @@ -451,8 +450,8 @@ def render(self, name, value, attrs):
except (TypeError, ValueError):
pass
else:
set_locale()
value = ("%%.%sf" % localeconv()["frac_digits"]) % value
frac_digits = get_frac_digits()
value = ("%%.%sf" % frac_digits) % value
attrs["style"] = "text-align:right;"
return super(MoneyWidget, self).render(name, value, attrs)

Expand Down
42 changes: 39 additions & 3 deletions cartridge/shop/templatetags/shop_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from __future__ import unicode_literals
from future.builtins import str
from future.builtins import str, chr

from decimal import Decimal
import locale
Expand All @@ -10,6 +10,7 @@

from cartridge.shop.utils import set_locale

from mezzanine.conf import settings

register = template.Library()

Expand All @@ -19,10 +20,45 @@ def currency(value):
"""
Format a value as currency according to locale.
"""
set_locale()
try:
set_locale()
except:
pass
if not value:
value = 0
if hasattr(locale, "currency"):
# if SHOP_CURRENCY_SYMBOL and SHOP_CURRENCY_FRAC_DIGITS are defined,
# they take precedence.
# SHOP_CURRENCY_SYMBOL can be an ASCII string like
# SHOP_CURRENCY_SYMBOL = "$",
# or it can be a number that represents a currency, like
# SHOP_CURRENCY_SYMBOL = 163
# where 163 is the ASCII code for the British Pound currency symbol.
if hasattr(settings, "SHOP_CURRENCY_SYMBOL") and \
hasattr(settings, "SHOP_CURRENCY_FRAC_DIGITS"):
frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS
try:
currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL)
except:
currency_symbol = settings.SHOP_CURRENCY_SYMBOL
if hasattr(settings, "SHOP_CURRENCY_SEP_BY_SPACE"):
p_sep_by_space = settings.SHOP_CURRENCY_SEP_BY_SPACE
else:
p_sep_by_space = False
if hasattr(settings, "SHOP_CURRENCY_MON_DECIMAL_POINT"):
mon_decimal_point = settings.SHOP_CURRENCY_MON_DECIMAL_POINT
else:
mon_decimal_point = "."
if hasattr(settings, "SHOP_CURRENCY_P_CS_PRECEDES"):
p_cs_precedes = settings.SHOP_CURRENCY_P_CS_PRECEDES
else:
p_cs_precedes = True

value = [currency_symbol, p_sep_by_space and " " or "",
(("%%.%sf" % frac_digits) % value).replace(".", mon_decimal_point)]
if not p_cs_precedes:
value.reverse()
value = "".join(value)
elif hasattr(locale, "currency"):
value = locale.currency(Decimal(value), grouping=True)
if platform.system() == 'Windows':
try:
Expand Down
11 changes: 10 additions & 1 deletion cartridge/shop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from future.builtins import bytes, zip, str as _str

import hmac
from locale import setlocale, LC_MONETARY
from locale import setlocale, LC_MONETARY, localeconv
try:
from hashlib import sha512 as digest
except ImportError:
Expand Down Expand Up @@ -144,3 +144,12 @@ def set_locale():
"configure the SHOP_CURRENCY_LOCALE setting in your settings "
"module.")
raise ImproperlyConfigured(msg % currency_locale)


def get_frac_digits():
if hasattr(settings, "SHOP_CURRENCY_FRAC_DIGITS"):
frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS
else:
set_locale()
frac_digits = localeconv()["frac_digits"]
return frac_digits