Notes on how I pulled this off and questions on performance.

First, to make my template work, I devised a dirty template tag called
group_by that lives in my custom product module. The code is
relatively boring:
-----------------------------------------------------
import os, logging
from django import template
from django.template import Library, Node, Variable
from hss.apps.hssproduct.models import *
from satchmo.utils import cross_list

log = logging.getLogger('hssproduct.models')
register = template.Library()

class ProductGroupsNode(Node):
        def __init__(self, product, groups, varname):
                self.product = Variable(product)
                self.groups = groups
                self.varname = varname
        def render(self, context):
                self.product = self.product.resolve(context)
                p_types = self.product.get_subtypes()
                groups = [group.strip() for group in self.groups.split(',')]
                if 'ConfigurableProduct' in p_types:
                        cp = self.product.configurableproduct
                        og = [group for group in groups if 
cp.option_group.filter
(name=group)]
                        op = [cp.option_group.get(name=option) for option in og]
                        vr = cp.get_valid_options()
                        # build a list containing all possible combinations 
based on the
groups provided
                        masterlist = []
                        sublist = []
                        for group in op:
                                for value in group.option_set.all():
                                        sublist.append(value)
                                masterlist.append(sublist)
                                sublist = []
                        opt = cross_list(masterlist)
                        masterlist = []
                        for group in opt:
                                ls = {}
                                ls['products'] = []
                                for option in group:
                                        ls[option.option_group.name] = 
option.name
                                for variant in 
cp.get_variations_for_options(group):
                                        options = variant.options
                                        varopt = {}
                                        varopt['product'] = variant
                                        for option in variant.options.all():
                                                if not option.option_group in 
op:
                                                        
varopt[option.option_group.name] = option.value
                                                        varopt['sort_order'] = 
option.sort_order
                                        ls['products'].append(varopt)
                                if len(ls['products']):
                                        masterlist.append(ls)
                else:
                        raise template.TemplateSyntaxError, 'This is not a 
configurable
product.'
                context[self.varname] = masterlist
                return ''

#group_options for product by "Gender, Colors" as product_options
@register.tag
def group_options(parser, token):
        try:
                tag_name, for_string, product, by_string, groupings, as_string,
varname = token.split_contents()
        except:
                raise template.TemplateSyntaxError, 'get_product_photos takes
exactly four arguments'
        return ProductGroupsNode(product, groupings[1:-1], varname)
-----------------------------------------------------

Which is handy, and lets me do this in my template:
{% group_options for product by "Gender, Colors" as product_options %}

Ultra handy, and made my life remarkably easy.

Problem is, with this new code, I'm having to hit the database WAY too
much. I'd assume that option and options groups were already being
retrieved for a product, and would therefor be cached, but this does
not seem to be the case.

A bare bones configurableproduct template requires something akin to
200 queries, and my code is doubling that without blinking.

Any insights into how I can do this faster?

Thanks.

On Nov 9, 3:48 pm, Jack Shedd <[EMAIL PROTECTED]> wrote:
> Here's a screen grab of the current template. Hopefully this clarifies
> what I need to accomplish.
>
> http://jackshedd.com/examples/hssproductpage.jpg
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to