Changeset 639
- Timestamp:
- 10/22/08 16:28:11 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugins/resourcetoolsplugin/tracresourcetools/relation/api.py
r634 r639 39 39 yield related_resource 40 40 41 def get_ancestors(self, resource, req=None ):41 def get_ancestors(self, resource, req=None, tree=None): 42 42 """Returns a list of ancestors. 43 43 … … 46 46 @return: Generator 47 47 """ 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 55 63 56 def get_children(self, resource ):64 def get_children(self, resource, tree=None): 57 65 """Returns a list of child resources. 58 66 … … 60 68 @return: Generator 61 69 """ 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 66 78 67 79 def get_tree(self): … … 71 83 """ 72 84 nodes = {} 73 _ new_node = lambda x: nodes.setdefault(x, dict(resource=x,children=[]))85 _get_node = lambda x: nodes.setdefault(x, ResourceTreeNode(x)) 74 86 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) 77 91 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 78 97 79 98 # internals … … 81 100 parts = resource_description.split(':', 1) 82 101 if len(parts)==1: 83 parts = [default_realm]+ parts102 parts = [default_realm]+(parts[0]=='' and [None] or parts) 84 103 return Resource(parts[0], parts[1]) 104 105 class 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.
