Skip to content

Commit

Permalink
Moved setting of locale out of the shop instance into Django settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
diefenbach committed Apr 9, 2012
1 parent d8c9a25 commit bb0c8d7
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 61 deletions.
12 changes: 12 additions & 0 deletions docs/developer/settings.rst
Expand Up @@ -86,6 +86,18 @@ LFS_DOCS
within the management interface. Defaults to
http://docs.getlfs.com/en/latest/.

LFS_LOCALE
Sets the locale for the shop, which is the base for number formatting and
the displayed currency. If you don't set it, the current locale of your
Python is not touched at all. Example::

LFS_LOCALE = "en_US.UTF-8"

You can find more about locale here:

* http://en.wikipedia.org/wiki/Locale
* http://docs.python.org/library/locale.html

LFS_LOG_FILE
Absolute path to LFS' log file.

Expand Down
13 changes: 6 additions & 7 deletions lfs/cart/tests.py
@@ -1,5 +1,8 @@
# coding: utf-8

# python imports
import locale

# django imports
from django.contrib.auth.models import User
from django.contrib.sessions.backends.file import SessionStore
Expand Down Expand Up @@ -432,14 +435,12 @@ def test_totals_1(self):
request.session = self.session
request.user = self.user

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

# Added product_1 to cart
add_to_cart(request)
response = added_to_cart_items(request)

# Check we are using german locale
shop = lfs_get_object_or_404(Shop, pk=1)
self.assertEqual(shop.default_locale, 'en_US.UTF-8')

# need to test for two versions of currency output (Mac and Ubuntu differ)
self.failIf(response.find(u"Total: $10.00") == -1)

Expand All @@ -456,9 +457,7 @@ def test_totals_2(self):
request.session = self.session
request.user = self.user

# Check we are using german locale
shop = lfs_get_object_or_404(Shop, pk=1)
self.assertEqual(shop.default_locale, 'en_US.UTF-8')
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

# Added product_1 two times to cart
add_to_cart(request)
Expand Down
14 changes: 12 additions & 2 deletions lfs/core/management/commands/lfs_migrate.py
Expand Up @@ -35,13 +35,23 @@ def handle(self, *args, **options):
if version == "0.5":
self.migrate_to_06(application, version)
self.migrate_to_07(application, version)
print "Your database has been migrated to version 0.7."
self.migrate_to_08(application, version)
print "Your database has been migrated to version 0.8."
elif version == "0.6":
self.migrate_to_07(application, version)
print "Your database has been migrated to version 0.7."
self.migrate_to_08(application, version)
print "Your database has been migrated to version 0.8."
elif version == "0.7":
self.migrate_to_08(application, version)
print "Your database has been migrated to version 0.8."
elif version == "0.8":
print "You are up-to-date"

def migrate_to_08(self, application, version):
db.delete_column("core_shop", "default_locale")
application.version = "0.8"
application.save()

def migrate_to_07(self, application, version):
from lfs.catalog.models import Product
from lfs.catalog.settings import VARIANT
Expand Down
47 changes: 24 additions & 23 deletions lfs/core/models.py
Expand Up @@ -87,69 +87,72 @@ class Meta:


class Shop(models.Model):
"""Holds all shop related information.
At the moment there must be exactly one shop with id == 1. (This may be
changed in future to provide multi shops.)
Instance variables:
"""
Holds all shop related information.
- name
name
The name of the shop. This is used for the the title of the HTML pages
- shop_owner
shop_owner
The shop owner. This is displayed within several places for instance the
checkout page
- from_email
from_email
This e-mail address is used for the from header of all outgoing e-mails
- notification_emails
notification_emails
This e-mail addresses are used for incoming notification e-mails, e.g.
received an order. One e-mail address per line.
- description
description
A description of the shop
- image
image
An image which can be used as default image if a category doesn't have one
- product_cols, product_rows, category_cols
product_cols, product_rows, category_cols
Upmost format information, which defines how products and categories are
displayed within several views. These may be inherited by categories and
sub categories.
- google_analytics_id
google_analytics_id
Used to generate google analytics tracker code and e-commerce code. the
id has the format UA-xxxxxxx-xx and is provided by Google.
- ga_site_tracking
ga_site_tracking
If selected and the google_analytics_id is given google analytics site
tracking code is inserted into the HTML source code.
- ga_ecommerce_tracking
ga_ecommerce_tracking
If selected and the google_analytics_id is given google analytics
e-commerce tracking code is inserted into the HTML source code.
- countries
countries
Selected countries will be offered to the shop customer tho choose for
shipping and invoice address.
- default_country
default_country
This country will be used to calculate shipping price if the shop
customer doesn't have select a country yet.
- price_calculator
use_international_currency_code
If this is True the international currency code from the current locale
is used.
price_calculator
Class that implements lfs.price.PriceCalculator for calculating product
price. This is the default price calculator for all products.
- checkout_type
checkout_type
Decides whether the customer has to login, has not to login or has the
choice to to login or not to be able to check out.
- confirm_toc
confirm_toc
If this is activated the shop customer has to confirm terms and
conditions to checkout.
meta*
This information is used within HTML meta tags of the shop view.
"""
name = models.CharField(_(u"Name"), max_length=30)
shop_owner = models.CharField(_(u"Shop owner"), max_length=100, blank=True)
Expand All @@ -171,8 +174,6 @@ class Shop(models.Model):
shipping_countries = models.ManyToManyField(Country, verbose_name=_(u"Shipping countries"), related_name="shipping")
default_country = models.ForeignKey(Country, verbose_name=_(u"Default shipping country"))

# You can find locale information here: http://en.wikipedia.org/wiki/Locale
default_locale = models.CharField(_(u"Default Shop Locale"), max_length=20, default="en_US.UTF-8")
use_international_currency_code = models.BooleanField(_(u"Use international currency codes"), default=False)
price_calculator = models.CharField(choices=settings.LFS_PRICE_CALCULATORS, max_length=255,
default=settings.LFS_PRICE_CALCULATORS[0][0],
Expand Down
12 changes: 10 additions & 2 deletions lfs/core/templatetags/lfs_tags.py
Expand Up @@ -14,7 +14,9 @@

# lfs imports
import lfs.catalog.utils
import lfs.core.utils
import lfs.utils.misc
import logging
from lfs.caching.utils import lfs_get_object_or_404
from lfs.catalog.models import Category
from lfs.catalog.settings import CONFIGURABLE_PRODUCT
Expand All @@ -24,11 +26,12 @@
from lfs.catalog.models import Product
from lfs.catalog.models import PropertyOption
from lfs.catalog.settings import PRODUCT_TYPE_LOOKUP
import lfs.core.utils
from lfs.core.models import Action
from lfs.page.models import Page
from lfs.shipping import utils as shipping_utils


logger = logging.getLogger("default")
register = template.Library()


Expand Down Expand Up @@ -472,7 +475,10 @@ def get_slug_from_request(request):
@register.filter
def currency(value, request=None, grouping=True):
"""
Returns the currency based on the given locale within settings.LFS_LOCALE
e.g.
import locale
locale.setlocale(locale.LC_ALL, 'de_CH.UTF-8')
currency(123456.789) # Fr. 123'456.79
Expand All @@ -484,8 +490,9 @@ def currency(value, request=None, grouping=True):
shop = lfs.core.utils.get_default_shop(request)
try:
result = locale.currency(value, grouping=grouping, international=shop.use_international_currency_code)
except ValueError:
except ValueError, e:
result = value
logger.error("currency filter: %s" % e)

