def get(self): self.response.headers['Content-Type'] = 'application/json' target = self.request.get('target') url = '%s%s/api/organization/tree?entity=%s' % (config.PROTOCOL, config.APP_HOSTNAME, target) result = urlfetch.fetch(url) if result.status_code == 200: orgtree = json.loads(result.content) else: orgtree = None logging.warning('DEBUG OperationList - orgtree: %s' % str(orgtree)) key = ndb.Key(urlsafe = target) operationlist = [] replaced = [] has_parent = True if orgtree is not None: while has_parent: if key.kind() == 'CN': entity = CN.get_by_key(key.urlsafe()) operations = CustomOperation.fetch_by_target(entity.key.urlsafe()) if key.kind() == 'OU': entity = OU.get_by_key(key.urlsafe()) operations = Operation.fetch_by_target(entity.key.urlsafe()) if key.kind() == 'Organization': entity = Organization.get_by_key(key.urlsafe()) operations = Operation.fetch_by_target(entity.key.urlsafe()) has_parent = False for operation in operations: operationdict = {'key': operation.key.urlsafe(), 'name': operation.name, 'target': operation.target.urlsafe(), 'target_kind': key.kind(), 'direction': operation.direction, 'amount': operation.amount, 'frequency': operation.frequency, 'cutoff_date': json.loads(operation.cutoff_date), 'delay_raise': operation.delay_raise, 'created': tools.datetime_to_str(operation.created), 'updated': tools.datetime_to_str(operation.updated)} if key.kind() == 'CN': operationdict['parent'] = operation.parent.urlsafe() replaced.append(operation.parent.urlsafe()) operationlist.append(operationdict) if has_parent: key = entity.parent index = 0 operationlist_clone = operationlist[:] for operation in operationlist_clone: logging.warning('DEBUG OperationList multidirectional - replaced: %s - operation[key]: %s- in: %s' % (str(replaced), operation['key'], str(operation['key'] in replaced))) if operation['key'] in replaced: operationlist.pop(index) else: index += 1 logging.warning('operationlist(%i) - operationlist_clone(%i)' % (len(operationlist), len(operationlist_clone))) if self.request.get('direction') in ['in', 'out']: index = 0 oplist_clone = operationlist[:] logging.warning('DEBUG OperationList unidirectional - operationlist(%i): %s' % (len(oplist_clone), str(operationlist))) for op in oplist_clone: if op['direction'] != self.request.get('direction'): operationlist.pop(index) else: index += 1 logging.warning('operationlist(%i) - operationlist_clone(%i)' % (len(operationlist), len(oplist_clone))) self.response.write(json.dumps(operationlist))