Commits

Anonymous committed 31340f0

Added sub-product model
By default, don't clutter Admin view with lots of blank related records

  • Participants
  • Parent commits 954331a

Comments (0)

Files changed (2)

catalogue/admin.py

 from gnocchi_catalogue import models
 from gnocchi_tools.admin import InlineAttributeAdmin
 
-class InlinePriceBreakAdmin( admin.TabularInline ):
+class InlinePriceBreakAdmin(admin.TabularInline):
     model = models.PriceBreak
+    extra = 0
 
-class InlineVariantAdmin( admin.TabularInline ):
+class InlineVariantAdmin(admin.TabularInline):
     model = models.ProductVariant
+    extra = 0
 
-class InlineProductImageAdmin( admin.TabularInline ):
-    model = models.ProductImage
+class InlineImageAdmin(admin.TabularInline):
+    model = models.Image
+    extra = 0
 
-class ProductAdmin( admin.ModelAdmin ):
-    list_display = ( 'title', 'price', 'short_description', 'get_codes', )
-    list_filter = ( 'price', )
-    filter_horizontal = ( 'related_products', )
+class InlineSubProductAdmin(admin.TabularInline):
+    model = models.SubProduct
+    extra = 0
+
+class ProductAdmin(admin.ModelAdmin):
+    list_display = ('title', 'price', 'short_description', 'get_codes',)
+    list_filter = ('price',)
+    filter_horizontal = ('related_products',)
     inlines = [
+        InlineSubProdictAdmin,
         InlineVariantAdmin,
-        InlineProductImageAdmin,
+        InlineImageAdmin,
         InlinePriceBreakAdmin,
         InlineAttributeAdmin,
     ]
     def get_codes(self, obj):
-        return u', '.join(obj.productvariant_set.values_list('sku', flat=True ))
+        return u', '.join(obj.productvariant_set.values_list('sku', flat=True))
     get_codes.short_description = 'Codes'
 
-
-admin.site.register( models.Product, ProductAdmin )
+admin.site.register(models.Product, ProductAdmin)

catalogue/models.py

 from django.db import models
 from django.core.urlresolvers import reverse
-from django.core.exceptions import ValidationError
 from django.contrib.contenttypes import generic
-from django.utils.translation import ugettext_lazy as _
-
-from datetime import datetime
-from decimal import Decimal
 
 from taggit.managers import TaggableManager
 
 from gnocchi_tools.attr import AttrHelper
+from gnocchi_catalogue.settings import SKU_SEPARATOR
+
+import os.path
 
 class Product(models.Model, AttrHelper):
-    title = models.CharField(max_length=200, verbose_name=_('Title'))
-    short_description = models.CharField(verbose_name=_('Short Description'),
-        max_length=1024, blank=True)
-    description = models.TextField(verbose_name=_('Description'))
-    price = models.DecimalField(verbose_name=_('Price'), max_digits=12,
-        decimal_places=2)
-    shipping = models.DecimalField(verbose_name=_('Shipping Cost'),
-        max_digits=12, decimal_places=2, default='0.0')
+    title = models.CharField(max_length=200)
+    short_description = models.CharField(max_length=1024, blank=True)
+    sku = models.CharField(max_length=256)
+    description = models.TextField()
+    price = models.DecimalField(max_digits=12, decimal_places=2)
+    shipping = models.DecimalField(max_digits=12, decimal_places=2, default='0.0')
     attributes = generic.GenericRelation('tools.Attribute')
 
     tags = TaggableManager()
 
-    related_products = models.ManyToManyField('self', blank=True,
-        verbose_name=_('Related Products'))
+    related_products = models.ManyToManyField('self', blank=True)
 
     def __unicode__(self):
         return self.title
         return reverse('product_detail', args=[self.pk])
 
 def product_image_name(instance, filename):
-    return 'images/products/%s/%s' % (instance.product.pk, filename,)
+    return os.path.join('images/products/', str(instance.product.pk), filename,)
 
-class ProductImage(models.Model):
+class Image(models.Model):
     product = models.ForeignKey('Product', related_name='images')
     order = models.IntegerField(default=1)
     image = models.ImageField(upload_to=product_image_name)
     class Meta:
         ordering = ('-order',)
 
-class ProductVariant(models.Model):
+class PriceBreak(models.Model):
     product = models.ForeignKey('Product')
-    sku = models.CharField(max_length=256, verbose_name=_("Order Code"))
-    description = models.CharField(max_length=1024, blank=True,
-        verbose_name=_('Description'))
+    quantity = models.PositiveIntegerField()
+    price = models.DecimalField(max_digits=12, decimal_places=2)
+    class Meta:
+        ordering = ('quantity',)
 
-    def __unicode__(self):
-        return u'%s (%s)' % (self.product.short_description, self.description,)
-
-    def get_absolute_url(self):
-        return self.product.get_absolute_url()
-
-class PriceBreak( models.Model ):
+class SubProduct(models.Model):
     product = models.ForeignKey('Product')
-    quantity = models.PositiveIntegerField(verbose_name=_('Quantity'))
-    price = models.DecimalField(max_digits=12, decimal_places=2,
-        verbose_name=_('Price'))
-    class Meta:
-        ordering = ( 'quantity', )
+    sku_extension = models.CharField(max_length=256)
+    short_description = models.CharField(max_digits=1024)
+    @property
+    def sku(self):
+        return SKU_SEPARATOR.join(self.product.sku, self.sku)