Browse Source

using a separate field for a Product image's thumbnail

master
Frederic 11 years ago
parent
commit
f2d5398b23
  1. 1
      cash/admin.py
  2. 16
      cash/models.py

1
cash/admin.py

@ -94,6 +94,7 @@ class ProductAdmin(admin.ModelAdmin): @@ -94,6 +94,7 @@ class ProductAdmin(admin.ModelAdmin):
list_filter = ['category', 'active']
ordering = ('-active', 'name')
inlines = [ProductBarcodeInline]
fields = ('name', 'price', 'active', 'category', 'image')
class ProductCategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'comment')

16
cash/models.py

@ -1,14 +1,16 @@ @@ -1,14 +1,16 @@
from django.conf import settings
from django.db import models
from django.core.files import File
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db.models.signals import pre_save, post_save
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django_auth_ldap.backend import populate_user
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext_noop
import PIL
import StringIO
class Account(models.Model):
user = models.OneToOneField(User)
@ -114,6 +116,9 @@ class Product(models.Model): @@ -114,6 +116,9 @@ class Product(models.Model):
verbose_name = _('category'))
image = models.ImageField(upload_to="products", verbose_name = _('image'),
blank=True, null=True)
image_thumbnail = models.ImageField(upload_to="products_thumb",
verbose_name = _('image'),
blank=True, null=True)
def __unicode__(self):
return self.name
@ -122,19 +127,20 @@ class Product(models.Model): @@ -122,19 +127,20 @@ class Product(models.Model):
verbose_name = _('product')
verbose_name_plural = _('products')
@receiver(post_save, sender=Product)
@receiver(pre_save, sender=Product)
def product_post_save_handler(sender, instance, **kwargs):
img = instance.image
if img:
scaledFile = StringIO.StringIO()
img.open(mode='r')
with img:
scaled = PIL.Image.open(img)
thumbnail_size = getattr(settings, 'THUMBNAIL_SIZE', (150, 150))
scaled.thumbnail(thumbnail_size, PIL.Image.ANTIALIAS)
scaled.save(scaledFile, 'PNG')
scaledFile.seek(0)
img.open(mode='w')
with img:
scaled.save(img)
instance.image_thumbnail.save(img.url, File(scaledFile), save=False)
class ProductBarcode(models.Model):

Loading…
Cancel
Save