Enabling show_empty setting for Categories

Create issue
Issue #478 new
Former user created an issue

Within the satchmo_category.py templatetags file is an option for "show_entry." I set that to False (should this be a setting somewhere outside the code?) and the empty categories are not returned, but the active categories are not obtained/formatted correctly. Here's an example

Baseball:Pitching Machines Football:Footballs Football:Football Helmets Soccer:Field Equipment

The category_tree that gets returned is:

Baseball Football Footballs Soccer

as if Baseball were the parent category and all of the categories under it are children. The relevant code is below. Any help would be appreciated.

def recurse_for_children(current_node, parent_node, active_cat, show_empty=False): child_count = current_node.child.count()

if show_empty or child_count > 0 or

current_node.product_set.count() > 0: temp_parent = SubElement(parent_node, 'li') attrs = {'href': current_node.get_absolute_url()} if current_node == active_cat: attrs["class"] = "current" link = SubElement(temp_parent, 'a', attrs) link.text = current_node.translated_name()

    if child_count > 0:
        new_parent = SubElement(temp_parent, 'ul')
        children = current_node.child.all()
        for child in children:
            recurse_for_children(child, new_parent, active_cat)

def category_tree(id=None): """ Creates an unnumbered list of the categories. For example: <ul> <li>Books <ul> <li>Science Fiction <ul> <li>Space stories</li> <li>Robot stories</li> </ul> </li> <li>Non-fiction</li> </ul> </ul> """ active_cat = None if id: active_cat = Category.objects.get(id=id) root = Element("ul") for cats in Category.objects.filter(parent__isnull=True): recurse_for_children(cats, root, active_cat) return tostring(root, 'utf-8')

register.simple_tag(category_tree)

What I ended up doing to make it work was to add the "show_empty = False" argument from the calling function, so under

def category_tree(id=None): ... for cats in Category.objects.filter(parent__isnull=True): recurse_for_children(cats, root, active_cat, show_empty=False) return tostring(root, 'utf-8')

Reported by jroynan

Comments (0)

  1. Log in to comment