def get(self, request, *args, **kwargs): result = {} if self.kwargs.has_key('id'): id = self.kwargs['id'] #if not cache.has_key('investment_flow_source_'+str(id)): investment = InvestmentData.item(id) recipient = None funding = None obj = None if investment is not None: if investment.recipient_entity_id: # Projeto Receptor do Investimento recipient = investment.recipient_entity result.update({'id': 'p'+str(recipient.entity_id), 'name': recipient.title, 'data': {'level': 0, 'css': 'project'}, 'children': [] }) funding = InvestmentData.list(funding_entity_id = recipient.entity_id)[:1] if not funding and investment.recipient_organization_id: funding = InvestmentData.list(funding_organization_id = investment.recipient_organization_id)[:1] if funding: obj = {'id': 'o'+str(funding.id), 'name': funding.name, 'data': {'level': 0, 'css': 'project'}, 'children': [] } else: obj = {'id': 'p'+str(funding.entity_id), 'name': funding.title, 'data': {'level': 0, 'css': 'project'}, 'children': [] } elif investment.recipient_organization_id: recipient = investment.recipient_organization result.update({'id': 'o'+str(recipient.id), 'name': recipient.name, 'data': {'level': 0, 'css': 'organization'}, 'children': [] }) funding = InvestmentData.list(funding_entity_id = recipient.entity_id)[:1] if recipient: self.before_flow({ 'id': investment.id, 'funding_entity': investment.funding_entity_id, 'funding_organization':investment.funding_organization_id, 'created_at':investment.created_at }, result['children']) if obj : result['children'].append(obj) self.after_flow(investment, obj['children']) #cache.set('investment_flow_source_'+str(id), result, (60 * 60) * 24) #else: # result = cache.get('investment_flow_source_'+str(id)) return http.HttpResponse(dumps(result, cls=DjangoJSONEncoder), content_type='application/json')
def after_flow(self, investment, childrens, level=1): self.after_investments.append(investment.id) # Financiadores if investment.recipient_entity_id: recipient = investment.funding_entity investments = InvestmentData.list(funding_entity_id=recipient.entity_id).exclude(pk__in=self.after_investments, created_at__gt=investment.created_at).all() #investments = funding.funding_investments.all() obj = {'id': 'p_%d_%d' % (level, recipient.entity_id), 'name': recipient.title, 'data': {'level': level, 'css': 'project', 'count': len(investments)}, 'children': [] } if level > 1: childrens.append(obj) self.after_projects.append(recipient.entity_id) c = Counter(self.after_projects) if len(investments) > 0 and c[recipient.entity_id] < 2: for inv in investments: self.after_flow(inv, obj['children'], level+1) elif investment.recipient_organization_id: recipient = investment.funding_organization investments = InvestmentData.list(funding_organization_id=recipient.id).exclude(pk__in=self.after_investments, created_at__gt=investment.created_at).all() #investments = funding.funding_investments.all() obj = {'id': 'o_%d_%d' % (level, recipient.id), 'name': recipient.name, 'data': {'level': level, 'css': 'organization', 'count': len(investments)}, 'children': [] } if level > 1: childrens.append(obj) self.after_orgs.append(recipient.id) c = Counter(self.after_orgs) if len(investments) > 0 and c[recipient.id] < 2: for inv in investments: self.after_flow(inv, obj['children'], level+1)
def before_flow(self, investment, childrens, level=1): investment = self.dict2obj(investment) self.before_investments.append(investment.id) # Financiadores if investment.funding_entity: funding = ProjectData.item(investment.funding_entity) investments = InvestmentData.list(recipient_entity_id=funding.entity_id).exclude(pk__in=self.before_investments, created_at__gt=investment.created_at).all().values('id', 'created_at', 'funding_entity', 'funding_organization').annotate(projects=Count('funding_entity'), organizations=Count('funding_organization')) #investments = funding.recipient_investments.all() obj = {'id': 'p_%d_%d' % (level, funding.entity_id), 'name': funding.title, 'data': {'level': level, 'css': 'project', 'count': len(investments)}, 'children': [] } childrens.append(obj) self.before_projects.append(funding.entity_id) c = Counter(self.before_projects) if len(investments) > 0 and c[funding.entity_id] < 2: for inv in investments: self.before_flow(inv, obj['children'], level+1) elif investment.funding_organization: funding = OrganizationData.item(investment.funding_organization) investments = InvestmentData.list(recipient_organization_id=funding.id).exclude(pk__in=self.before_investments, created_at__gt=investment.created_at).all().values('id', 'created_at', 'funding_entity', 'funding_organization').annotate(projects=Count('funding_entity'), organizations=Count('funding_organization')) #investments = funding.recipient_investments.all() print(investments) obj = {'id': 'o_%d_%d' % (level, funding.id), 'name': funding.name, 'data': {'level': level, 'css': 'organization', 'count': len(investments)}, 'children': [] } childrens.append(obj) self.before_orgs.append(funding.id) c = Counter(self.before_orgs) if len(investments) > 0 and c[funding.id] < 2: for inv in investments: self.before_flow(inv, obj['children'], level+1)