Hi Jack, This is wonderful. I may need this feature. I will try it out on my machine and post it here again.
Thanks, Shakthi ----- Original Message ----- From: "Jack Shedd" <[EMAIL PROTECTED]> To: "Satchmo users" <[email protected]> Sent: Wednesday, 3 December, 2008 17:51:27 GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi Subject: A better way to handle product photos? By default, Satchmo just stores a huge list of photos for a given product. Aside from sort order, there is no way to distinguish one photo from another. That blows. Here's what I did instead: On my custom product module, I have a few models: ----------------------------------------------------------------------------- class HSSProductPhotoType(models.Model): name = models.CharField(blank=False, max_length=75) def _get_subtype(self): return "HSS Product Photo Type" def __unicode__(self): return u"%s" % self.name class Meta: verbose_name = _('Photo Type') verbose_name_plural = _('Photo Types') ----------------------------------------------------------------------------- class HSSProductPhoto(models.Model): product = models.ForeignKey(HSSProduct, blank=False) phototype = models.ForeignKey(HSSProductPhotoType, blank=False, verbose_name=_('Type')) picture = ImageWithThumbnailField(verbose_name='Picture', upload_to="assets/products/pictures/", name_field="_filename", blank=True, null=True) sortorder = models.IntegerField(blank=False, null=False) def _get_subtype(self): return "HSS Product Photo" def __unicode__(self): return u"%s-%s-%s-%s" % (self.product.product.slug, self.phototype.name, self.sortorder, self.id) @property def _filename(self): if self.phototype: return u"%s-%s-%s-%s" % (self.product.product.slug, self.phototype.name, self.sortorder, self.id) else: return 'unknown' class Meta: verbose_name = "Product Photo" verbose_name_plural = "Product Photos" ordering = ['sortorder'] ----------------------------------------------------------------------------- In my admin.py for the module: ----------------------------------------------------------------------------- from django.contrib import admin from hss.apps.hssproduct.models import * class PhotosInline(admin.TabularInline): model = HSSProductPhoto class HSSProductAdmin(admin.ModelAdmin): inlines = [ PhotosInline, ] admin.site.register(HSSProduct, HSSProductAdmin) admin.site.register(HSSProductPhotoType) ----------------------------------------------------------------------------- This makes it easier to have different photos for different applications for a product, such as one image for the cart, another for previews, or "live shots" or just specific layout sections. Next, I have a template tag, "get_product_photos" that works like this: {% get_product_photos for product of type "Preview" as preview_photo %} Code looks like: ----------------------------------------------------------------------------- import os, logging from django import template from django.template import Library, Node, Variable from hss.apps.hssproduct.models import * from satchmo.product.models import ConfigurableProduct, ProductVariation log = logging.getLogger('hssproduct.models') register = template.Library() class ProductPhotosNode(Node): def __init__(self, product, photo_type, varname): self.product = Variable(product) self.photo_type = photo_type self.varname = varname def render(self, context): photo_type = HSSProductPhotoType.objects.get(name=self.photo_type) temp_product = self.product.resolve(context) ptypes = temp_product.get_subtypes() if 'ProductVariation' in ptypes: pv = ProductVariation.objects.get(product=temp_product) if pv: temp_product = pv.parent if photo_type: photos = HSSProductPhoto.objects.filter(product=temp_product, phototype=photo_type) if photos: log.debug('Found %s photos' % len(photos)) if len(photos) == 1: context[self.varname] = photos[0] else: log.debug('Returning %s photos' % len(photos)) context[self.varname] = photos else: log.debug('Found 0 photos') else: raise template.TemplateSyntaxError, '%s is not a valid photo type for this' % self.photo_type return '' #get_product_photos for product of type 'Cart' as cart_photo @register.tag def get_product_photos(parser, token): try: tag_name, for_string, product, of_string, type_string, type_name, as_string, varname = token.split_contents() except: raise template.TemplateSyntaxError, 'get_product_photos takes exactly five arguments and you passed %s' % len(bits) return ProductPhotosNode(product, type_name[1:-1], varname) ----------------------------------------------------------------------------- Nothing complicated, but for the type of stores I'm using satchmo for, infinetly more useful than a straight list of photos. Requires some admin training, but works well enough. Could stand to be made more fault tolerant, I guess. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Satchmo users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/satchmo-users?hl=en -~----------~----~----~----~------~----~------~--~---
