Exemplo n.º 1
0
 def test_election_title(self):
     assert election_title(2016, 'President') == (
         '2016 Election United States President')
     assert election_title(2016, 'Senate', state='CA') == (
         '2016 Election United States Senate - California')
     assert election_title(2016, 'House', state='CA', district='08') == (
         '2016 Election United States House - California - District 08')
Exemplo n.º 2
0
def elections(request, office, cycle, state=None, district=None):
    cycle = int(cycle)

    # Get all cycles up until the cycle from the URL if it's beyond the current cycle
    # this fixes the issue of an election page not showing user-provided cycle
    # in the cycle select
    max_cycle = cycle if cycle > utils.current_cycle(
    ) else utils.current_cycle()
    cycles = utils.get_cycles(max_cycle)

    if office.lower() == 'president':
        cycles = [each for each in cycles if each % 4 == 0]
    elif office.lower() == 'senate':
        cycles = api_caller.get_all_senate_cycles(state)

    if office.lower() not in ['president', 'senate', 'house']:
        raise Http404()
    if state and state.upper() not in constants.states:
        raise Http404()

    return render(
        request, 'elections.jinja', {
            'office': office,
            'office_code': office[0],
            'parent': 'data',
            'cycle': cycle,
            'cycles': cycles,
            'state': state,
            'state_full': constants.states[state.upper()] if state else None,
            'district': district,
            'title': utils.election_title(cycle, office, state, district),
        })
Exemplo n.º 3
0
def elections(request, office, cycle, state=None, district=None):
    cycle = int(cycle)

    # Get all cycles up until the cycle from the URL if it's beyond the current cycle
    # this fixes the issue of an election page not showing user-provided cycle
    # in the cycle select
    max_cycle = cycle if cycle > utils.current_cycle(
    ) else utils.current_cycle()
    cycles = utils.get_cycles(max_cycle)

    if office.lower() == 'president':
        cycles = [each for each in cycles if each % 4 == 0]
    elif office.lower() == 'senate':
        cycles = api_caller.get_all_senate_cycles(state)

    if office.lower() not in ['president', 'senate', 'house']:
        raise Http404()
    if (state is not None) and (state
                                and state.upper() not in constants.states):
        raise Http404()

    #map/redirect legacy tab names to correct anchor
    tab = request.GET.get('tab', '').replace('/', '')
    legacy_tabs = {
        'contributions': '#individual-contributions',
        'totals': '#candidate-financial-totals',
        'spending-by-others': '#independent-expenditures'
    }

    if tab in legacy_tabs.keys():
        if office == 'senate' or office == 'house':
            return redirect(
                reverse('elections-state-district',
                        args=(office, state, district, cycle)) +
                legacy_tabs[tab])
        if office == 'president':
            return redirect(
                reverse('elections-president', args=(office, cycle)) +
                legacy_tabs[tab])

    return render(
        request, 'elections.jinja', {
            'office': office,
            'office_code': office[0],
            'parent': 'data',
            'cycle': cycle,
            'cycles': cycles,
            'state': state,
            'state_full': constants.states[state.upper()] if state else None,
            'district': district,
            'title': utils.election_title(cycle, office, state, district),
        })
Exemplo n.º 4
0
def elections(request, office, cycle, state=None, district=None):
    cycle = int(cycle)

    max_cycle = utils.current_cycle() + 4
    cycles = utils.get_cycles(max_cycle)

    if office.lower() == "president":
        cycles = [each for each in cycles if each % 4 == 0]
    elif office.lower() == "senate":
        cycles = api_caller.get_all_senate_cycles(state)

    if office.lower() not in ["president", "senate", "house"]:
        raise Http404()
    if (state is not None) and (state
                                and state.upper() not in constants.states):
        raise Http404()

    election_duration = election_durations.get(office[0].upper(), 2)
    # Puerto Rico house/resident commissioners have 4-year cycles
    if state and state.upper() == "PR":
        election_duration = 4

    # map/redirect legacy tab names to correct anchor
    tab = request.GET.get("tab", "").replace("/", "")
    legacy_tabs = {
        "contributions": "#individual-contributions",
        "totals": "#candidate-financial-totals",
        "spending-by-others": "#independent-expenditures",
    }

    if tab in legacy_tabs:
        if office == "house":
            return redirect(
                reverse("elections-house",
                        args=(office, state, district, cycle)) +
                legacy_tabs[tab])
        elif office == "senate":
            return redirect(
                reverse("elections-senate", args=(office, state, cycle)) +
                legacy_tabs[tab])
        elif office == "president":
            return redirect(
                reverse("elections-president", args=(office, cycle)) +
                legacy_tabs[tab])

    return render(
        request,
        "elections.jinja",
        {
            "office": office,
            "office_code": office[0],
            "parent": "data",
            "cycle": cycle,
            "election_duration": election_duration,
            "cycles": cycles,
            "state": state,
            "state_full": constants.states[state.upper()] if state else None,
            "district": district,
            "title": utils.election_title(cycle, office, state, district),
            "social_image_identifier": "data",
        },
    )