def get(self): self.response.headers['Content-Type'] = 'application/json' key = ndb.Key(urlsafe = self.request.get('entity')) if key.kind() == 'Organization': entity = Organization.get_by_key(key.urlsafe()) elif key.kind() == 'OU': entity = OU.get_by_key(key.urlsafe()) elif key.kind() == 'CN': entity = CN.get_by_key(key.urlsafe()) logging.warning('DEBUG OrganizationTree - key.kind(): %s' % key.kind()) if key.kind() in ['Organization', 'OU']: name = entity.name else: name = entity.first_name + ' ' + entity.last_name treedict = {'key': entity.key.urlsafe(), 'name': name, 'kind': key.kind()} level = treedict # Ascending tree while key.kind() != 'Organization': parent = entity.parent if parent.kind() == 'Organization': entity = Organization.query(Organization.key == parent).get() else: entity = OU.query(OU.key == parent).get() key = entity.key logging.warning('DEBUG OrganizationTree - parent: %s - entity: %s' % (parent, str(entity))) entitydict = {'key': entity.key.urlsafe(), 'name': entity.name, 'kind': parent.kind()} level['parent'] = entitydict level = level['parent'] children = [] ouchildren = OU.query(OU.parent == key).fetch() for ouchild in ouchildren: oudict = {'key': ouchild.key.urlsafe(), 'name': ouchild.name, 'kind': ouchild.key.kind()} children.append(oudict) cnchildren = CN.query(CN.parent == key).fetch() for cnchild in cnchildren: cndict = {'key': cnchild.key.urlsafe(), 'name': cnchild.first_name + ' ' + cnchild.last_name, 'kind': cnchild.key.kind()} children.append(cndict) treedict['child'] = pop_list(treedict) self.response.write(json.dumps(treedict))
def pop_list(node): #logging.warning('DEBUG pop_list - node: %s' % str(node)) nodelist = [] ouchildren = OU.query(OU.parent == ndb.Key(urlsafe = node['key'])).fetch() for ouchild in ouchildren: oudict = {'key': ouchild.key.urlsafe(), 'name': ouchild.name, 'kind': ouchild.key.kind()} nodelist.append(oudict) cnchildren = CN.query(CN.parent == ndb.Key(urlsafe = node['key'])).fetch() for cnchild in cnchildren: cndict = {'key': cnchild.key.urlsafe(), 'name': cnchild.first_name + ' ' + cnchild.last_name, 'kind': cnchild.key.kind()} nodelist.append(cndict) if len(nodelist) > 0: node['child'] = nodelist for node in node['child']: if pop_list(node) is not None: node['child'] = pop_list(node) return nodelist else: return None