Wrote a quick custom template tag to do previous/next products. Might
be useful to someone else.

Use like this:
{% prev_next_products for product as products with 6 others %}

It'll give you a structure that looks like:
products.previous # if there's a product before this one, this will
equal that product, otherwise, it's set to None
products.next # if there's a product after this one, this will equal
that product, otherwise, it's set to None
products.products # array of the next products, based on how many
"others" you told it to grab

Code:
---------------------------------------------------------------------------------------------
import os, logging
from django import template
from django.template import Library, Node, Variable
from django.contrib.sites.models import Site
from satchmo.product.models import Product, ProductManager

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

class ProductOrderNode(Node):
        def __init__(self, product, included_count, varname):
                self.product = Variable(product)
                self.included = included_count
                self.varname = varname
        def render(self, context, **kwargs):
                masterlist = []
                returned = {}
                previous, next, found, last = None, None, None, None
                self.product = self.product.resolve(context)

                site = Site.objects.get_current()
                productlist = Product.objects.filter(active=True,
productvariation__parent__isnull=True)

                for product in productlist:
                        if product != self.product:
                                masterlist.append(product)
                        if found and not next:
                                next = product
                        if (len(masterlist) > self.included) and not found:
                                masterlist.pop(0)
                        elif (len(masterlist) >= self.included) and found:
                                break
                        if not found:
                                if product == self.product:
                                        found = product
                                        previous = last
                        last = product

                if previous:
                        returned['previous'] = previous
                if next:
                        returned['next'] = next
                if len(masterlist):
                        returned['products'] = masterlist
                log.debug('%s' % masterlist)
                context[self.varname] = returned
                return ''

#prev_next_product for product as variable with num others
@register.tag
def prev_next_products(parser, token):
        try:
                tag_name, for_string, product, as_string, varname, with_string,
included_count, others_string = token.split_contents()
        except:
                raise template.TemplateSyntaxError, 'prev_next_product should be
formatted like: prev_next_product for product as variable with num
others'
        return ProductOrderNode(product, included_count, varname)

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