From 6c50ab0f1844486c2f0560da22527cdc77039f39 Mon Sep 17 00:00:00 2001 From: erickson Date: Tue, 22 Jan 2008 20:03:18 +0000 Subject: [PATCH] more org utility code git-svn-id: svn://svn.open-ils.org/ILS/trunk@8466 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/python/oils/org.py | 60 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/Open-ILS/src/python/oils/org.py b/Open-ILS/src/python/oils/org.py index 6fc60e550c..46c221b1c8 100644 --- a/Open-ILS/src/python/oils/org.py +++ b/Open-ILS/src/python/oils/org.py @@ -6,27 +6,44 @@ class OrgUtil(object): _org_tree = None _org_types = None + _flat_org_tree = {} @staticmethod def fetch_org_tree(): ''' Returns the whole org_unit tree ''' + if OrgUtil._org_tree: return OrgUtil._org_tree + tree = osrf.ses.ClientSession.atomic_request( oils.const.OILS_APP_ACTOR, 'open-ils.actor.org_tree.retrieve') + oils.event.Event.parse_and_raise(tree) OrgUtil._org_tree = tree + OrgUtil.flatten_org_tree(tree) return tree @staticmethod + def flatten_org_tree(node): + ''' Creates links from an ID-based hash to the org units in the org tree ''' + if not node: + node = OrgUtil._org_tree + OrgUtil._flat_org_tree[node.id()] = node + for child in node.children(): + OrgUtil.flatten_org_tree(child) + + + @staticmethod def fetch_org_types(): ''' Returns the list of org_unit_type objects ''' + if OrgUtil._org_types: return OrgUtil._org_types + types = osrf.ses.ClientSession.atomic_request( - oils.const.OILS_APP_ACTOR, - 'open-ils.actor.org_types.retrieve') + oils.const.OILS_APP_ACTOR, 'open-ils.actor.org_types.retrieve') + oils.event.Event.parse_and_raise(types) OrgUtil._org_types = types return types @@ -39,3 +56,42 @@ class OrgUtil(object): return [t for t in types if t.id() == org_unit.ou_type()][0] + @staticmethod + def get_related_tree(org_unit): + ''' Returns a cloned tree of orgs including all ancestors and + descendants of the provided org ''' + + org = org_unit = org_unit.shallow_clone() + while org.parent_ou(): + parent = org.parent_ou() + if not isinstance(parent, osrf.net_obj.NetworkObject): + parent = OrgUtil._flat_org_tree[parent] + parent = parent.shallow_clone() + parent.children([org]) + org = parent + root = org + + def trim_org(node): + node = node.shallow_clone() + children = node.children() + if len(children) > 0: + node.children([]) + for child in children: + node.children().append(trim_org(child)) + return node + + trim_org(org_unit) + return root + + @staticmethod + def debug_org(org_unit, indent=0): + ''' Simple function to print the tree of orgs provided ''' + import sys + for i in range(indent): + sys.stdout.write('-') + print org_unit.shortname() + indent += 1 + for child in org_unit.children(): + OrgUtil.debug_org(child, indent) + + -- 2.11.0