Exemplo n.º 1
0
def senate(request, cycle):
    
    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404
        
    title="Senate - Cycle Summary"
    explanatory_text="Fundraising totals are for the selected two-year election cycle for Senate candidates who reported having $1,000 or more, or who have been targeted by $1,000 or more in independent expenditures. Only candidates actually running in the current cycle who filed a statement of candidacy are included. If we included anyone who isn't running--or missed anyone who is, please <a href='mailto:[email protected]'>let us know</a>. Please note these totals reflect current FEC filings and may not match the summarized data available elsewhere on Influence Explorer."

    # Give up on ORM for data; we're not willing to enforce all the relationships required for them
    districts = District.objects.filter(office='S', cycle=cycle)

    legislators = Candidate_Overlay.objects.filter(office='S', cycle=cycle).filter(Q(cash_on_hand__gte=1000)|Q(is_incumbent=True)|Q(total_expenditures__gte=1000)).select_related('district').order_by('-cash_on_hand')
    
    districts = District.objects.filter(office='H', cycle=cycle)

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [cycle_fake(cycle, "/senate/%s/" % cycle), cycle_fake(other_year, "/senate/%s/" % other_year)]
    

    return render_to_response('datapages/senate_legislator_list.html',
        {
        'STATE_LIST':STATE_LIST,
        'districts':districts,
        'object_list':legislators,
        'title':title,
        'explanatory_text':explanatory_text,
        'cycle_list':cycle_list
        }, 
        context_instance=RequestContext(request)
    )
Exemplo n.º 2
0
def races(request, cycle):
    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404
        
    title="Race-wide spending totals for %s cycle" % (cycle)
    explanatory_text="District totals (ie. House and Senate races) are based on the most recent information available, but different political groups report to the FEC on different schedules. Super PACs must report independent expenditures within 48- or 24-hours, but candidate committees only disclose on a quarterly basis. Please note these totals reflect current FEC filings and may not match the summarized data available elsewhere on Influence Explorer. <br>For primary contests see our list of <a href='/competitive-primaries/'>competitive primaries</a>."


    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [cycle_fake(cycle, "/races/%s/" % cycle), cycle_fake(other_year, "/races/%s/" % other_year)]

    districts = District.objects.filter(cycle=cycle)

    return render_to_response('datapages/races.html',
        {
        'STATE_LIST':STATE_LIST,
        'title':title,
        'explanatory_text':explanatory_text,
        'races':districts,
        'cycle_list':cycle_list        
        }, 
        context_instance=RequestContext(request)
    )
Exemplo n.º 3
0
def downloads(request, cycle):
    
    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [cycle_fake(cycle, "/download-index/%s/" % cycle), cycle_fake(other_year, "/download-index/%s/" % other_year)]
    
    title="Bulk Downloads" 
    update_time = get_update_time(BULK_EXPORT_KEY)

    return render_to_response('datapages/downloads.html',
        {
        'title':title,
        'cycle':cycle,
        'cycle_list':cycle_list,
        'update_time':update_time,
        }, 
        context_instance=RequestContext(request)
    )
Exemplo n.º 4
0
def downloads(request, cycle):

    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [
        cycle_fake(cycle, "/download-index/%s/" % cycle),
        cycle_fake(other_year, "/download-index/%s/" % other_year)
    ]

    title = "Bulk Downloads"
    update_time = get_update_time(BULK_EXPORT_KEY)

    return render_to_response('datapages/downloads.html', {
        'title': title,
        'cycle': cycle,
        'cycle_list': cycle_list,
        'update_time': update_time,
    },
                              context_instance=RequestContext(request))
Exemplo n.º 5
0
def races(request, cycle):
    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404

    title = "Race-wide spending totals for %s cycle" % (cycle)
    explanatory_text = "District totals (ie. House and Senate races) are based on the most recent information available, but different political groups report to the FEC on different schedules. Super PACs must report independent expenditures within 48- or 24-hours, but candidate committees only disclose on a quarterly basis. Please note these totals reflect current FEC filings and may not match the summarized data available elsewhere on Influence Explorer. <br>For primary contests see our list of <a href='/competitive-primaries/'>competitive primaries</a>."

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [
        cycle_fake(cycle, "/races/%s/" % cycle),
        cycle_fake(other_year, "/races/%s/" % other_year)
    ]

    districts = District.objects.filter(cycle=cycle)

    return render_to_response('datapages/races.html', {
        'STATE_LIST': STATE_LIST,
        'title': title,
        'explanatory_text': explanatory_text,
        'races': districts,
        'cycle_list': cycle_list
    },
                              context_instance=RequestContext(request))
