Source

gnocchi-catalogue / catalogue / models.py

Full commit
from django.db import models
from django.core.urlresolvers import reverse
from django.contrib.contenttypes import generic

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)
    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)

    def __unicode__(self):
        return self.title
    def get_price(self, quantity):
        try:
            return self.pricebreak_set.filter(quantity__lte=quantity)[0].price
        except (PriceBreak.DoesNotExist, IndexError):
            return self.price
    def get_absolute_url(self):
        return reverse('product_detail', args=[self.pk])

def product_image_name(instance, filename):
    return os.path.join('images/products/', str(instance.product.pk), filename,)

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 PriceBreak(models.Model):
    product = models.ForeignKey('Product')
    quantity = models.PositiveIntegerField()
    price = models.DecimalField(max_digits=12, decimal_places=2)
    class Meta:
        ordering = ('quantity',)

class SubProduct(models.Model):
    product = models.ForeignKey('Product')
    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)