Ejemplo n.º 1
0
	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))
Ejemplo n.º 2
0
	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))