Skip to content

Commit

Permalink
better readability in admin classes
Browse files Browse the repository at this point in the history
use lists instead of tuples
  • Loading branch information
jrief committed Jul 23, 2018
1 parent 67f700b commit aa1b6e4
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 81 deletions.
34 changes: 17 additions & 17 deletions example/myshop/admin/i18n_polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,40 @@ class CommodityAdmin(SortableAdminMixin, TranslatableAdmin, FrontendEditableAdmi
Since our Commodity model inherits from polymorphic Product, we have to redefine its admin class.
"""
base_model = Product
fieldsets = (
fieldsets = [
(None, {
'fields': ('product_name', 'slug', 'product_code', 'unit_price', 'active',),
'fields': ['product_name', 'slug', 'product_code', 'unit_price', 'active'],
}),
(_("Translatable Fields"), {
'fields': ('caption',)
'fields': ['caption'],
}),
(_("Properties"), {
'fields': ('manufacturer',)
'fields': ['manufacturer'],
}),
)
filter_horizontal = ('cms_pages',)
inlines = (ProductImageInline,)
prepopulated_fields = {'slug': ('product_name',)}
]
filter_horizontal = ['cms_pages']
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}


@admin.register(SmartCard)
class SmartCardAdmin(SortableAdminMixin, TranslatableAdmin, FrontendEditableAdminMixin,
CMSPageAsCategoryMixin, PlaceholderAdminMixin, PolymorphicChildModelAdmin):
base_model = Product
fieldsets = (
fieldsets = [
(None, {
'fields': ('product_name', 'slug', 'product_code', 'unit_price', 'active',),
'fields': ['product_name', 'slug', 'product_code', 'unit_price', 'active'],
}),
(_("Translatable Fields"), {
'fields': ('caption', 'description',)
'fields': ['caption', 'description'],
}),
(_("Properties"), {
'fields': ('manufacturer', 'storage', 'card_type', 'speed',)
'fields': ['manufacturer', 'storage', 'card_type', 'speed'],
}),
)
filter_horizontal = ('cms_pages',)
inlines = (ProductImageInline,)
prepopulated_fields = {'slug': ('product_name',)}
]
filter_horizontal = ['cms_pages']
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}

admin.site.register(OperatingSystem, admin.ModelAdmin)

Expand Down Expand Up @@ -88,7 +88,7 @@ class SmartPhoneAdmin(SortableAdminMixin, TranslatableAdmin, FrontendEditableAdm
}),
]
filter_horizontal = ['cms_pages']
inlines = (ProductImageInline, SmartPhoneInline,)
inlines = [ProductImageInline, SmartPhoneInline]
prepopulated_fields = {'slug': ['product_name'],}

def save_model(self, request, obj, form, change):
Expand Down
18 changes: 9 additions & 9 deletions example/myshop/admin/i18n_smartcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
@admin.register(SmartCard)
class SmartCardAdmin(SortableAdminMixin, TranslatableAdmin,
CMSPageAsCategoryMixin, admin.ModelAdmin):
fieldsets = (
fieldsets = [
(None, {
'fields': ('product_name', 'slug', 'product_code', 'unit_price', 'active',),
'fields': ['product_name', 'slug', 'product_code', 'unit_price', 'active'],
}),
(_("Translatable Fields"), {
'fields': ('caption', 'description',)
'fields': ['caption', 'description'],
}),
(_("Properties"), {
'fields': ('manufacturer', 'storage', 'card_type',)
'fields': ['manufacturer', 'storage', 'card_type'],
}),
)
inlines = (ProductImageInline,)
prepopulated_fields = {'slug': ('product_name',)}
list_display = ('product_name', 'product_code', 'unit_price', 'active',)
search_fields = ('product_name',)
]
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}
list_display = ['product_name', 'product_code', 'unit_price', 'active']
search_fields = ['product_name']
12 changes: 6 additions & 6 deletions example/myshop/admin/polymorphic_.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class CommodityAdmin(SortableAdminMixin, FrontendEditableAdminMixin, Placeholder
"""
base_model = Product
fields = ['product_name', 'slug', 'product_code', 'unit_price', 'active', 'caption', 'manufacturer']
filter_horizontal = ('cms_pages',)
inlines = (ProductImageInline,)
filter_horizontal = ['cms_pages']
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}


Expand All @@ -39,7 +39,7 @@ class SmartCardAdmin(SortableAdminMixin, FrontendEditableAdminMixin,
fields = ['product_name', 'slug', 'product_code', 'unit_price', 'active', 'caption',
'description', 'manufacturer', 'storage', 'card_type', 'speed']
filter_horizontal = ['cms_pages']
inlines = (ProductImageInline,)
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}

