def individual_entity(request, entity_id): cycle = request.GET.get("cycle", DEFAULT_CYCLE) context = {} context["entity_id"] = entity_id context["cycle"] = cycle # get entity metadata metadata = get_metadata(entity_id, cycle, "individual") available_cycles = metadata["available_cycles"] entity_info = metadata["entity_info"] # a little error-checking now that we have the entity info check_entity(entity_info, cycle, "individual") context["entity_info"] = entity_info context["external_links"] = external_sites.get_individual_links( standardize_individual_name(entity_info["name"]), entity_info["external_ids"], cycle ) # get contributions information if it is available for this entity if metadata["contributions"]: context["contributions_data"] = True recipient_candidates = api.indiv_pol_recipients(entity_id, cycle) recipient_orgs = api.indiv_org_recipients(entity_id, cycle) candidates_barchart_data = [] for record in recipient_candidates: candidates_barchart_data.append( { "key": generate_label( standardize_politician_name_with_metadata( record["recipient_name"], record["party"], record["state"] ) ), "value": record["amount"], "href": barchart_href(record, cycle, entity_type="politician"), } ) context["candidates_barchart_data"] = json.dumps(bar_validate(candidates_barchart_data)) orgs_barchart_data = [] for record in recipient_orgs: orgs_barchart_data.append( { "key": generate_label(standardize_organization_name(record["recipient_name"])), "value": record["amount"], "href": barchart_href(record, cycle, entity_type="organization"), } ) context["orgs_barchart_data"] = json.dumps(bar_validate(orgs_barchart_data)) party_breakdown = api.indiv_party_breakdown(entity_id, cycle) for key, values in party_breakdown.iteritems(): party_breakdown[key] = float(values[1]) context["party_breakdown"] = json.dumps(pie_validate(party_breakdown)) context["sparkline_data"] = api.indiv_sparkline(entity_id, cycle) # 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 int(float(entity_info["totals"]["contributor_amount"])) < 0: context["suppress_contrib_graphs"] = True context["reason"] = "negative" elif ( not context["candidates_barchart_data"] and not context["orgs_barchart_data"] and not context["party_breakdown"] ): context["suppress_contrib_graphs"] = True context["reason"] = "empty" # get lobbying info if it's available for this entity if metadata["lobbying"]: context["lobbying_data"] = True context["lobbying_with_firm"] = api.indiv_registrants(entity_id, cycle) context["issues_lobbied_for"] = [item["issue"] for item in api.indiv_issues(entity_id, cycle)] context["lobbying_for_clients"] = api.indiv_clients(entity_id, cycle) context["lobbying_links"] = external_sites.get_lobbying_links( "lobbyist", standardize_individual_name(entity_info["name"]), entity_info["external_ids"], cycle ) return render_to_response("individual.html", context, entity_context(request, cycle, available_cycles))
def organization_entity(request, entity_id): cycle = request.GET.get("cycle", DEFAULT_CYCLE) context = {} context["entity_id"] = entity_id context["cycle"] = cycle metadata = get_metadata(entity_id, cycle, "organization") context["available_cycles"] = metadata["available_cycles"] entity_info = metadata["entity_info"] context["entity_info"] = entity_info # a little error-checking now that we have the entity info check_entity(entity_info, cycle, "organization") entity_info["metadata"]["source_display_name"] = get_source_display_name(entity_info["metadata"]) context["external_links"] = external_sites.get_organization_links( standardize_organization_name(entity_info["name"]), entity_info["external_ids"], cycle ) # get contributions data if it exists for this entity if metadata["contributions"]: context["contributions_data"] = True org_recipients = api.org_recipients(entity_id, cycle=cycle) recipients_barchart_data = [] for record in org_recipients: recipients_barchart_data.append( { "key": generate_label( standardize_politician_name_with_metadata(record["name"], record["party"], record["state"]) ), "value": record["total_amount"], "value_employee": record["employee_amount"], "value_pac": record["direct_amount"], "href": barchart_href(record, cycle, entity_type="politician"), } ) context["recipients_barchart_data"] = json.dumps(bar_validate(recipients_barchart_data)) party_breakdown = api.org_party_breakdown(entity_id, cycle) for key, values in party_breakdown.iteritems(): party_breakdown[key] = float(values[1]) context["party_breakdown"] = json.dumps(pie_validate(party_breakdown)) level_breakdown = api.org_level_breakdown(entity_id, cycle) for key, values in level_breakdown.iteritems(): level_breakdown[key] = float(values[1]) context["level_breakdown"] = json.dumps(pie_validate(level_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 int(float(entity_info["totals"]["contributor_amount"])) < 0: context["suppress_contrib_graphs"] = True context["reason"] = "negative" elif ( not context["recipients_barchart_data"] and not context["party_breakdown"] and not context["level_breakdown"] ): context["suppress_contrib_graphs"] = True context["reason"] = "empty" if cycle != DEFAULT_CYCLE: if int(cycle) == int(LATEST_CYCLE): cut_off_at_step = months_into_cycle_for_date(datetime.date.today(), cycle) else: cut_off_at_step = 24 else: cut_off_at_step = 9999 context["cut_off_sparkline_at_step"] = cut_off_at_step context["sparkline_data"] = api.org_sparkline_by_party(entity_id, cycle) # get lobbying info if it exists for this entity if metadata["lobbying"]: context["lobbying_data"] = True is_lobbying_firm = bool(entity_info["metadata"].get("lobbying_firm", False)) context["is_lobbying_firm"] = is_lobbying_firm if is_lobbying_firm: context["lobbying_clients"] = api.org_registrant_clients(entity_id, cycle) context["lobbying_lobbyists"] = api.org_registrant_lobbyists(entity_id, cycle) context["lobbying_issues"] = [item["issue"] for item in api.org_registrant_issues(entity_id, cycle)] context["lobbying_links"] = external_sites.get_lobbying_links( "firm", standardize_organization_name(entity_info["name"]), entity_info["external_ids"], cycle ) else: context["lobbying_clients"] = api.org_registrants(entity_id, cycle) context["lobbying_lobbyists"] = api.org_lobbyists(entity_id, cycle) context["lobbying_issues"] = [item["issue"] for item in api.org_issues(entity_id, cycle)] context["lobbying_links"] = external_sites.get_lobbying_links( "client", standardize_organization_name(entity_info["name"]), entity_info["external_ids"], cycle ) return render_to_response( "organization.html", context, entity_context(request, cycle, metadata["available_cycles"]) )