Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #146 from jrief/localize-decimal-fields

Localize decimal fields
  • Loading branch information...
commit fb94499579acf2fbf4b8f7f935b05a51ccc7319c 2 parents e6532f7 + d316976
@chrisglass authored
Showing with 38 additions and 5 deletions.
  1. +8 −0 CHANGELOG.rst
  2. +25 −0 shop/admin/mixins.py
  3. +5 −5 shop/admin/orderadmin.py
View
8 CHANGELOG.rst
@@ -1,3 +1,11 @@
+localize-decimal-fields
+=======================
+
+* Added a mixin class which helps to localize model fields of type DecimalField
+ in Django admin view.
+* Added this newly created mixin class to OrderAdmin, so that all price fields
+ are handled with the correct localization.
+
Version 0.0.14
==============
View
25 shop/admin/mixins.py
@@ -0,0 +1,25 @@
+#-*- coding: utf-8 -*-
+from django import forms
+
+
+class LocalizeDecimalFieldsForm(forms.ModelForm):
+ def __new__(cls, *args, **kwargs):
+ new_class = super(LocalizeDecimalFieldsForm, cls).__new__(cls)
+ if hasattr(new_class, 'base_fields'):
+ for field in new_class.base_fields.values():
+ if isinstance(field, forms.DecimalField):
+ field.localize = True
+ field.widget.is_localized = True
+ return new_class
+
+
+class LocalizeDecimalFieldsMixin(object):
+ '''
+ To be used as a mixin for classes derived from admin.ModelAdmin,
+ admin.TabularInline, etc. which localizes the input fields for models
+ of type DecimalField in the admin interface.
+ If your class derived from ModelAdmin wants to override the form attribute,
+ make sure that this form is derived from LocalizeDecimalFieldsForm and not
+ from forms.ModelForm.
+ '''
+ form = LocalizeDecimalFieldsForm
View
10 shop/admin/orderadmin.py
@@ -3,7 +3,7 @@
from django.contrib.admin.options import ModelAdmin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
-
+from shop.admin.mixins import LocalizeDecimalFieldsMixin
from shop.models.ordermodel import (Order, OrderItem,
OrderExtraInfo, ExtraOrderPriceField, OrderPayment)
@@ -13,24 +13,24 @@ class OrderExtraInfoInline(admin.TabularInline):
extra = 0
-class OrderPaymentInline(admin.TabularInline):
+class OrderPaymentInline(LocalizeDecimalFieldsMixin, admin.TabularInline):
model = OrderPayment
extra = 0
-class ExtraOrderPriceFieldInline(admin.TabularInline):
+class ExtraOrderPriceFieldInline(LocalizeDecimalFieldsMixin, admin.TabularInline):
model = ExtraOrderPriceField
extra = 0
-class OrderItemInline(admin.TabularInline):
+class OrderItemInline(LocalizeDecimalFieldsMixin, admin.TabularInline):
model = OrderItem
extra = 0
#TODO: add ExtraOrderItemPriceField inline, ideas?
-class OrderAdmin(ModelAdmin):
+class OrderAdmin(LocalizeDecimalFieldsMixin, ModelAdmin):
list_display = ('id', 'user', 'status', 'order_total', 'created')
list_filter = ('status', 'user')
search_fields = ('id', 'shipping_address_text', 'user__username')
Please sign in to comment.
Something went wrong with that request. Please try again.