From 3ff7cd296ac6074b605825688d16c31d7939d5fe Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 13:45:09 +0800 Subject: [PATCH 1/7] On OpenBSD systems, LC_MONETARY cannot be set to anything other than "", "C" or "POSIX". As such, I've added a way to specify the currency and frac_digits using: SHOP_CURRENCY_FRAC_DIGITS SHOP_CURRENCY_SYMBOL --- cartridge/shop/fields.py | 8 ++++-- cartridge/shop/forms.py | 6 ++-- cartridge/shop/templatetags/shop_tags.py | 36 ++++++++++++++++++++++-- cartridge/shop/utils.py | 10 +++++++ 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/cartridge/shop/fields.py b/cartridge/shop/fields.py index b13ea8b0d..113e65aa6 100644 --- a/cartridge/shop/fields.py +++ b/cartridge/shop/fields.py @@ -11,7 +11,9 @@ from django.db.models import CharField, DecimalField from django.utils.translation import ugettext_lazy as _ -from cartridge.shop.utils import set_locale +from mezzanine.conf import settings + +from cartridge.shop.utils import get_frac_digits class OptionField(CharField): @@ -43,9 +45,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) diff --git a/cartridge/shop/forms.py b/cartridge/shop/forms.py index 0256271ea..2566af5ce 100644 --- a/cartridge/shop/forms.py +++ b/cartridge/shop/forms.py @@ -23,7 +23,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) @@ -451,8 +451,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) diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index 39e03f869..9f5864d84 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -10,6 +10,7 @@ from cartridge.shop.utils import set_locale +from mezzanine.conf import settings register = template.Library() @@ -19,10 +20,41 @@ 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 = unichr(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: diff --git a/cartridge/shop/utils.py b/cartridge/shop/utils.py index e2d1dee56..95c8dbee5 100644 --- a/cartridge/shop/utils.py +++ b/cartridge/shop/utils.py @@ -144,3 +144,13 @@ 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 + From bbb71ec6f9d545c23364560a8991bff6115e7c8a Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 16:55:41 +0800 Subject: [PATCH 2/7] Forgot to import localeconv --- cartridge/shop/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cartridge/shop/utils.py b/cartridge/shop/utils.py index 95c8dbee5..a0ea08cfb 100644 --- a/cartridge/shop/utils.py +++ b/cartridge/shop/utils.py @@ -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: From dd101e758cf4bae85f716b8fccfda2158b58c16f Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 17:13:20 +0800 Subject: [PATCH 3/7] Removed unneeded import lines and reformatted long lines. --- cartridge/shop/fields.py | 4 ---- cartridge/shop/forms.py | 1 - cartridge/shop/templatetags/shop_tags.py | 12 ++++++++---- cartridge/shop/utils.py | 1 - 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cartridge/shop/fields.py b/cartridge/shop/fields.py index 113e65aa6..35521dd23 100644 --- a/cartridge/shop/fields.py +++ b/cartridge/shop/fields.py @@ -6,13 +6,9 @@ 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 mezzanine.conf import settings - from cartridge.shop.utils import get_frac_digits diff --git a/cartridge/shop/forms.py b/cartridge/shop/forms.py index 2566af5ce..f94b75d56 100644 --- a/cartridge/shop/forms.py +++ b/cartridge/shop/forms.py @@ -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 diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index 9f5864d84..effa4d3d9 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -26,11 +26,15 @@ def currency(value): pass if not value: value = 0 - # 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 + # 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"): + if hasattr(settings, "SHOP_CURRENCY_SYMBOL") and \ + hasattr(settings, "SHOP_CURRENCY_FRAC_DIGITS"): frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS try: currency_symbol = unichr(settings.SHOP_CURRENCY_SYMBOL) diff --git a/cartridge/shop/utils.py b/cartridge/shop/utils.py index a0ea08cfb..f629009c4 100644 --- a/cartridge/shop/utils.py +++ b/cartridge/shop/utils.py @@ -153,4 +153,3 @@ def get_frac_digits(): set_locale() frac_digits = localeconv()["frac_digits"] return frac_digits - From e9ccb94db8a03150f9899394a4743bc7356a1d82 Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 17:28:43 +0800 Subject: [PATCH 4/7] Python 3 doesn't have unichr(), so added chr(). --- cartridge/shop/templatetags/shop_tags.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index effa4d3d9..95b1b05a3 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -38,6 +38,8 @@ def currency(value): frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS try: currency_symbol = unichr(settings.SHOP_CURRENCY_SYMBOL) + except NameError: + currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL) except: currency_symbol = settings.SHOP_CURRENCY_SYMBOL if hasattr(settings, "SHOP_CURRENCY_SEP_BY_SPACE"): From 1866d890766779dc748e06485eff714991a63c4b Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 17:45:46 +0800 Subject: [PATCH 5/7] Version check for python. --- cartridge/shop/templatetags/shop_tags.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index 95b1b05a3..d5e3d72f1 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -5,6 +5,7 @@ from decimal import Decimal import locale import platform +import sys from django import template @@ -37,9 +38,10 @@ def currency(value): hasattr(settings, "SHOP_CURRENCY_FRAC_DIGITS"): frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS try: - currency_symbol = unichr(settings.SHOP_CURRENCY_SYMBOL) - except NameError: - currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL) + if sys.version_info[0] < 3: + currency_symbol = unichr(settings.SHOP_CURRENCY_SYMBOL) + else: + currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL) except: currency_symbol = settings.SHOP_CURRENCY_SYMBOL if hasattr(settings, "SHOP_CURRENCY_SEP_BY_SPACE"): From 91c2278d66475b0fe97422ae67b667f29b1f6339 Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 18:08:18 +0800 Subject: [PATCH 6/7] Used the chr() from futures instead. --- cartridge/shop/templatetags/shop_tags.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index d5e3d72f1..9de72fd61 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -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 @@ -38,10 +38,7 @@ def currency(value): hasattr(settings, "SHOP_CURRENCY_FRAC_DIGITS"): frac_digits = settings.SHOP_CURRENCY_FRAC_DIGITS try: - if sys.version_info[0] < 3: - currency_symbol = unichr(settings.SHOP_CURRENCY_SYMBOL) - else: - currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL) + currency_symbol = chr(settings.SHOP_CURRENCY_SYMBOL) except: currency_symbol = settings.SHOP_CURRENCY_SYMBOL if hasattr(settings, "SHOP_CURRENCY_SEP_BY_SPACE"): From 8715fb164749a075e9046b9e92cd8a0fa3cd3607 Mon Sep 17 00:00:00 2001 From: Eric Boo Date: Tue, 8 Jul 2014 18:22:00 +0800 Subject: [PATCH 7/7] Removed unused import sys --- cartridge/shop/templatetags/shop_tags.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cartridge/shop/templatetags/shop_tags.py b/cartridge/shop/templatetags/shop_tags.py index 9de72fd61..018d234a9 100644 --- a/cartridge/shop/templatetags/shop_tags.py +++ b/cartridge/shop/templatetags/shop_tags.py @@ -5,7 +5,6 @@ from decimal import Decimal import locale import platform -import sys from django import template