Changeset 639


Ignore:
Timestamp:
10/22/08 16:28:11 (5 years ago)
Author:
cbalan
Message:

ResourceTools?: - get_ancestors and get_children can support tree.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/resourcetoolsplugin/tracresourcetools/relation/api.py

    r634 r639  
    3939                yield related_resource 
    4040     
    41     def get_ancestors(self, resource, req=None): 
     41    def get_ancestors(self, resource, req=None, tree=None): 
    4242        """Returns a list of ancestors. 
    4343         
     
    4646        @return: Generator 
    4747        """ 
    48         parent = resource 
    49         for counter in range(self.max_levels_lookup): 
    50             parent_description = self.resource_parent.get(parent, req=req).value 
    51             if not parent_description: 
    52                 break 
    53             parent = self._normalize_resource(parent_description, resource.realm) 
    54             yield parent 
     48        if tree and tree.has_key(resource): 
     49            tree_node = tree[resource] 
     50            for counter in range(self.max_levels_lookup): 
     51                tree_node = tree_node.parent   
     52                if not tree_node: 
     53                    break 
     54                yield tree_node.resource 
     55        else: 
     56            parent = resource 
     57            for counter in range(self.max_levels_lookup): 
     58                parent_description = self.resource_parent.get(parent, req=req).value 
     59                if not parent_description: 
     60                    break 
     61                parent = self._normalize_resource(parent_description, resource.realm) 
     62                yield parent 
    5563             
    56     def get_children(self, resource): 
     64    def get_children(self, resource, tree=None): 
    5765        """Returns a list of child resources. 
    5866         
     
    6068        @return: Generator 
    6169        """ 
    62         rp_system = ResourcePropertySystem(self.env) 
    63         for search_criteria in [resource.realm+':'+resource.id, resource.id]: 
    64             for child_resource in rp_system.get_resources_with_properties(resource_parent = search_criteria):   
    65                 yield child_resource  
     70        if tree and tree.has_key(resource): 
     71            for child_resource in tree[resource].children: 
     72                yield child_resource 
     73        else: 
     74            rp_system = ResourcePropertySystem(self.env) 
     75            for search_criteria in [resource.realm+':'+resource.id, resource.id]: 
     76                for child_resource in rp_system.get_resources_with_properties(resource_parent = search_criteria):   
     77                    yield child_resource  
    6678     
    6779    def get_tree(self): 
     
    7183        """ 
    7284        nodes = {} 
    73         _new_node = lambda x: nodes.setdefault(x, dict(resource=x,children=[])) 
     85        _get_node = lambda x: nodes.setdefault(x, ResourceTreeNode(x)) 
    7486        for rp_model in ResourcePropertyModel.get_resource_properties(self.env, resource_properties=dict(resource_parent = self.resource_parent)): 
    75             node_parent_resource = self._normalize_resource(rp_model.value, rp_model.resource.parent.realm) 
    76             _new_node(node_parent_resource)['children'].append( _new_node(rp_model.resource.parent) ) 
     87            node = _get_node(rp_model.resource.parent) 
     88            node_parent = _get_node(self._normalize_resource(rp_model.value, rp_model.resource.parent.realm)) 
     89            node.parent = node_parent 
     90            node_parent.children.append(node) 
    7791        return nodes 
     92     
     93    def get_cached_tree(self, req): 
     94        if 'resource_hierarchical_tree' not in req.callbacks: 
     95            req.callbacks['resource_hierarchical_tree'] = lambda req: self.get_tree() 
     96        return req.resource_hierarchical_tree 
    7897     
    7998    # internals 
     
    81100        parts = resource_description.split(':', 1) 
    82101        if len(parts)==1: 
    83             parts = [default_realm]+parts 
     102            parts = [default_realm]+(parts[0]=='' and [None] or parts) 
    84103        return Resource(parts[0], parts[1]) 
     104 
     105class ResourceTreeNode(object): 
     106    """Object representing a resource tree node.""" 
     107    __slots__ = ('resource', 'parent', 'children') 
     108    def __init__(self, resource): 
     109        self.resource = resource 
     110        self.parent = None 
     111        self.children=[] 
Note: See TracChangeset for help on using the changeset viewer.