Exemplo n.º 6
0
def outside_spenders(request, cycle):
    
    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [cycle_fake(cycle, "/outside-spenders/%s/" % cycle), cycle_fake(other_year, "/outside-spenders/%s/" % other_year)]
    
    explanatory_text = "Find and filter outside spender information for the entire %s election cycle. Click the committee name to see filings; click the expenditure amount to see this spending broken down line-by-line. By \"major activity\" we mean the activity the PAC has reported spending the most money on." % cycle
    
    return render_to_response('datapages/dynamic_outside_spenders.html', 
        {
        'explanatory_text':explanatory_text,
        'title':"Outside spending committee summaries (%s cycle)" % cycle,
        'PAGINATE_BY':PAGINATE_BY,
        'cycle_list':cycle_list,
        },
        context_instance=RequestContext(request)
    )
Exemplo n.º 7
0
def pacs(request, cycle):
    
    if not is_valid_four_digit_string_cycle(cycle):
        raise Http404
        
    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [cycle_fake(cycle, "/pacs/%s/" % cycle), cycle_fake(other_year, "/pacs/%s/" % other_year)]
    
    return render_to_response('datapages/dynamic_pacs.html', 
        {
        'explanatory_text':'Find and filter committee summary information for an entire two-year cycle (2016 or 2014). Review how much groups raised and spent, their debts and cash on hand. Click the committee name to see filings. For more, see a <a href="/about/#pacs">more detailed explanation</a>.',
        'title':'Political action committee summaries',
        'PAGINATE_BY':PAGINATE_BY,
        'cycle_list':cycle_list
        },
        context_instance=RequestContext(request)
    ) 
Exemplo n.º 8
0
def senate(request, cycle):

    if not is_valid_four_digit_string_cycle(cycle):
        # should redirect, but for now:
        raise Http404

    title = "Senate - Cycle Summary"
    explanatory_text = "Fundraising totals are for the selected two-year election cycle for Senate candidates who reported having $1,000 or more, or who have been targeted by $1,000 or more in independent expenditures. Only candidates actually running in the current cycle who filed a statement of candidacy are included. If we included anyone who isn't running--or missed anyone who is, please <a href='mailto:[email protected]'>let us know</a>. Please note these totals reflect current FEC filings and may not match the summarized data available elsewhere on Influence Explorer."

    # Give up on ORM for data; we're not willing to enforce all the relationships required for them
    districts = District.objects.filter(office='S', cycle=cycle)

    legislators = Candidate_Overlay.objects.filter(
        office='S', cycle=cycle).filter(
            Q(cash_on_hand__gte=1000) | Q(is_incumbent=True)
            | Q(total_expenditures__gte=1000)).select_related(
                'district').order_by('-cash_on_hand')

    districts = District.objects.filter(office='H', cycle=cycle)

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [
        cycle_fake(cycle, "/senate/%s/" % cycle),
        cycle_fake(other_year, "/senate/%s/" % other_year)
    ]

    return render_to_response('datapages/senate_legislator_list.html', {
        'STATE_LIST': STATE_LIST,
        'districts': districts,
        'object_list': legislators,
        'title': title,
        'explanatory_text': explanatory_text,
        'cycle_list': cycle_list
    },
                              context_instance=RequestContext(request))
Exemplo n.º 9
0
def outside_spenders(request, cycle):

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [
        cycle_fake(cycle, "/outside-spenders/%s/" % cycle),
        cycle_fake(other_year, "/outside-spenders/%s/" % other_year)
    ]

    explanatory_text = "Find and filter outside spender information for the entire %s election cycle. Click the committee name to see filings; click the expenditure amount to see this spending broken down line-by-line. By \"major activity\" we mean the activity the PAC has reported spending the most money on." % cycle

    return render_to_response(
        'datapages/dynamic_outside_spenders.html', {
            'explanatory_text': explanatory_text,
            'title': "Outside spending committee summaries (%s cycle)" % cycle,
            'PAGINATE_BY': PAGINATE_BY,
            'cycle_list': cycle_list,
        },
        context_instance=RequestContext(request))