# add css class if value is negative
if value < 0:
Expand Down Expand Up @@ -631,6 +638,7 @@ def render(self, context):

return ""


def do_category_product_prices_gross(parser, token):
"""
Calculates needed gross price of a product for the category view.
Expand Down
5 changes: 2 additions & 3 deletions lfs/core/tests.py
Expand Up @@ -70,9 +70,8 @@ def test_shop_defaults(self):
def test_unsupported_locale(self):
"""
"""
shop = lfs.core.utils.get_default_shop()
shop.default_locale = "unsupported"
shop.save()
from django.conf import settings
settings.LFS_LOCALE = "unsupported"

self.client.get("/")

Expand Down
11 changes: 6 additions & 5 deletions lfs/core/views.py
Expand Up @@ -59,8 +59,9 @@ def server_error(request):


def one_time_setup():
shop = lfs_get_object_or_404(Shop, pk=1)
try:
locale.setlocale(locale.LC_ALL, str(shop.default_locale))
except locale.Error, e:
logger.error("Unsupported locale in LMI/Preferences/Default Values/Default Shop Locale: '%s'." % shop.default_locale)
lfs_locale = getattr(settings, "LFS_LOCALE", None)
if lfs_locale:
try:
locale.setlocale(locale.LC_ALL, lfs_locale)
except locale.Error, e:
logger.error("Unsupported locale in settings.LFS_LOCALE: '%s'." % lfs_locale)
3 changes: 1 addition & 2 deletions lfs/integrationtests/fixtures/lfs_price_test.xml
Expand Up @@ -1534,7 +1534,6 @@
<field type="BooleanField" name="ga_site_tracking">False</field>
<field type="BooleanField" name="ga_ecommerce_tracking">False</field>
<field to="core.country" name="default_country" rel="ManyToOneRel">101</field>
<field type="CharField" name="default_locale">de_DE.UTF-8</field>
<field type="PositiveSmallIntegerField" name="checkout_type">0</field>
<field type="BooleanField" name="confirm_toc">False</field>
<field to="core.country" name="invoice_countries" rel="ManyToManyRel">
Expand Down Expand Up @@ -4273,4 +4272,4 @@
<field to="auth.group" name="groups" rel="ManyToManyRel"></field>
<field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
</object>
</django-objects>
</django-objects>
2 changes: 1 addition & 1 deletion lfs/manage/information/views.py
Expand Up @@ -31,7 +31,7 @@ def environment(request, template_name="manage/information/environment.html"):
"""
apps = []
for app in settings.INSTALLED_APPS:
if app in ["lfs", "lfstheme"] or \
if app in ["lfs"] or \
app.startswith("lfs.") or \
app.startswith("django."):
continue
Expand Down
2 changes: 1 addition & 1 deletion lfs/manage/views/shop.py
Expand Up @@ -51,7 +51,7 @@ class ShopDefaultValuesForm(ModelForm):
class Meta:
model = Shop
fields = ("price_calculator", "product_cols", "product_rows", "category_cols",
"default_country", "invoice_countries", "shipping_countries", "default_locale", "use_international_currency_code")
"default_country", "invoice_countries", "shipping_countries", "use_international_currency_code")


@permission_required("core.manage_shop", login_url="/login/")
Expand Down
15 changes: 0 additions & 15 deletions lfs/templates/manage/shop/default_values_tab.html
Expand Up @@ -129,21 +129,6 @@ <h2>

<fieldset>
<legend>{% trans "Localization" %}</legend>
<div class="field">
<div class="label">
{{ form.default_locale.label_tag }}:
</div>
{% if form.default_locale.errors %}
<div class="error">
{{ form.default_locale }}
{{ form.default_locale.errors }}
</div>
{% else %}
<div>
{{ form.default_locale }}
</div>
{% endif %}
</div>
<div class="field">
<div class="label">
{{ form.use_international_currency_code.label_tag }}:
Expand Down

0 comments on commit bb0c8d7

Please sign in to comment.