def fetch(self): entity_id, cycle, external_ids = self.entity.entity_id, self.entity.cycle, self.entity.external_ids self.data['top_contributors'] = api.pol.contributors(entity_id, cycle) self.data['top_industries'] = api.pol.industries(entity_id, cycle=cycle) self.data['industries_unknown_amount'] = api.pol.industries_unknown(entity_id, cycle=cycle) self.data['local_breakdown'] = api.pol.local_breakdown(entity_id, cycle) self.data['entity_breakdown'] = api.pol.contributor_type_breakdown(entity_id, cycle) self.partytime_link, self.partytime_data = external_sites.get_partytime_data(external_ids) self.data['bundling'] = api.entities.bundles(entity_id, cycle) self.fec_summary = api.pol.fec_summary(entity_id, cycle) if int(cycle) != -1 else None if self.fec_summary: self.data['fec_timeline'] = api.pol.fec_timeline(entity_id, cycle) self.fec_indexp = api.pol.fec_indexp(entity_id, cycle)[:10] return True
def fetch(self): entity_id, cycle, external_ids = self.entity.entity_id, self.entity.cycle, self.entity.external_ids self.data['top_contributors'] = api.pol.contributors(entity_id, cycle) self.data['top_industries'] = api.pol.industries(entity_id, cycle=cycle) self.data['industries_unknown_amount'] = api.pol.industries_unknown( entity_id, cycle=cycle) self.data['local_breakdown'] = api.pol.local_breakdown( entity_id, cycle) self.data['entity_breakdown'] = api.pol.contributor_type_breakdown( entity_id, cycle) self.partytime_link, self.partytime_data = external_sites.get_partytime_data( external_ids) self.data['bundling'] = api.entities.bundles(entity_id, cycle) self.fec_summary = api.pol.fec_summary( entity_id, cycle) if int(cycle) != -1 else None if self.fec_summary: self.data['fec_timeline'] = api.pol.fec_timeline(entity_id, cycle) self.fec_indexp = api.pol.fec_indexp(entity_id, cycle)[:10] return True
def pol_contribution_section(entity_id, standardized_name, cycle, amount, external_ids): section = { 'name': 'Campaign Finance', 'template': 'contributions.html', } section['contributions_data'] = True top_contributors = api.pol.contributors(entity_id, cycle) top_industries = api.pol.industries(entity_id, cycle=cycle) section['pct_known'] = pct_contribs_from_known_industries(entity_id, cycle, amount) contributors_barchart_data = [] for record in top_contributors: contributors_barchart_data.append({ 'key': generate_label(str(OrganizationNameCleaver(record['name']).parse())), 'value' : record['total_amount'], 'value_employee' : record['employee_amount'], 'value_pac' : record['direct_amount'], 'href' : barchart_href(record, cycle, 'organization') }) contributors_barchart_data = bar_validate(contributors_barchart_data) section['contributors_barchart_data'] = json.dumps(contributors_barchart_data) industries_barchart_data = [] for record in top_industries: industries_barchart_data.append({ 'key': generate_label(str(OrganizationNameCleaver(record['name']).parse())), 'href': barchart_href(record, cycle, 'industry'), 'value' : record['amount'], }) industries_barchart_data = bar_validate(industries_barchart_data) section['industries_barchart_data'] = json.dumps(industries_barchart_data) local_breakdown = api.pol.local_breakdown(entity_id, cycle) for key, values in local_breakdown.iteritems(): # values is a list of [count, amount] local_breakdown[key] = float(values[1]) local_breakdown = pie_validate(local_breakdown) section['local_breakdown'] = json.dumps(local_breakdown) entity_breakdown = api.pol.contributor_type_breakdown(entity_id, cycle) for key, values in entity_breakdown.iteritems(): # values is a list of [count, amount] entity_breakdown[key] = float(values[1]) entity_breakdown = pie_validate(entity_breakdown) section['entity_breakdown'] = json.dumps(entity_breakdown) # if none of the charts have data, or if the aggregate total # received was negative, then suppress that whole content # section except the overview bar if amount < 0: section['suppress_contrib_graphs'] = True section['reason'] = "negative" elif not any((industries_barchart_data, contributors_barchart_data, local_breakdown, entity_breakdown)): section['suppress_contrib_graphs'] = True section['reason'] = 'empty' partytime_link, section['partytime_data'] = external_sites.get_partytime_data(external_ids) section['external_links'] = external_sites.get_contribution_links('politician', standardized_name.name_str(), external_ids, cycle) if partytime_link: section['external_links'].append({'url': partytime_link, 'text': 'Party Time'}) bundling = api.entities.bundles(entity_id, cycle) section['bundling_data'] = [ [x[key] for key in 'lobbyist_entity lobbyist_name firm_entity firm_name amount'.split()] for x in bundling ] if int(cycle) == LATEST_CYCLE: section['fec_summary'] = api.pol.fec_summary(entity_id, cycle) if section['fec_summary']: section['include_fec'] = True if section['fec_summary'] and 'date' in section['fec_summary']: section['fec_summary']['clean_date'] = datetime.datetime.strptime(section['fec_summary']['date'], "%Y-%m-%d") timelines = [] for pol in api.pol.fec_timeline(entity_id, cycle): tl = { 'name': pol['candidate_name'], 'party': pol['party'], 'is_this': pol['entity_id'] == entity_id, 'timeline': map(lambda item: item if item >= 0 else 0, pol['timeline']), 'href': '/politician/%s/%s?cycle=%s' % (slugify(PoliticianNameCleaver(pol['candidate_name']).parse().name_str()), pol['entity_id'], cycle) } tl['sum'] = sum(tl['timeline']) timelines.append(tl) timelines.sort(key=lambda t: (int(t['is_this']), t['sum']), reverse=True) # restrict to top 5, and only those receiving at least 10% of this pol's total if timelines: this_sum = timelines[0]['sum'] timelines = [timeline for timeline in timelines if timeline['sum'] > 0.1 * this_sum] timelines = timelines[:5] section['fec_timelines'] = json.dumps(timelines) section['fec_indexp'] = api.pol.fec_indexp(entity_id, cycle)[:10] return section