Exemple #1
0
def committee(request, committee_id):
    # grab url query string parameters
    cycle = request.GET.get('cycle', None)

    redirect_to_previous = False if cycle else True
    committee, candidates, cycle = api_caller.load_with_nested(
        'committee', committee_id, 'candidates', cycle)

    parent = 'data'
    cycle = int(cycle)
    year = to_date(committee, cycle)
    result_type = 'committees'

    # Link to current cycle if candidate has a corresponding page, else link
    # without cycle query parameter
    # See https://github.com/18F/openFEC/issues/1536
    for candidate in candidates:
        election_years = [
            election_year for election_year in candidate['election_years']
            if election_year -
            election_durations[candidate['office']] < cycle <= election_year
        ]
        candidate['related_cycle'] = max(
            election_years) if election_years else None

    # add related candidates a level below
    financials = api_caller.load_cmte_financials(committee_id, cycle=cycle)

    report_type = report_types.get(committee['committee_type'], 'pac-party')
    reports = financials['reports']
    totals = financials['totals']

    context_vars = {
        'cycle': cycle,
        'timePeriod': str(int(cycle) - 1) + '–' + str(cycle),
        'name': committee['name'],
    }

    template_variables = {
        'name': committee['name'],
        'committee': committee,
        'committee_id': committee_id,
        'committee_type_full': committee['committee_type_full'],
        'committee_type': committee['committee_type'],
        'designation_full': committee['designation_full'],
        'street_1': committee['street_1'],
        'city': committee['city'],
        'state': committee['state'],
        'zip': committee['zip'],
        'treasurer_name': committee['treasurer_name'],
        'parent': parent,
        'cycle': cycle,
        'cycles': committee['cycles'],
        'year': year,
        'result_type': result_type,
        'report_type': report_type,
        'reports': reports,
        'totals': totals,
        'min_receipt_date': utils.three_days_ago(),
        'context_vars': context_vars,
        'party_full': committee['party_full']
    }

    if financials['reports'] and financials['totals']:
        # Format the current two-year-period's totals using the process utilities
        if committee['committee_type'] == 'I':
            # IE-only committees have very little data, so they just get this one
            template_variables['ie_summary'] = utils.process_ie_data(
                financials['totals'][0])
        else:
            # All other committees have three tables
            template_variables['raising_summary'] = utils.process_raising_data(
                financials['totals'][0])
            template_variables[
                'spending_summary'] = utils.process_spending_data(
                    financials['totals'][0])
            template_variables['cash_summary'] = utils.process_cash_data(
                financials['totals'][0])

    if redirect_to_previous and not financials['reports']:
        # If there's no reports, find the first year with reports and redirect there
        for c in sorted(committee['cycles'], reverse=True):
            financials = api_caller.load_cmte_financials(
                committee['committee_id'], cycle=c)
            if financials['reports']:
                return redirect(
                    reverse('committee-by-id',
                            kwargs={'committee_id': committee['committee_id']})
                    + '?cycle=' + str(c))

    # If it's not a senate committee and we're in the current cycle
    # check if there's any raw filings in the last three days
    if committee['committee_type'] != 'S' and cycle == utils.current_cycle():
        raw_filings = api_caller._call_api(
            'efile',
            'filings',
            cycle=cycle,
            committee_id=committee['committee_id'],
            min_receipt_date=template_variables['min_receipt_date'])
        if len(raw_filings.get('results')) > 0:
            template_variables['has_raw_filings'] = True
    else:
        template_variables['has_raw_filings'] = False

    return render(request, 'committees-single.jinja', template_variables)
