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

show variations in admin #60

Open
wants to merge 3 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
47 changes: 43 additions & 4 deletions cartridge/shop/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
from copy import deepcopy

from django.contrib import admin
from django.db.models import ImageField
from django.core.urlresolvers import reverse
from django.db.models import ImageField, CharField
from django.forms import TextInput
from django.utils.translation import ugettext_lazy as _

from mezzanine.conf import settings
Expand Down Expand Up @@ -94,7 +96,7 @@ class CategoryAdmin(PageAdmin):
variations_extra = 1


class ProductVariationAdmin(admin.TabularInline):
class ProductVariationInlineAdmin(admin.TabularInline):
verbose_name_plural = _("Current variations")
model = ProductVariation
fields = variation_fields
Expand All @@ -105,7 +107,7 @@ class ProductVariationAdmin(admin.TabularInline):
formset = ProductVariationAdminFormset


class ProductImageAdmin(TabularDynamicInlineAdmin):
class ProductImageInlineAdmin(TabularDynamicInlineAdmin):
model = ProductImage
formfield_overrides = {ImageField: {"widget": ImageWidget}}

Expand Down Expand Up @@ -150,7 +152,7 @@ class Media:
filter_horizontal = ("categories", "related_products", "upsell_products")
search_fields = ("title", "content", "categories__title",
"variations__sku")
inlines = (ProductImageAdmin, ProductVariationAdmin)
inlines = (ProductImageInlineAdmin, ProductVariationInlineAdmin)
form = ProductAdminForm
fieldsets = product_fieldsets

Expand Down Expand Up @@ -238,6 +240,42 @@ class ProductOptionAdmin(admin.ModelAdmin):
radio_fields = {"type": admin.HORIZONTAL}


class ProductVariationAdmin(admin.ModelAdmin):
list_display = ("admin_thumb", "__unicode__", "sku",
"num_in_stock", "unit_price", "sale_price", "product_url",)
list_editable = ("sku", "num_in_stock", "unit_price", "sale_price")
formfield_overrides = {
CharField: {'widget': TextInput(attrs={'class': ''})},
}

def product_url(self, obj):
url = reverse("admin:shop_product_change", args=(obj.product.id,))
return '<a href="%s">%s</a>' % (url, obj.product)
product_url.allow_tags = True
product_url.short_description = _("Change")

def change_view(self, request, object_id=None):
if object_id:
variation = ProductVariation.objects.get(id=object_id)
product_id = variation.product.id
from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('admin:shop_product_change',
args=(product_id,)))

def has_add_permission(self, request):
return False

def save_model(self, request, obj, form, change):
"""
Copy data from the default variation to the original product fields.
"""
super(ProductVariationAdmin, self).save_model(request, obj,
form, change)
self._productvariation = obj
if self._productvariation.default:
self._productvariation.product.copy_default_variation()


class OrderItemInline(admin.TabularInline):
verbose_name_plural = _("Items")
model = OrderItem
Expand Down Expand Up @@ -311,6 +349,7 @@ class DiscountCodeAdmin(admin.ModelAdmin):
admin.site.register(Product, ProductAdmin)
if settings.SHOP_USE_VARIATIONS:
admin.site.register(ProductOption, ProductOptionAdmin)
admin.site.register(ProductVariation, ProductVariationAdmin)
admin.site.register(Order, OrderAdmin)
admin.site.register(Sale, SaleAdmin)
admin.site.register(DiscountCode, DiscountCodeAdmin)
3 changes: 2 additions & 1 deletion cartridge/shop/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
default=(
(_("Content"), ("pages.Page", "blog.BlogPost",
"generic.ThreadedComment", (_("Media Library"), "fb_browse"),)),
(_("Shop"), ("shop.Product", "shop.ProductOption", "shop.DiscountCode",
(_("Shop"), ("shop.Product", "shop.ProductOption",
"shop.ProductVariation", "shop.DiscountCode",
"shop.Sale", "shop.Order")),
(_("Site"), ("sites.Site", "redirects.Redirect", "conf.Setting")),
(_("Users"), ("auth.User", "auth.Group",)),
Expand Down
Binary file modified cartridge/shop/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
Loading