views:

695

answers:

1

I am using MPTT's templatetag to render my genre tree.

   {% for genre, structure in genres|tree_info %}
      {% if tree.new_level %}<ul><li>{% else %}</li><li>{% endif %}
         {{ genre.name }}
      {% for level in tree.closed_levels %}</li></ul>{% endfor %}
   {% endfor %}

The thing is, my genre object has is_visible property which should be respected.

    def is_visible(self):  
       if self.is_root_node() or not self.visibility:
           return self.visibility                          
       for parent in self.get_ancestors():
           if not parent.visibility:      
               return False                                    
       return True

What's the smartest and cleanest way to accomplish this?


additional information

I need the (X)HTML list nesting to be produced properly. I have defined SQL functions for inhertited visibility checking.

CREATE OR REPLACE function get_genre_parent_id( _genre_id int )
RETURNS INTEGER AS $$
DECLARE 
    _parent_id INTEGER;
    _genre_id ALIAS FOR $1;    
BEGIN
    SELECT parent_id INTO _parent_id
    FROM product_productgenre
    WHERE id = _genre_id;

    RETURN _parent_id;

END;
$$  LANGUAGE plpgsql;


CREATE OR REPLACE function is_genre_visible( _genre_id int )
RETURNS BOOLEAN AS $$
DECLARE 
    _visible BOOLEAN;
    _genre_id ALIAS FOR $1;    
BEGIN
    SELECT visibility INTO _visible
    FROM product_productgenre
    WHERE id = _genre_id;

    RETURN _visible;

END;
$$  LANGUAGE plpgsql;


CREATE OR REPLACE function is_genre_branch_visible( _genre_id int )
RETURNS BOOLEAN AS $$
DECLARE 
    visible BOOLEAN;
    _genre_id ALIAS FOR $1;
    _temp_genre_id INTEGER;
BEGIN
    visible = true;
    _temp_genre_id := _genre_id;
    -- checking for our own genre
    IF NOT is_genre_visible(_temp_genre_id) THEN
        RETURN false;
    END IF;
    -- iterating through all parent genres
    WHILE get_genre_parent_id(_temp_genre_id) IS NOT NULL LOOP
        _temp_genre_id = get_genre_parent_id(_temp_genre_id);
        IF NOT is_genre_visible(_temp_genre_id) THEN
            RETURN false;
        END IF;
    END LOOP;

    RETURN visible;

END;
$$  LANGUAGE plpgsql;

And tried to override the full_tree_for_model tag to make it use a custom manager, which simply adds extra is_genre_branch_visible(genre_id) to the QuerySet. But something goes wrong with the ordering, can't really figure this out.

Plus It works, but I don't like the approach, feels ugly to me.

A: 

Well, a simple

{% if genre.is_visible %}

just inside the for-loop shoud do the trick :)

mikl
It will not, since it will generate invalid tags nesting (`tree.closed_levels`).
ohnoes