2 import oils.event, oils.const
6 ''' Collection of general purpose org_unit utility functions '''
14 if not OrgUtil._org_tree:
15 OrgUtil.fetch_org_tree()
19 ''' Returns the whole org_unit tree '''
22 return OrgUtil._org_tree
24 tree = osrf.ses.ClientSession.atomic_request(
25 oils.const.OILS_APP_ACTOR,
26 'open-ils.actor.org_tree.retrieve')
28 oils.event.Event.parse_and_raise(tree)
29 OrgUtil._org_tree = tree
30 OrgUtil.flatten_org_tree(tree)
34 def flatten_org_tree(node):
35 ''' Creates links from an ID-based hash to the org units in the org tree '''
37 node = OrgUtil._org_tree
38 OrgUtil._flat_org_tree[int(node.id())] = node
39 for child in node.children():
40 OrgUtil.flatten_org_tree(child)
43 def get_org_unit(org_id):
44 OrgUtil._verify_tree()
45 if isinstance(org_id, osrf.net_obj.NetworkObject):
47 return OrgUtil._flat_org_tree[int(org_id)]
51 def fetch_org_types():
52 ''' Returns the list of org_unit_type objects '''
54 if OrgUtil._org_types:
55 return OrgUtil._org_types
57 types = osrf.ses.ClientSession.atomic_request(
58 oils.const.OILS_APP_ACTOR, 'open-ils.actor.org_types.retrieve')
60 oils.event.Event.parse_and_raise(types)
61 OrgUtil._org_types = types
66 def get_org_type(org_unit):
67 ''' Given an org_unit, this returns the org_unit_type object it's linked to '''
68 types = OrgUtil.fetch_org_types()
69 return [t for t in types if t.id() == org_unit.ou_type()][0]
73 def get_related_tree(org_unit):
74 ''' Returns a cloned tree of orgs including all ancestors and
75 descendants of the provided org '''
77 OrgUtil._verify_tree()
78 org = org_unit = OrgUtil.get_org_unit(org_unit.id()).shallow_clone()
79 while org.parent_ou():
80 parent = org.parent_ou()
81 if not isinstance(parent, osrf.net_obj.NetworkObject):
82 parent = OrgUtil._flat_org_tree[parent]
83 parent = parent.shallow_clone()
84 parent.children([org])
89 node = node.shallow_clone()
90 children = node.children()
93 for child in children:
94 node.children().append(trim_org(child))
101 def get_union_tree(org_id_list):
102 ''' Returns the smallest org tree which encompases all of the orgs in org_id_list '''
104 OrgUtil._verify_tree()
105 if len(org_id_list) == 0:
107 main_tree = OrgUtil.get_related_tree(OrgUtil.get_org_unit(org_id_list[0]))
109 if len(org_id_list) == 1:
112 for org in org_id_list[1:]:
113 node = OrgUtil.get_related_tree(OrgUtil.get_org_unit(org))
114 main_node = main_tree
116 while node.id() == main_node.id():
117 child = node.children()[0]
118 main_child_node = main_node.children()[0]
119 child.parent_ou(node)
120 main_child_node.parent_ou(main_node)
122 main_node = main_child_node
124 main_node.parent_ou().children().append(node)
129 def get_related_list(org_unit):
130 ''' Returns a flat list of related org_units '''
131 OrgUtil._verify_tree()
132 tree = OrgUtil.get_related_tree(org_unit)
136 for child in node.children():
142 def get_min_depth(org_id_list):
143 ''' Returns the minimun depth (highest tree position) of all orgs in the list '''
145 for org in org_id_list:
146 new_depth = OrgUtil.get_org_type(OrgUtil.get_org_unit(org)).depth()
149 elif new_depth < depth:
154 def debug_tree(org_unit, indent=0):
155 ''' Simple function to print the tree of orgs provided '''
156 for i in range(indent):
157 sys.stdout.write('_')
158 print '%s id=%s depth=%s' % (org_unit.shortname(), str(org_unit.id()), str(OrgUtil.get_org_type(org_unit).depth()))
160 for child in org_unit.children():
161 OrgUtil.debug_tree(child, indent)