Exemple #2
0
def get_committee(committee_id, cycle):
    """
    Given a committee_id and cycle, call the API and get the committee
    and committee financial data needed to render the committee profile page
    """

    committee, all_candidates, cycle = load_committee_history(
        committee_id, cycle)
    # When there are multiple candidate records of various offices (H, S, P)
    # linked to a single committee ID,
    # associate the candidate record with the matching committee type
    candidates = [
        candidate for candidate in all_candidates
        if committee["committee_type"] == candidate["office"]
    ]

    parent = "data"
    result_type = "committees"
    cycle = int(cycle)
    year = to_date(committee, cycle)

    # Link to current cycle if candidate has a corresponding page, else link
    # without cycle query parameter
    # See https://github.com/fecgov/openFEC/issues/1536
    for candidate in candidates:
        election_years = []
        for election_year in candidate["election_years"]:
            start_of_election_period = (
                election_year - election_durations[candidate["office"]])
            if start_of_election_period < cycle and cycle <= election_year:
                election_years.append(election_year)
        # For each candidate, set related_cycle to the candidate's time period
        # relative to the selected cycle.
        candidate["related_cycle"] = cycle if election_years else None

    report_type = report_types.get(committee["committee_type"], "pac-party")

    cycle_out_of_range, fallback_cycle, cycles = load_cycle_data(
        committee, cycle)

    reports, totals = load_reports_and_totals(committee_id, cycle,
                                              cycle_out_of_range,
                                              fallback_cycle)

    # Check organization types to determine SSF status
    is_ssf = committee.get("organization_type") in [
        "W", "C", "L", "V", "M", "T"
    ]

    # if cycles_has_activity's options are more than cycles_has_financial's,
    # when clicking back to financial summary/raising/spending,
    # reset cycle=fallback_cycle and timePeriod and.
    # to make sure missing message page show correct timePeriod.
    time_period_js = str(int(cycle) - 1) + "–" + str(cycle)
    cycle_js = cycle
    if cycle_out_of_range:
        cycle_js = fallback_cycle
        time_period_js = str(int(cycle_js) - 1) + "–" + str(cycle_js)

    context_vars = {
        "cycle": cycle_js,
        "timePeriod": time_period_js,
        "name": committee["name"],
        "cycleOutOfRange": cycle_out_of_range,
        "lastCycleHasFinancial": fallback_cycle,
    }

    template_variables = {
        "candidates": candidates,
        "committee": committee,
        "committee_id": committee_id,
        "committee_type": committee["committee_type"],
        "context_vars": context_vars,
        "cycle": cycle,
        "cycles": cycles,
        "is_SSF": is_ssf,
        "cycle_out_of_range": cycle_out_of_range,
        "parent": parent,
        "result_type": result_type,
        "report_type": report_type,
        "reports": reports,
        "totals": totals,
        "min_receipt_date": utils.three_days_ago(),
        "context_vars": context_vars,
        "party_full": committee["party_full"],
        "candidates": candidates,
        "social_image_identifier": "data",
        "year": year,
        "timePeriod": time_period_js,
    }
    # Format the current two-year-period's totals
    if reports and totals:
        # IE-only committees
        if committee["committee_type"] == "I":
            template_variables["ie_summary"] = utils.process_ie_data(totals)
        # Inaugural Committees
        elif committee["organization_type"] == "I":
            template_variables[
                "inaugural_summary"] = utils.process_inaugural_data(totals)
        # Host Committees that file on Form 4
        elif committee["organization_type"] == "H" and reports[
                "form_type"] == "F4":
            template_variables[
                "raising_summary"] = utils.process_host_raising_data(totals)
            template_variables[
                "spending_summary"] = utils.process_host_spending_data(totals)
            template_variables["cash_summary"] = utils.process_cash_data(
                totals)
        else:
            # All other committees have three tables
            template_variables["raising_summary"] = utils.process_raising_data(
                totals)
            template_variables[
                "spending_summary"] = utils.process_spending_data(totals)
            template_variables["cash_summary"] = utils.process_cash_data(
                totals)

    # If in the current cycle, check for raw filings in the last three days
    if cycle == utils.current_cycle():
        # (4)call efile/filings under tag: efiling
        path = "/efile/filings/"
        filters = {}
        filters["cycle"] = cycle
        filters["committee_id"] = committee["committee_id"]
        filters["min_receipt_date"] = template_variables["min_receipt_date"]
        raw_filings = api_caller.load_endpoint_results(path, **filters)

        template_variables["has_raw_filings"] = True if raw_filings else False
    else:
        template_variables["has_raw_filings"] = False

    # Needed for filings tab
    template_variables["filings_lookup"] = {
        "reports": ["F3", "F3X", "F3P", "F3L", "F4", "F5", "F7", "F13"],
        "notices": ["F5", "F24", "F6", "F9", "F10", "F11"],
        "statements": ["F1"],
        "other": ["F1M", "F8", "F99", "F12"],
    }

    # Call /filings?committee_id=C00693234&form_type=F1
    # Get the statements of organization
    statement_of_organization = api_caller.load_committee_statement_of_organization(
        committee_id)

    if statement_of_organization:
        statement_of_organization["receipt_date"] = format_receipt_date(
            statement_of_organization["receipt_date"])

    template_variables["statement_of_organization"] = statement_of_organization

    # Add message for a committee that was formerly an authorized candidate committee.
    # These committees are now unauthorized committees.
    converted_committee_id = None
    former_committee_name = None
    former_authorized_candidate_id = None
    former_authorized_candidate_name = None

    if cycle == 2020 and committee_to_candidate_linkage.get(committee_id):
        # Call /candidate/{candidate_id}/history/
        converted_committee_id = committee.get('committee_id')
        former_authorized_candidate_id = committee_to_candidate_linkage.get(
            converted_committee_id)
        path = "/candidate/" + str(
            former_authorized_candidate_id) + "/history/"
        filters = {}
        filters["per_page"] = 1
        candidate = api_caller.load_first_row_data(path, **filters)
        # Get the converted committee's former name and candidate name
        former_committee_name = former_committee_names.get(
            converted_committee_id)
        former_authorized_candidate_name = candidate.get('name')

    template_variables["former_committee_name"] = former_committee_name
    template_variables[
        "former_authorized_candidate_name"] = former_authorized_candidate_name
    template_variables[
        "former_authorized_candidate_id"] = former_authorized_candidate_id

    return template_variables