<s> from dateutil . parser import parse as dateparse <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . db . models import Sum , Max , Min <EOL> from formdata . models import SkedA , SkedE <EOL> from fec_alerts . models import new_filing <EOL> from formdata . utils . fec_import_logging import fec_logger <EOL> from django . db import connection <EOL> def mark_superceded_F24s ( new_f3x_new_filing ) : <EOL> coverage_from_date = new_f3x_new_filing . coverage_from_date <EOL> coverage_through_date = new_f3x_new_filing . coverage_to_date <EOL> raw_filer_id = new_f3x_new_filing . fec_id <EOL> filing_numbers = new_filing . objects . filter ( fec_id = raw_filer_id , form_type__startswith = '<STR_LIT>' ) . values ( '<STR_LIT>' ) <EOL> filing_array = [ ] <EOL> for i in filing_numbers : <EOL> filing_array . append ( i [ '<STR_LIT>' ] ) <EOL> updated = SkedE . objects . filter ( filing_number__in = filing_array , superceded_by_amendment = False , expenditure_date_formatted__gte = coverage_from_date , expenditure_date_formatted__lte = coverage_through_date ) . update ( superceded_by_amendment = True ) <EOL> if updated : <EOL> print "<STR_LIT>" % updated <EOL> def mark_superceded_F65s ( new_f3x_new_filing ) : <EOL> coverage_from_date = new_f3x_new_filing . coverage_from_date <EOL> coverage_through_date = new_f3x_new_filing . coverage_to_date <EOL> raw_filer_id = new_f3x_new_filing . fec_id <EOL> filing_numbers = new_filing . objects . filter ( fec_id = raw_filer_id , form_type__startswith = '<STR_LIT>' ) . values ( '<STR_LIT>' ) <EOL> filing_array = [ ] <EOL> for i in filing_numbers : <EOL> filing_array . append ( i [ '<STR_LIT>' ] ) <EOL> updated = SkedA . objects . filter ( filing_number__in = filing_array , superceded_by_amendment = False , contribution_date__gte = coverage_from_date , contribution_date__lte = coverage_through_date ) . update ( superceded_by_amendment = True ) <EOL> if updated : <EOL> print "<STR_LIT>" % updated <EOL> def mark_superceded_F57s ( new_monthly_f5 ) : <EOL> print "<STR_LIT>" <EOL> coverage_from_date = new_monthly_f5 . coverage_from_date <EOL> coverage_through_date = new_monthly_f5 . coverage_to_date <EOL> raw_filer_id = new_monthly_f5 . fec_id <EOL> filing_numbers = new_filing . objects . filter ( fec_id = raw_filer_id , is_f5_quarterly = False ) . values ( '<STR_LIT>' ) <EOL> filing_array = [ ] <EOL> for i in filing_numbers : <EOL> filing_array . append ( i [ '<STR_LIT>' ] ) <EOL> updated = SkedE . objects . filter ( form_type__istartswith = '<STR_LIT>' , filing_number__in = filing_array , superceded_by_amendment = False , expenditure_date_formatted__gte = coverage_from_date , expenditure_date_formatted__lte = coverage_through_date ) . update ( superceded_by_amendment = True ) <EOL> if updated : <EOL> print "<STR_LIT>" % updated <EOL> def summarize_f24 ( new_filing ) : <EOL> filing_ies = SkedE . objects . filter ( filing_number = new_filing . filing_number ) <EOL> results = filing_ies . aggregate ( tot_spent = Sum ( '<STR_LIT>' ) , start_date = Min ( '<STR_LIT>' ) , end_date = Max ( '<STR_LIT>' ) ) <EOL> if results : <EOL> new_filing . tot_spent = results [ '<STR_LIT>' ] <EOL> new_filing . tot_ies = results [ '<STR_LIT>' ] <EOL> new_filing . coverage_from_date = results [ '<STR_LIT>' ] <EOL> new_filing . coverage_to_date = results [ '<STR_LIT>' ] <EOL> new_filing . save ( ) <EOL> def summarize_f6 ( new_filing ) : <EOL> filing_skeda = SkedA . objects . filter ( filing_number = new_filing . filing_number ) <EOL> results = filing_skeda . aggregate ( tot_raised = Sum ( '<STR_LIT>' ) , start_date = Min ( '<STR_LIT>' ) , end_date = Max ( '<STR_LIT>' ) ) <EOL> if results : <EOL> new_filing . tot_raised = results [ '<STR_LIT>' ] <EOL> new_filing . coverage_from_date = results [ '<STR_LIT>' ] <EOL> new_filing . coverage_to_date = results [ '<STR_LIT>' ] <EOL> new_filing . save ( ) <EOL> def summarize_nonquarterly_f5 ( new_filing ) : <EOL> filing_ies = SkedE . objects . filter ( filing_number = new_filing . filing_number ) <EOL> results = filing_ies . aggregate ( start_date = Min ( '<STR_LIT>' ) , end_date = Max ( '<STR_LIT>' ) ) <EOL> new_filing . coverage_from_date = results [ '<STR_LIT>' ] <EOL> new_filing . coverage_to_date = results [ '<STR_LIT>' ] <EOL> new_filing . save ( ) <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> requires_model_validation = False <EOL> def handle ( self , * args , ** options ) : <EOL> logger = fec_logger ( ) <EOL> filings_to_process = new_filing . objects . filter ( previous_amendments_processed = True , header_is_processed = True , data_is_processed = True , body_rows_superceded = False ) . order_by ( '<STR_LIT>' ) <EOL> for this_filing in filings_to_process : <EOL> print "<STR_LIT>" % ( this_filing . filing_number ) <EOL> if this_filing . form_type . upper ( ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> summarize_f24 ( this_filing ) <EOL> elif this_filing . form_type . upper ( ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> summarize_f6 ( this_filing ) <EOL> elif this_filing . form_type . startswith ( '<STR_LIT>' ) and not this_filing . is_f5_quarterly : <EOL> summarize_nonquarterly_f5 ( this_filing ) <EOL> elif this_filing . form_type . startswith ( '<STR_LIT>' ) and this_filing . is_f5_quarterly : <EOL> mark_superceded_F57s ( this_filing ) <EOL> elif this_filing . form_type . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> this_filing . lines_present [ '<STR_LIT:E>' ] <EOL> mark_superceded_F24s ( this_filing ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> this_filing . lines_present [ '<STR_LIT:A>' ] <EOL> mark_superceded_F65s ( this_filing ) <EOL> except KeyError : <EOL> pass <EOL> if not this_filing . cycle : <EOL> this_filing . set_cycle ( save_now = False ) <EOL> this_filing . body_rows_superceded = True <EOL> this_filing . save ( ) </s>