Exemplo n.º 10
0
def pacs(request, cycle):

    if not is_valid_four_digit_string_cycle(cycle):
        raise Http404

    other_year = None
    if cycle == '2016':
        other_year = '2014'
    elif cycle == '2014':
        other_year = '2016'
    cycle_list = [
        cycle_fake(cycle, "/pacs/%s/" % cycle),
        cycle_fake(other_year, "/pacs/%s/" % other_year)
    ]

    return render_to_response('datapages/dynamic_pacs.html', {
        'explanatory_text':
        'Find and filter committee summary information for an entire two-year cycle (2016 or 2014). Review how much groups raised and spent, their debts and cash on hand. Click the committee name to see filings. For more, see a <a href="/about/#pacs">more detailed explanation</a>.',
        'title': 'Political action committee summaries',
        'PAGINATE_BY': PAGINATE_BY,
        'cycle_list': cycle_list
    },
                              context_instance=RequestContext(request))
Exemplo n.º 11
0
    def handle(self, *args, **options):

        for cycle in ACTIVE_CYCLES:

            cycle_details = cycle_calendar[int(cycle)]
            CYCLE_START = cycle_details['start']
            CYCLE_END = cycle_details['end']

            update_time = datetime.now()

            print "Running outside spending summaries"
            summary_obj = {}

            all_independent_expenditures = SkedE.objects.filter(
                superceded_by_amendment=False,
                effective_date__gte=CYCLE_START,
                effective_date__lte=CYCLE_END)
            summary_obj['ie_sum'] = all_independent_expenditures.aggregate(
                total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']

            print "\tpositive vs negative"
            # postive vs negative
            summary_obj['positive'] = all_independent_expenditures.filter(
                support_oppose_checked='S').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']
            summary_obj['negative'] = all_independent_expenditures.filter(
                support_oppose_checked='O').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']

            print "\ttarget party breakdown"
            # breakdown by target party
            summary_obj['pro_dem'] = all_independent_expenditures.filter(
                support_oppose_checked='S',
                candidate_party_checked='D').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']
            summary_obj['pro_rep'] = all_independent_expenditures.filter(
                support_oppose_checked='S',
                candidate_party_checked='R').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']
            summary_obj['anti_dem'] = all_independent_expenditures.filter(
                support_oppose_checked='O',
                candidate_party_checked='D').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']
            summary_obj['anti_rep'] = all_independent_expenditures.filter(
                support_oppose_checked='O',
                candidate_party_checked='R').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']

            print "\toffice breakdown"
            # breakdown by target party
            summary_obj['senate'] = all_independent_expenditures.filter(
                candidate_office_checked='S').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']
            summary_obj['house'] = all_independent_expenditures.filter(
                candidate_office_checked='H').aggregate(total_expenditures=Sum(
                    'expenditure_amount'))['total_expenditures']

            all_outside_spenders = Committee_Overlay.nulls_last_objects.filter(
                total_indy_expenditures__gt=0, cycle=str(cycle))
            print "\toutside spending types"
            # breakdown by types:
            summary_obj['party_committees'] = all_outside_spenders.filter(
                ctype__in=('X', 'Y', 'Z')).aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            # includes hybrids
            summary_obj['super_pacs'] = all_outside_spenders.filter(
                ctype__in=('O', 'U', 'V', 'W')).aggregate(
                    total_expenditures=Sum(
                        'total_indy_expenditures'))['total_expenditures']
            # non-committees:
            summary_obj['non_committees'] = all_outside_spenders.filter(
                ctype__in=('I')).aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            summary_obj['oth_committees'] = all_outside_spenders.filter(
                ctype__in=('N', 'Q')).aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            summary_obj['house_committees'] = all_outside_spenders.filter(
                ctype__in=('H')).aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            summary_obj['senate_committees'] = all_outside_spenders.filter(
                ctype__in=('S')).aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']

            print "\toutside spending parties"
            # breakdown by parties
            summary_obj['dem_affil'] = all_outside_spenders.filter(
                political_orientation='D').aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            summary_obj['rep_affil'] = all_outside_spenders.filter(
                political_orientation='R').aggregate(total_expenditures=Sum(
                    'total_indy_expenditures'))['total_expenditures']
            summary_obj['no_affil'] = all_outside_spenders.exclude(
                political_orientation__in=('R', 'D')).aggregate(
                    total_expenditures=Sum(
                        'total_indy_expenditures'))['total_expenditures']

            ## write the outside spending overview

            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [
                cycle_fake(cycle, "/overview/outside-money/%s/" % cycle),
                cycle_fake(other_year,
                           "/overview/outside-money/%s/" % other_year)
            ]

            page_title = "Independent Expenditures, %s Cycle" % cycle
            c = Context({
                "update_time": update_time,
                "sums": summary_obj,
                "page_title": page_title,
                "cycle_list": cycle_list,
                "cycle_start": CYCLE_START,
                "cycle_end": CYCLE_END
            })
            this_template = get_template(
                'generated_pages/overview_outside_money.html')
            result = this_template.render(c)
            thisurl = "/overview/outside-money/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html"  # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

            thisflatpage.save()

            ### end outside spending part.

            ## deal with the inside spending

            # assumes fake committees have been removed; disregard joint fundraisers which disburse their proceeds to their recipients

            print "Running main overview summaries"

            all_webk = WebK.objects.filter(cycle=cycle).exclude(com_des='J')
            summary_types = [{
                'name': 'Super PACs',
                'code': 'UOVW',
                'outside_spending': summary_obj['super_pacs']
            }, {
                'name':
                'Party Committees',
                'code':
                'XYZ',
                'outside_spending':
                summary_obj['party_committees']
            }, {
                'name':
                'House Candidate Committees',
                'code':
                'H',
                'outside_spending':
                summary_obj['house_committees']
            }, {
                'name':
                'Senate Candidate Committees',
                'code':
                'S',
                'outside_spending':
                summary_obj['senate_committees']
            }, {
                'name': 'Other PACs',
                'code': 'NQ',
                'outside_spending': summary_obj['oth_committees']
            }]
            for s in summary_types:
                code_list = [i for i in s['code']]
                sums = all_webk.filter(com_typ__in=code_list).aggregate(
                    tot_rec=Sum('tot_rec'),
                    tot_dis=Sum('tot_dis'),
                    par_com_con=Sum('par_com_con'),
                    oth_com_con=Sum('oth_com_con'),
                    ind_ite_con=Sum('ind_ite_con'),
                    ind_uni_con=Sum('ind_uni_con'),
                    fed_can_com_con=Sum('fed_can_com_con'),
                    tot_ope_exp=Sum('tot_ope_exp'),
                    ope_exp=Sum('ope_exp'))
                s['tot_dis'] = sums['tot_dis'] or 0
                s['tot_rec'] = sums['tot_rec'] or 0
                s['oth_com_con'] = (sums['oth_com_con']
                                    or 0) + (sums['par_com_con'] or 0)
                s['ind_ite_con'] = sums['ind_ite_con'] or 0
                s['ind_uni_con'] = sums['ind_uni_con'] or 0
                s['fed_can_com_con'] = sums['fed_can_com_con'] or 0

                # operating expenses are recorded differently for candidate pacs
                if s['code'] in ['H', 'S']:
                    s['tot_ope_exp'] = sums['ope_exp'] or 0
                else:
                    s['tot_ope_exp'] = sums['tot_ope_exp'] or 0

            ## reuse this stuff as is in noncommittees
            all_noncommittees = Committee_Overlay.objects.filter(
                ctype__in=['I'], cycle=cycle).exclude(designation='J')
            sums = all_noncommittees.aggregate(
                tot_ie=Sum('total_indy_expenditures'))
            ##
            dark_money_total_ies = sums['tot_ie']
            summary_types.append({
                'name': 'Dark Money',
                'code': 'I',
                'outside_spending': sums['tot_ie'],
                'tot_dis': 0,
                'tot_rec': 0,
                'oth_com_con': 0,
                'ind_ite_con': 0,
                'ind_uni_con': 0,
                'fed_can_com_con': 0,
                'tot_ope_exp': 0
            })

            print "\toverview main sums: %s" % summary_obj
            print "\toverview inside money: %s" % summary_types

            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [
                cycle_fake(cycle, "/overview/%s/" % cycle),
                cycle_fake(other_year, "/overview/%s/" % other_year)
            ]

            page_title = "Cycle Overview, %s Cycle" % cycle
            c = Context({
                "update_time": update_time,
                "sums": summary_obj,
                "inside_money": summary_types,
                "page_title": page_title,
                "cycle_list": cycle_list,
                "cycle_start": CYCLE_START,
                "cycle_end": CYCLE_END
            })

            this_template = get_template('generated_pages/overview_main.html')
            result = this_template.render(c)

            thisurl = "/overview/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html"  # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

            thisflatpage.save()

            # now superpacs
            print "Now running superpac summaries... "

            sp_summary_types = [{
                'name': 'Super PACs',
                'code': 'UO'
            }, {
                'name': 'Hybrid Super PACs',
                'code': 'VW'
            }, {
                'name': 'All Super PACs',
                'code': 'UOVW'
            }]
            all_superpacs = Committee_Overlay.objects.filter(
                cycle=cycle, ctype__in=['U', 'O', 'V',
                                        'W']).exclude(designation='J')

            for s in sp_summary_types:
                code_list = [i for i in s['code']]
                sums = all_superpacs.filter(ctype__in=code_list).aggregate(
                    tot_ie=Sum('total_indy_expenditures'),
                    tot_rec=Sum('total_receipts'),
                    coh=Sum('cash_on_hand'))
                s['tot_ie'] = sums['tot_ie'] or 0
                s['tot_rec'] = sums['tot_rec'] or 0
                s['coh'] = sums['coh'] or 0

            top_superpacs = all_superpacs.order_by(
                '-total_indy_expenditures')[:20]

            page_title = "Top Super PACs by Independent Expenditures, %s Cycle" % (
                cycle)

            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [
                cycle_fake(cycle, "/overview/super-pacs/%s/" % cycle),
                cycle_fake(other_year, "/overview/super-pacs/%s/" % other_year)
            ]

            c = Context({
                "update_time": update_time,
                "sums": sp_summary_types,
                "top_superpacs": top_superpacs,
                "page_title": page_title,
                "cycle_list": cycle_list,
                "cycle_start": CYCLE_START,
                "cycle_end": CYCLE_END
            })
            this_template = get_template(
                'generated_pages/overview_superpac.html')
            result = this_template.render(c)

            thisurl = "/overview/super-pacs/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html"  # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

            thisflatpage.save()

            print "Regenerating dark money pages"
            # now dark money groups -- the sums were calculated previously

            top_noncommittees = all_noncommittees.order_by(
                '-total_indy_expenditures')[:10]

            page_title = "Top Dark Money groups by Independent Expenditures, %s Cycle" % (
                cycle)

            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [
                cycle_fake(cycle, "/overview/dark-money/%s/" % cycle),
                cycle_fake(other_year, "/overview/dark-money/%s/" % other_year)
            ]

            c = Context({
                "update_time": update_time,
                "dark_money_total_ies": dark_money_total_ies,
                "top_darkmoneyers": top_noncommittees,
                "page_title": page_title,
                "cycle_list": cycle_list,
                "cycle_start": CYCLE_START,
                "cycle_end": CYCLE_END
            })
            this_template = get_template(
                'generated_pages/overview_dark_money.html')
            result = this_template.render(c)

            thisurl = "/overview/dark-money/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html"  # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

            thisflatpage.save()

            ## do connected pacs now.

            all_webk = WebK.objects.filter(cycle=cycle).exclude(com_des='J')

            connected_org_types = [{
                'name': 'Corporation',
                'code': 'C'
            }, {
                'name': 'Labor organization',
                'code': 'L'
            }, {
                'name': 'Member Organization',
                'code': 'M'
            }, {
                'name': 'Cooperative',
                'code': 'V'
            }, {
                'name': 'Trade Association',
                'code': 'T'
            }, {
                'name': 'Corporation without capital stock',
                'code': 'W'
            }]

            for j in connected_org_types:
                committees = Committee.objects.filter(cmte_tp__in=['N', 'Q'],
                                                      cycle=str(cycle),
                                                      org_tp=j['code'])
                committee_id_list = [i.cmte_id for i in committees]

                sums = all_webk.filter(com_id__in=committee_id_list).aggregate(
                    tot_rec=Sum('tot_rec'),
                    tot_dis=Sum('tot_dis'),
                    par_com_con=Sum('par_com_con'),
                    oth_com_con=Sum('oth_com_con'),
                    ind_ite_con=Sum('ind_ite_con'),
                    ind_uni_con=Sum('ind_uni_con'),
                    fed_can_com_con=Sum('fed_can_com_con'),
                    tot_ope_exp=Sum('tot_ope_exp'),
                    ope_exp=Sum('ope_exp'))
                j['tot_dis'] = sums['tot_dis'] or 0
                j['tot_rec'] = sums['tot_rec'] or 0
                j['oth_com_con'] = (sums['oth_com_con']
                                    or 0) + (sums['par_com_con'] or 0)
                j['ind_ite_con'] = sums['ind_ite_con'] or 0
                j['ind_uni_con'] = sums['ind_uni_con'] or 0
                j['fed_can_com_con'] = sums['fed_can_com_con'] or 0
                j['tot_ope_exp'] = sums['tot_ope_exp'] or 0

            print "building connected pac with inside money set to %s" % (
                connected_org_types)

            page_title = "Cycle Overview, %s Cycle -- Connected pacs" % cycle
            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [
                cycle_fake(cycle, "/overview/connected/%s/" % cycle),
                cycle_fake(other_year, "/overview/connected/%s/" % other_year)
            ]

            c = Context({
                "update_time": update_time,
                "inside_money": connected_org_types,
                "page_title": page_title,
                "cycle_list": cycle_list,
                "cycle_start": CYCLE_START,
                "cycle_end": CYCLE_END
            })
            this_template = get_template(
                'generated_pages/overview_connected.html')
            result = this_template.render(c)
            thisurl = "/overview/connected/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html"  # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

            thisflatpage.save()
Exemplo n.º 12
0
    def handle(self, *args, **options):
        
        for cycle in ACTIVE_CYCLES:
            
            cycle_details = cycle_calendar[int(cycle)]
            CYCLE_START = cycle_details['start']
            CYCLE_END = cycle_details['end']
        
            update_time = datetime.now()
        
            print "Running outside spending summaries"
            summary_obj = {}
        
            all_independent_expenditures = SkedE.objects.filter(superceded_by_amendment=False, effective_date__gte=CYCLE_START, effective_date__lte=CYCLE_END)
            summary_obj['ie_sum'] = all_independent_expenditures.aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
        
            print "\tpositive vs negative"
            # postive vs negative
            summary_obj['positive'] = all_independent_expenditures.filter(support_oppose_checked='S').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
            summary_obj['negative'] = all_independent_expenditures.filter(support_oppose_checked='O').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']

            print "\ttarget party breakdown"
            # breakdown by target party
            summary_obj['pro_dem'] = all_independent_expenditures.filter(support_oppose_checked='S', candidate_party_checked='D').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
            summary_obj['pro_rep'] = all_independent_expenditures.filter(support_oppose_checked='S', candidate_party_checked='R').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
            summary_obj['anti_dem'] = all_independent_expenditures.filter(support_oppose_checked='O', candidate_party_checked='D').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
            summary_obj['anti_rep'] = all_independent_expenditures.filter(support_oppose_checked='O', candidate_party_checked='R').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
        
            print "\toffice breakdown"
            # breakdown by target party
            summary_obj['senate'] = all_independent_expenditures.filter(candidate_office_checked='S').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
            summary_obj['house'] = all_independent_expenditures.filter(candidate_office_checked='H').aggregate(total_expenditures=Sum('expenditure_amount'))['total_expenditures']
       
        
            all_outside_spenders = Committee_Overlay.nulls_last_objects.filter(total_indy_expenditures__gt=0, cycle=str(cycle))
            print "\toutside spending types"
            # breakdown by types:
            summary_obj['party_committees'] = all_outside_spenders.filter(ctype__in=('X', 'Y', 'Z')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            # includes hybrids
            summary_obj['super_pacs'] = all_outside_spenders.filter(ctype__in=('O', 'U', 'V', 'W')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            # non-committees:
            summary_obj['non_committees'] = all_outside_spenders.filter(ctype__in=('I')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            summary_obj['oth_committees'] = all_outside_spenders.filter(ctype__in=('N', 'Q')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            summary_obj['house_committees'] = all_outside_spenders.filter(ctype__in=('H')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            summary_obj['senate_committees'] = all_outside_spenders.filter(ctype__in=('S')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
        
        
        
            print "\toutside spending parties"
            # breakdown by parties 
            summary_obj['dem_affil'] = all_outside_spenders.filter(political_orientation='D').aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            summary_obj['rep_affil'] = all_outside_spenders.filter(political_orientation='R').aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
            summary_obj['no_affil'] = all_outside_spenders.exclude(political_orientation__in=('R', 'D')).aggregate(total_expenditures=Sum('total_indy_expenditures'))['total_expenditures']
        
        
        
            ## write the outside spending overview

            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [cycle_fake(cycle, "/overview/outside-money/%s/" % cycle), cycle_fake(other_year, "/overview/outside-money/%s/" % other_year)]


            page_title = "Independent Expenditures, %s Cycle" % cycle    
            c = Context({"update_time": update_time, "sums": summary_obj, "page_title":page_title, "cycle_list":cycle_list, "cycle_start":CYCLE_START, "cycle_end":CYCLE_END})
            this_template = get_template('generated_pages/overview_outside_money.html')
            result = this_template.render(c)
            thisurl = "/overview/outside-money/%s/" % cycle
            
            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)            
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html" # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)
                
            thisflatpage.save()
            
            
            ### end outside spending part. 

            
            ## deal with the inside spending
        
            # assumes fake committees have been removed; disregard joint fundraisers which disburse their proceeds to their recipients
            
            print "Running main overview summaries" 
            
            all_webk = WebK.objects.filter(cycle=cycle).exclude(com_des='J')
            summary_types = [
                {'name':'Super PACs', 'code':'UOVW', 'outside_spending': summary_obj['super_pacs']},
                {'name':'Party Committees', 'code':'XYZ', 'outside_spending': summary_obj['party_committees']},
                {'name':'House Candidate Committees', 'code':'H', 'outside_spending': summary_obj['house_committees']},
                {'name':'Senate Candidate Committees', 'code':'S', 'outside_spending': summary_obj['senate_committees']},
                {'name':'Other PACs', 'code':'NQ', 'outside_spending': summary_obj['oth_committees']}
            ]
            for s in summary_types:
                code_list = [i for i in s['code']]
                sums = all_webk.filter(com_typ__in=code_list).aggregate(tot_rec=Sum('tot_rec'), tot_dis=Sum('tot_dis'), par_com_con=Sum('par_com_con'), oth_com_con=Sum('oth_com_con'), ind_ite_con=Sum('ind_ite_con'), ind_uni_con=Sum('ind_uni_con'), fed_can_com_con=Sum('fed_can_com_con'), tot_ope_exp=Sum('tot_ope_exp'), ope_exp=Sum('ope_exp'))
                s['tot_dis'] = sums['tot_dis'] or 0
                s['tot_rec'] = sums['tot_rec'] or 0
                s['oth_com_con'] = (sums['oth_com_con'] or 0) + (sums['par_com_con'] or 0)
                s['ind_ite_con']= sums['ind_ite_con'] or 0
                s['ind_uni_con'] = sums['ind_uni_con'] or 0
                s['fed_can_com_con'] = sums['fed_can_com_con'] or 0
            
                # operating expenses are recorded differently for candidate pacs
                if s['code'] in ['H', 'S']:
                    s['tot_ope_exp'] = sums['ope_exp'] or 0
                else: 
                    s['tot_ope_exp'] = sums['tot_ope_exp'] or 0
        
        
            ## reuse this stuff as is in noncommittees
            all_noncommittees = Committee_Overlay.objects.filter(ctype__in=['I'], cycle=cycle).exclude(designation='J')       
            sums = all_noncommittees.aggregate(tot_ie=Sum('total_indy_expenditures'))
            ##
            dark_money_total_ies = sums['tot_ie']
            summary_types.append({'name':'Dark Money', 'code':'I', 'outside_spending': sums['tot_ie'], 'tot_dis':0, 'tot_rec':0, 'oth_com_con':0, 'ind_ite_con':0, 'ind_uni_con':0, 'fed_can_com_con':0, 'tot_ope_exp':0})
        
            print "\toverview main sums: %s" % summary_obj
            print "\toverview inside money: %s" % summary_types


            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [cycle_fake(cycle, "/overview/%s/" % cycle), cycle_fake(other_year, "/overview/%s/" % other_year)]



            page_title = "Cycle Overview, %s Cycle" % cycle    
            c = Context({"update_time": update_time, "sums": summary_obj, "inside_money": summary_types, "page_title":page_title, "cycle_list":cycle_list, "cycle_start":CYCLE_START, "cycle_end":CYCLE_END})
            
            this_template = get_template('generated_pages/overview_main.html')
            result = this_template.render(c)
            
            thisurl = "/overview/%s/" % cycle
            
            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)            
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html" # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

                
            thisflatpage.save()
        
        
            # now superpacs
            print "Now running superpac summaries... "
            
            sp_summary_types = [
                {'name':'Super PACs', 'code':'UO'},
                {'name':'Hybrid Super PACs', 'code':'VW'},
                {'name': 'All Super PACs', 'code':'UOVW'}
            ]
            all_superpacs = Committee_Overlay.objects.filter(cycle=cycle,ctype__in=['U', 'O', 'V', 'W']).exclude(designation='J')
        
            for s in sp_summary_types:
                code_list = [i for i in s['code']]
                sums = all_superpacs.filter(ctype__in=code_list).aggregate(tot_ie=Sum('total_indy_expenditures'), tot_rec=Sum('total_receipts'), coh=Sum('cash_on_hand'))
                s['tot_ie'] = sums['tot_ie'] or 0
                s['tot_rec'] = sums['tot_rec'] or 0
                s['coh'] = sums['coh'] or 0
        
        
            top_superpacs = all_superpacs.order_by('-total_indy_expenditures')[:20]
            
            page_title = "Top Super PACs by Independent Expenditures, %s Cycle" % (cycle)
            
            
            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [cycle_fake(cycle, "/overview/super-pacs/%s/" % cycle), cycle_fake(other_year, "/overview/super-pacs/%s/" % other_year)]
            
            c = Context({"update_time": update_time, "sums": sp_summary_types, "top_superpacs": top_superpacs, "page_title":page_title, "cycle_list":cycle_list, "cycle_start":CYCLE_START, "cycle_end":CYCLE_END})
            this_template = get_template('generated_pages/overview_superpac.html')
            result = this_template.render(c)
        
            thisurl = "/overview/super-pacs/%s/" % cycle
            
            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)            
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html" # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)

                
            thisflatpage.save()
                
        
            print "Regenerating dark money pages"
            # now dark money groups -- the sums were calculated previously 


            top_noncommittees = all_noncommittees.order_by('-total_indy_expenditures')[:10]
        
            page_title = "Top Dark Money groups by Independent Expenditures, %s Cycle" % (cycle)
            
            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [cycle_fake(cycle, "/overview/dark-money/%s/" % cycle), cycle_fake(other_year, "/overview/dark-money/%s/" % other_year)]
            
            
            c = Context({"update_time": update_time, "dark_money_total_ies": dark_money_total_ies, "top_darkmoneyers": top_noncommittees, "page_title":page_title, "cycle_list":cycle_list, "cycle_start":CYCLE_START, "cycle_end":CYCLE_END})
            this_template = get_template('generated_pages/overview_dark_money.html')
            result = this_template.render(c)
            
            thisurl = "/overview/dark-money/%s/" % cycle

            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)            
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html" # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)


            thisflatpage.save()
            
            
            
            ## do connected pacs now.
        
            all_webk = WebK.objects.filter(cycle=cycle).exclude(com_des='J')

            connected_org_types = [
                {'name':'Corporation', 'code':'C'},
                {'name':'Labor organization', 'code':'L'},
                {'name':'Member Organization', 'code':'M'},
                {'name':'Cooperative', 'code':'V'},
                {'name':'Trade Association', 'code':'T'},
                {'name':'Corporation without capital stock', 'code':'W'}
            ]

            for j in connected_org_types:
                committees = Committee.objects.filter(cmte_tp__in=['N', 'Q'], cycle=str(cycle),org_tp=j['code'])
                committee_id_list = [i.cmte_id for i in committees]

                sums = all_webk.filter(com_id__in=committee_id_list).aggregate(tot_rec=Sum('tot_rec'), tot_dis=Sum('tot_dis'), par_com_con=Sum('par_com_con'), oth_com_con=Sum('oth_com_con'), ind_ite_con=Sum('ind_ite_con'), ind_uni_con=Sum('ind_uni_con'), fed_can_com_con=Sum('fed_can_com_con'), tot_ope_exp=Sum('tot_ope_exp'), ope_exp=Sum('ope_exp'))
                j['tot_dis'] = sums['tot_dis'] or 0
                j['tot_rec'] = sums['tot_rec'] or 0
                j['oth_com_con'] = (sums['oth_com_con'] or 0) + (sums['par_com_con'] or 0)
                j['ind_ite_con']= sums['ind_ite_con'] or 0
                j['ind_uni_con'] = sums['ind_uni_con'] or 0
                j['fed_can_com_con'] = sums['fed_can_com_con'] or 0
                j['tot_ope_exp'] = sums['tot_ope_exp'] or 0
         
            print "building connected pac with inside money set to %s" % (connected_org_types)
            
            page_title = "Cycle Overview, %s Cycle -- Connected pacs" % cycle
            other_year = None
            if cycle == '2016':
                other_year = '2014'
            elif cycle == '2014':
                other_year = '2016'
            cycle_list = [cycle_fake(cycle, "/overview/connected/%s/" % cycle), cycle_fake(other_year, "/overview/connected/%s/" % other_year)]
            
            
            c = Context({"update_time": update_time, "inside_money": connected_org_types, "page_title":page_title, "cycle_list":cycle_list, "cycle_start":CYCLE_START, "cycle_end":CYCLE_END})
            this_template = get_template('generated_pages/overview_connected.html')
            result = this_template.render(c)
            thisurl = "/overview/connected/%s/" % cycle
            
            thisflatpage, created = FlatPage.objects.get_or_create(url=thisurl)            
            thisflatpage.title = page_title
            thisflatpage.content = result
            thisflatpage.template_name = "flatpages/cycle_enabled_base.html" # Default for admin flatpages too
            if not thisflatpage.sites.all():
                thisflatpage.sites.add(this_site)


            thisflatpage.save()