<s> import csv <EOL> from datetime import datetime , date <EOL> from django . conf import settings <EOL> from django . db . models import Sum <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from formdata . models import SkedE <EOL> from summary_data . models import Committee_Overlay <EOL> from datetime import date <EOL> from summary_data . utils . weekly_update_utils import get_week_number , get_week_end , summarize_week_queryset , summarize_week_queryset_cumulative <EOL> CHART_CSV_DOWNLOAD_DIR = settings . CHART_CSV_DOWNLOAD_DIR <EOL> CSV_FILE_NAME = '<STR_LIT>' <EOL> CSV_FILE_NAME_CUMULATIVE = '<STR_LIT>' <EOL> data_start = date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> write_cumulative = False <EOL> def make_header ( start_week_number , end_week_number ) : <EOL> headers = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for week in range ( start_week_number , end_week_number + <NUM_LIT:1> ) : <EOL> headers . append ( get_week_end ( week ) . strftime ( "<STR_LIT>" ) ) <EOL> return headers <EOL> all_ies = SkedE . objects . filter ( superceded_by_amendment = False ) <EOL> all_outside_groups = Committee_Overlay . objects . filter ( total_indy_expenditures__gt = <NUM_LIT:0> ) <EOL> dem_id_list = [ i . fec_id for i in all_outside_groups . filter ( political_orientation = '<STR_LIT:D>' ) ] <EOL> rep_id_list = [ i . fec_id for i in all_outside_groups . filter ( political_orientation = '<STR_LIT:R>' ) ] <EOL> noncommittees = Committee_Overlay . objects . filter ( ctype__in = [ '<STR_LIT:I>' ] , total_indy_expenditures__gt = <NUM_LIT:0> ) <EOL> noncommittee_id_list = [ i . fec_id for i in noncommittees ] <EOL> dem_noncommittee_id_list = [ i . fec_id for i in noncommittees . filter ( political_orientation = '<STR_LIT:D>' ) ] <EOL> rep_noncommittee_id_list = [ i . fec_id for i in noncommittees . filter ( political_orientation = '<STR_LIT:R>' ) ] <EOL> superpacs = Committee_Overlay . objects . filter ( ctype__in = [ '<STR_LIT>' , '<STR_LIT:O>' ] , total_indy_expenditures__gt = <NUM_LIT:0> ) <EOL> superpac_id_list = [ i . fec_id for i in superpacs ] <EOL> dem_superpac_id_list = [ i . fec_id for i in superpacs . filter ( political_orientation = '<STR_LIT:D>' ) ] <EOL> rep_superpac_id_list = [ i . fec_id for i in superpacs . filter ( political_orientation = '<STR_LIT:R>' ) ] <EOL> party_committees = Committee_Overlay . objects . filter ( ctype__in = [ '<STR_LIT:X>' , '<STR_LIT:Y>' , '<STR_LIT>' ] , total_indy_expenditures__gt = <NUM_LIT:0> ) <EOL> party_committee_id_list = [ i . fec_id for i in party_committees ] <EOL> dem_party_committee_id_list = [ i . fec_id for i in party_committees . filter ( political_orientation = '<STR_LIT:D>' ) ] <EOL> rep_party_committee_id_list = [ i . fec_id for i in party_committees . filter ( political_orientation = '<STR_LIT:R>' ) ] <EOL> data_series = [ <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = noncommittee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = superpac_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = party_committee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:6> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_noncommittee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_noncommittee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_superpac_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:9> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_superpac_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_party_committee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:11> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_party_committee_id_list ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:12> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:15> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:16> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_superpac_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_superpac_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_superpac_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_superpac_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT:20> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_noncommittee_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_noncommittee_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_noncommittee_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_noncommittee_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_party_committee_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_party_committee_id_list , candidate_office_checked = '<STR_LIT:S>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = dem_party_committee_id_list , candidate_office_checked = '<STR_LIT:H>' ) } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:q>' : all_ies . filter ( filer_committee_id_number__in = rep_party_committee_id_list , candidate_office_checked = '<STR_LIT:H>' ) } <EOL> ] <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> requires_model_validation = False <EOL> def handle ( self , * args , ** options ) : <EOL> today = date ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:1> ) <EOL> last_week = get_week_number ( today ) - <NUM_LIT:1> <EOL> first_week = get_week_number ( data_start ) <EOL> outf = "<STR_LIT>" % ( CHART_CSV_DOWNLOAD_DIR , CSV_FILE_NAME ) <EOL> print "<STR_LIT>" % outf <EOL> outfile = open ( outf , '<STR_LIT:w>' ) <EOL> field_names = make_header ( first_week , last_week ) <EOL> outfile . write ( "<STR_LIT:U+002C>" . join ( field_names ) + "<STR_LIT:\n>" ) <EOL> dw = csv . writer ( outfile ) <EOL> if write_cumulative : <EOL> outcumf = "<STR_LIT>" % ( CHART_CSV_DOWNLOAD_DIR , CSV_FILE_NAME_CUMULATIVE ) <EOL> print "<STR_LIT>" % outcumf <EOL> outcumfile = open ( outcumf , '<STR_LIT:w>' ) <EOL> field_names = make_header ( first_week , last_week ) <EOL> outcumfile . write ( "<STR_LIT:U+002C>" . join ( field_names ) + "<STR_LIT:\n>" ) <EOL> cumdw = csv . writer ( outcumfile ) <EOL> for data in data_series : <EOL> this_row = [ data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ] <EOL> this_cum_row = [ data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ] <EOL> for week in range ( first_week , last_week + <NUM_LIT:1> ) : <EOL> print "<STR_LIT>" % ( data [ '<STR_LIT>' ] , week ) <EOL> this_row . append ( summarize_week_queryset ( week , data [ '<STR_LIT:q>' ] ) ) <EOL> if write_cumulative : <EOL> this_cum_row . append ( summarize_week_queryset_cumulative ( week , data [ '<STR_LIT:q>' ] ) ) <EOL> dw . writerow ( this_row ) <EOL> if write_cumulative : <EOL> cumdw . writerow ( this_cum_row ) </s>
