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 get(self): self.response.headers['Content-Type'] = 'application/json' oulist = [] ous = OU.fetch_by_organization(self.request.get('parent')) for ou in ous: oudict = {'key': ou.key.urlsafe(), 'name': ou.name, 'parent': ou.parent} oulist.append(oudict) self.response.write(json.dumps(oulist))
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
def get(self): self.response.headers['Content-Type'] = 'application/json' new = False if len(self.request.get('ou')) > 0: ou = OU.get_by_key(self.request.get('ou')) else: new = True ou = OU() if new or len(self.request.get('parent')) > 0: ou.parent = ndb.Key(urlsafe = self.request.get('parent')) if new or len(self.request.get('name')) > 0: ou.name = self.request.get('name') ou.put() self.response.write(json.dumps({'status': 0, 'message': '%s agregada.' % ou.name, 'ou': {'key': ou.key.urlsafe(), 'name': ou.name, 'parent': ou.parent.urlsafe()}}))