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
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))
def get(self): self.response.headers['Content-Type'] = 'application/json' target_key = ndb.Key(urlsafe = self.request.get('target')) if target_key.kind() == 'CN': target = CN.get_by_key(target_key.urlsafe()) if target_key.kind() == 'OU': target = OU.get_by_key(target_key.urlsafe()) if target_key.kind() == 'Organization': target = Organization.get_by_key(target_key.urlsafe()) url = config.PROTOCOL + config.APP_HOSTNAME + '/api/operation/list/sequential?direction=in&target=%s' % target_key.urlsafe() logging.warning('DEBUG Balance - operationlist_in url: %s' % url) result = urlfetch.fetch(url) if result.status_code == 200: operationlist_in = json.loads(result.content) #logging.warning('DEBUG CNBalance - operationlist_in: %s' % str(operationlist_in)) if self.request.get('type') == 'effective': url = config.PROTOCOL + config.APP_HOSTNAME + '/api/deposit/list?target=%s' % target_key.urlsafe() logging.warning('DEBUG Balance - depositlist url: %s' % url) result = urlfetch.fetch(url) if result.status_code == 200: depositlist = json.loads(result.content) #logging.warning('DEBUG CNBalance - depositlist: %s' % str(depositlist)) else: depositlist = [] elif self.request.get('type') == 'virtual': url = config.PROTOCOL + config.APP_HOSTNAME + '/api/operation/list/sequential?direction=out&target=%s' % target_key.urlsafe() logging.warning('DEBUG Balance - operationlist_out url: %s' % url) result = urlfetch.fetch(url) if result.status_code == 200: depositlist = json.loads(result.content) else: depositlist = [] for deposit in depositlist: depost['date_unix'] = deposit['cutof_date_unix'] if self.request.get('type') in ['effective', 'virtual']: capital = 0.0 operations_amount = 0.0 for deposit in depositlist: capital += deposit['amount'] for operation in operationlist_in: operation['capital'] = capital capital_to_date = 0.0 for deposit in depositlist: if deposit['date_unix'] <= operation['cutoff_date_unix']: capital_to_date += deposit['amount'] capital_to_date -= operations_amount operation['capital_to_date'] = capital_to_date if time.mktime(datetime.now().timetuple()) > operation['cutoff_date_unix'] and capital_to_date < operation['amount']: operation['late_payment'] = True operation['amount_before_raise'] = operation['amount'] operation['amount'] += (operation['amount'] * operation['delay_raise'] / 100) else: operation['late_payment'] = False if capital >= operation['amount']: operation['covered'] = True else: operation['covered'] = False operation['rest'] = operation['amount'] - capital logging.warning('DEBUG StudenBalance operation: %s' % str({'capital': capital, 'capital_to_date': capital_to_date, 'operation_amount': operation['amount']})) capital -= operation['amount'] balance = operationlist_in else: balance = {'status': 1, 'message': 'Invalid balance type.'} self.response.write(json.dumps(balance))