def handle_filing(this_filing):
    
    try:
        co = Committee_Overlay.objects.get(fec_id=this_filing.fec_id, cycle=this_filing.cycle)
        this_filing.committee_designation = co.designation
        this_filing.committee_name = co.name
        this_filing.committee_type = co.ctype
        this_filing.committee_slug = co.slug
        this_filing.party = co.party
        
        # mark that the committee is dirty
        co.is_dirty=True
        co.save()
        
    except Committee_Overlay.DoesNotExist:
        try:
            ## remember that ftpdata committees have cycles as ints, not strings. Not ideal. 
            if not this_filing.cycle:
                this_filing.cycle = CURRENT_CYCLE
            co = Committee.objects.get(cmte_id=this_filing.fec_id, cycle=int(this_filing.cycle))
            this_filing.committee_designation = co.cmte_dsgn
            this_filing.committee_type = co.cmte_tp
            this_filing.committee_name = co.cmte_name
            this_filing.party = get_party_from_pty(co.cmte_pty_affiliation)
        except Committee.DoesNotExist:
            pass
            
    
    header_data = this_filing.header_data
    
    form_type = this_filing.form_type
    parsed_data = {'coh_start':None, 'coh_end':None, 'new_loans':None,'tot_raised':None,'tot_spent':None}
    
    if form_type in ['F3A', 'F3N', 'F3T','F3PA', 'F3PN', 'F3PT', 'F3', 'F3P']:
        parsed_data = process_f3_header(header_data)
        print "got data %s" % (parsed_data)
        
        this_filing.coh_end =  parsed_data['coh_end'] if parsed_data['coh_end'] else 0
        this_filing.tot_raised = parsed_data['tot_raised'] if parsed_data['tot_raised'] else 0
        this_filing.tot_spent = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        this_filing.new_loans = parsed_data['new_loans'] if parsed_data['new_loans'] else 0
        this_filing.new_filing_details_set = True
        
    elif form_type in ['F3X', 'F3XA', 'F3XN', 'F3XT']:
        parsed_data = process_f3x_header(header_data)
        print "got data %s" % (parsed_data)
        
        this_filing.coh_end =  parsed_data['coh_end'] if parsed_data['coh_end'] else 0
        this_filing.tot_raised = parsed_data['tot_raised'] if parsed_data['tot_raised'] else 0
        this_filing.tot_spent = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        this_filing.new_loans = parsed_data['new_loans'] if parsed_data['new_loans'] else 0
        this_filing.tot_coordinated = parsed_data['tot_coordinated'] if parsed_data['tot_coordinated'] else 0
        this_filing.tot_ies = parsed_data['tot_ies'] if parsed_data['tot_ies'] else 0
        this_filing.new_filing_details_set = True
    
    
    elif form_type in ['F5', 'F5A', 'F5N']:
        parsed_data = process_f5_header(header_data)
        
        this_filing.tot_raised = parsed_data['tot_raised'] if parsed_data['tot_raised'] else 0
        this_filing.tot_spent = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        # total spending is total ies
        this_filing.tot_ies = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        this_filing.coverage_from_date = parsed_data['coverage_from_date']
        this_filing.coverage_to_date = parsed_data['coverage_to_date']
        
        try:
            this_filing.is_f5_quarterly = header_data['report_code'] in ['Q1', 'Q2', 'Q3', 'Q4', 'YE']
        except __HOLE__:
            # this is probably a problem. 
            pass
        this_filing.new_filing_details_set = True
        
        
    
    elif form_type in ['F7', 'F7A', 'F7N']:
        parsed_data = process_f7_header(header_data)
        #print "got data %s" % (parsed_data)
        this_filing.tot_raised = 0
        this_filing.tot_spent = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        this_filing.coverage_from_date = parsed_data['coverage_from_date'] if parsed_data['coverage_from_date'] else None
        this_filing.coverage_to_date = parsed_data['coverage_to_date'] if parsed_data['coverage_to_date'] else None
        this_filing.new_filing_details_set = True
        

    elif form_type in ['F9', 'F9A', 'F9N']:
        parsed_data = process_f9_header(header_data)
        
        this_filing.tot_raised = parsed_data['tot_raised'] if parsed_data['tot_raised'] else 0
        this_filing.tot_spent = parsed_data['tot_spent'] if parsed_data['tot_spent'] else 0
        this_filing.coverage_from_date = parsed_data['coverage_from_date'] if parsed_data['coverage_from_date'] else None
        this_filing.coverage_to_date = parsed_data['coverage_to_date'] if parsed_data['coverage_to_date'] else None
        this_filing.new_filing_details_set = True
        
    
    elif form_type in ['F13', 'F13A', 'F13N']:
        parsed_data = process_f13_header(header_data)
        #print "got data %s" % (parsed_data)
        
        this_filing.tot_raised = parsed_data['tot_raised'] if parsed_data['tot_raised'] else 0
        this_filing.coverage_from_date = parsed_data['coverage_from_date'] if parsed_data['coverage_from_date'] else None
        this_filing.coverage_to_date = parsed_data['coverage_to_date'] if parsed_data['coverage_to_date'] else None
        this_filing.new_filing_details_set = True
        
    else:
        # Nothing to be done, but mark this step as done. 
        this_filing.new_filing_details_set = True
        
    
    this_filing.save()
dataset/ETHPy150Open sunlightlabs/read_FEC/fecreader/fec_alerts/management/commands/set_new_filing_details.py/handle_filing