admin.site.register(OperatingSystem, admin.ModelAdmin)
Expand All @@ -58,8 +58,8 @@ class SmartPhoneAdmin(SortableAdminMixin, FrontendEditableAdminMixin, CMSPageAsC
'battery_type', 'battery_capacity', 'ram_storage', 'wifi_connectivity', 'bluetooth',
'gps', 'operating_system', ('width', 'height', 'weight'), 'screen_size']
filter_horizontal = ['cms_pages']
inlines = (ProductImageInline, SmartPhoneInline,)
prepopulated_fields = {'slug': ('product_name',)}
inlines = [ProductImageInline, SmartPhoneInline]
prepopulated_fields = {'slug': ['product_name']}

def save_model(self, request, obj, form, change):
if not change:
Expand All @@ -81,7 +81,7 @@ class ProductAdmin(PolymorphicSortableAdminMixin, PolymorphicParentModelAdmin):
list_display = ['product_name', 'get_price', 'product_type', 'active']
list_display_links = ['product_name']
search_fields = ['product_name']
list_filter = (PolymorphicChildModelFilter, CMSPageFilter,)
list_filter = [PolymorphicChildModelFilter, CMSPageFilter]
list_per_page = 250
list_max_show_all = 1000

Expand Down
16 changes: 8 additions & 8 deletions example/myshop/admin/smartcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

@admin.register(SmartCard)
class SmartCardAdmin(SortableAdminMixin, CMSPageAsCategoryMixin, admin.ModelAdmin):
fieldsets = (
fieldsets = [
(None, {
'fields': ('product_name', 'slug', 'product_code', 'unit_price', 'active', 'description',),
'fields': ['product_name', 'slug', 'product_code', 'unit_price', 'active', 'description'],
}),
(_("Properties"), {
'fields': ('manufacturer', 'storage', 'card_type', 'speed',)
'fields': ['manufacturer', 'storage', 'card_type', 'speed'],
}),
)
inlines = (ProductImageInline,)
prepopulated_fields = {'slug': ('product_name',)}
list_display = ('product_name', 'product_code', 'unit_price', 'active',)
search_fields = ('product_name',)
]
inlines = [ProductImageInline]
prepopulated_fields = {'slug': ['product_name']}
list_display = ['product_name', 'product_code', 'unit_price', 'active']
search_fields = ['product_name']
22 changes: 11 additions & 11 deletions shop/admin/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

class CustomerInlineAdminBase(admin.StackedInline):
model = CustomerModel
fieldsets = (
(None, {'fields': ('get_number',)}),
)
readonly_fields = ('get_number',)
fieldsets = [
(None, {'fields': ['get_number']}),
]
readonly_fields = ['get_number']

def get_extra(self, request, obj=None, **kwargs):
return 0 if obj is None else 1
Expand Down Expand Up @@ -76,15 +76,15 @@ class CustomerAdminBase(UserAdmin):
```
@admin.register(CustomerProxy)
class CustomerAdmin(CustomerAdminBase):
inlines = (CustomerInlineAdminBase,)
inlines = [CustomerInlineAdminBase]
```
"""
form = CustomerChangeForm
add_form = CustomerCreationForm
list_display = ('get_username', 'last_name', 'first_name', 'recognized', 'last_access', 'is_unexpired')
segmentation_list_display = ('get_username',)
list_filter = UserAdmin.list_filter + (CustomerListFilter,)
readonly_fields = ('last_login', 'date_joined', 'last_access', 'recognized')
list_display = ['get_username', 'last_name', 'first_name', 'recognized', 'last_access', 'is_unexpired']
segmentation_list_display = ['get_username']
list_filter = list(UserAdmin.list_filter) + [CustomerListFilter]
readonly_fields = ['last_login', 'date_joined', 'last_access', 'recognized']
ordering = ['id']

#class Media:
Expand All @@ -93,8 +93,8 @@ class CustomerAdmin(CustomerAdminBase):
def get_fieldsets(self, request, obj=None):
fieldsets = list(super(CustomerAdminBase, self).get_fieldsets(request, obj=obj))
if obj:
fieldsets[0][1]['fields'] = ('username', 'recognized', 'password',)
fieldsets[3][1]['fields'] = ('date_joined', 'last_login', 'last_access',)
fieldsets[0][1]['fields'] = ['username', 'recognized', 'password']
fieldsets[3][1]['fields'] = ['date_joined', 'last_login', 'last_access']
if not obj.has_usable_password():
fieldsets.pop(2)
return fieldsets
Expand Down
20 changes: 10 additions & 10 deletions shop/admin/defaults/commodity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
@admin.register(Commodity)
class CommodityAdmin(SortableAdminMixin, TranslatableAdmin, FrontendEditableAdminMixin,
PlaceholderAdminMixin, CMSPageAsCategoryMixin, admin.ModelAdmin):
fieldsets = (
fieldsets = [
(None, {
'fields': ('product_name', 'slug', 'caption',)
'fields': ['product_name', 'slug', 'caption']
}),
(_("Common Fields"), {
'fields': ('product_code', ('unit_price', 'active',), 'show_breadcrumb', 'sample_image'),
'fields': ['product_code', ('unit_price', 'active'), 'show_breadcrumb', 'sample_image'],
}),
)
filter_horizontal = ('cms_pages',)
list_filter = (CMSPageFilter,)
]
filter_horizontal = ['cms_pages']
list_filter = [CMSPageFilter]

def get_prepopulated_fields(self, request, obj=None):
return {
Expand All @@ -37,7 +37,7 @@ def get_prepopulated_fields(self, request, obj=None):
@admin.register(Commodity)
class CommodityAdmin(SortableAdminMixin, FrontendEditableAdminMixin, PlaceholderAdminMixin,
CMSPageAsCategoryMixin, admin.ModelAdmin):
fields = ('product_name', 'slug', 'caption', 'product_code',
('unit_price', 'active',), 'show_breadcrumb', 'sample_image',)
filter_horizontal = ('cms_pages',)
prepopulated_fields = {'slug': ('product_name',)}
fields = ['product_name', 'slug', 'caption', 'product_code',
('unit_price', 'active'), 'show_breadcrumb', 'sample_image']
filter_horizontal = ['cms_pages']
prepopulated_fields = {'slug': ['product_name']}
10 changes: 5 additions & 5 deletions shop/admin/defaults/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@


class CustomerInlineAdmin(CustomerInlineAdminBase):
fieldsets = (
(None, {'fields': ('get_number', 'salutation')}),
(_("Addresses"), {'fields': ('get_shipping_addresses', 'get_billing_addresses')})
)
readonly_fields = ('get_number', 'get_shipping_addresses', 'get_billing_addresses')
fieldsets = [
(None, {'fields': ['get_number', 'salutation']}),
(_("Addresses"), {'fields': ['get_shipping_addresses', 'get_billing_addresses']})
]
readonly_fields = ['get_number', 'get_shipping_addresses', 'get_billing_addresses']

def get_number(self, customer):
return customer.get_number() or '–'
Expand Down
34 changes: 19 additions & 15 deletions shop/admin/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
class OrderPaymentInline(admin.TabularInline):
model = OrderPayment
extra = 0
fields = ('amount', 'transaction_id', 'payment_method', 'created_at',)
readonly_fields = ('created_at',)
fields = ['amount', 'transaction_id', 'payment_method', 'created_at']
readonly_fields = ['created_at']

def get_formset(self, request, obj=None, **kwargs):
"""
Expand All @@ -43,10 +43,12 @@ def has_delete_permission(self, request, obj=None):
class OrderItemInline(admin.StackedInline):
model = OrderItemModel
extra = 0
fields = (
('product_code', 'unit_price', 'line_total',), ('quantity',), 'render_as_html_extra',
)
readonly_fields = ('product_code', 'quantity', 'unit_price', 'line_total', 'render_as_html_extra',)
fields = [
('product_code', 'unit_price', 'line_total',),
('quantity',),
'render_as_html_extra',
]
readonly_fields = ['product_code', 'quantity', 'unit_price', 'line_total', 'render_as_html_extra']
template = 'shop/admin/edit_inline/stacked-order.html'

def has_add_permission(self, request, obj=None):
Expand Down Expand Up @@ -85,17 +87,19 @@ def queryset(self, request, queryset):


class BaseOrderAdmin(FSMTransitionMixin, admin.ModelAdmin):
list_display = ('get_number', 'customer', 'status_name', 'get_total', 'created_at',)
list_filter = (StatusListFilter,)
fsm_field = ('status',)
list_display = ['get_number', 'customer', 'status_name', 'get_total', 'created_at']
list_filter = [StatusListFilter]
fsm_field = ['status']
date_hierarchy = 'created_at'
inlines = (OrderItemInline, OrderPaymentInline,)
readonly_fields = ('get_number', 'status_name', 'get_total', 'get_subtotal',
inlines = [OrderItemInline, OrderPaymentInline]
readonly_fields = ['get_number', 'status_name', 'get_total', 'get_subtotal',
'get_customer_link', 'get_outstanding_amount', 'created_at', 'updated_at',
'render_as_html_extra', 'stored_request',)
fields = ('get_number', 'status_name', ('created_at', 'updated_at'), 'get_customer_link',
('get_subtotal', 'get_total', 'get_outstanding_amount',),
'render_as_html_extra', 'stored_request',)
'render_as_html_extra', 'stored_request']
fields = ['get_number', 'status_name',
('created_at', 'updated_at'),
'get_customer_link',
('get_subtotal', 'get_total', 'get_outstanding_amount'),
'render_as_html_extra', 'stored_request']
actions = None
change_form_template = 'shop/admin/change_form.html'

Expand Down
1 change: 1 addition & 0 deletions shop/admin/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def label_from_instance(self, obj):
else:
return str(obj)


class CMSPageAsCategoryMixin(object):
"""
Add this mixin class to the ModelAdmin class for products wishing to be assigned to djangoCMS
Expand Down

0 comments on commit aa1b6e4

Please sign in to comment.