# Path: metrics/models.py
# class ProgramCorrection(models.Model):
#     
#     program = models.ForeignKey('cfda.Program', null=False, blank=False)
#     program_obligation = models.ForeignKey('cfda.ProgramObligation', null=False, blank=False)
#     correction_date = models.DateTimeField(null=False, blank=False)
#     corrected_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     old_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     note = models.TextField("Notes on Correction", blank=True, null=True)
# 
# class USASpendingAggregate(models.Model):
#     fiscal_year = models.IntegerField(blank=False, null=False, primary_key=True)
#     total_federal_funding = models.DecimalField("SUM(fed_funding_amount)", max_digits=21, decimal_places=2, null=False)
# 
#     def __unicode__(self):
#         return "<USASpendingAggregate(%d, %s)>" % (self.fiscal_year, unicode(pretty_money(self.total_federal_funding)))


from settings import *
from cfda.models import *
from metrics.models import ProgramCorrection, USASpendingAggregate
from datetime import datetime
import pg
import csv
import numpy as np
            elif 0 < int(input) <= len(outliers):
                program = outliers[int(input)-1]
                new_obligation = raw_input("Enter the new CFDA obligation for %s: " % program.program.program_title)
                if int(new_obligation) > program.obligation:
                    update_obligation(new_obligation, program)
                    print "%s obligation updated to %s" % (program.program.program_title, new_obligation)
                else:
                    confirm = raw_input('amount entered is less than original obligation. Continue anyway?(y or n): ')
                    if confirm == 'y':
                        update_obligation(new_obligation, program)
                    else:
                        continue
                    

def update_obligation(new_obligation, program):
    
    old_obligation = program.obligation
    program.obligation = int(new_obligation)
    program.delta = program.usaspending_obligation - program.obligation
    try:
        program.weighted_delta = program.delta / program.obligation
    except Exception:
        if program.obligation == 0:
            if not program.usaspending_obligation:
                program.weighted_delta = 0
            else:
                program.weighted_delta = 1
    program.corrected = True
    program.save()


# Path: metrics/models.py
# class ProgramCorrection(models.Model):
#     
#     program = models.ForeignKey('cfda.Program', null=False, blank=False)
#     program_obligation = models.ForeignKey('cfda.ProgramObligation', null=False, blank=False)
#     correction_date = models.DateTimeField(null=False, blank=False)
#     corrected_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     old_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     note = models.TextField("Notes on Correction", blank=True, null=True)
# 
# class USASpendingAggregate(models.Model):
#     fiscal_year = models.IntegerField(blank=False, null=False, primary_key=True)
#     total_federal_funding = models.DecimalField("SUM(fed_funding_amount)", max_digits=21, decimal_places=2, null=False)
# 
#     def __unicode__(self):
#         return "<USASpendingAggregate(%d, %s)>" % (self.fiscal_year, unicode(pretty_money(self.total_federal_funding)))


# Path: consistency/import_usaspending.py
from settings import *
from cfda.models import *
from metrics.models import ProgramCorrection, USASpendingAggregate
from datetime import datetime
import pg
import csv
import numpy as np

            elif 0 < int(input) <= len(outliers):
                program = outliers[int(input)-1]
                new_obligation = raw_input("Enter the new CFDA obligation for %s: " % program.program.program_title)
                if int(new_obligation) > program.obligation:
                    update_obligation(new_obligation, program)
                    print "%s obligation updated to %s" % (program.program.program_title, new_obligation)
                else:
                    confirm = raw_input('amount entered is less than original obligation. Continue anyway?(y or n): ')
                    if confirm == 'y':
                        update_obligation(new_obligation, program)
                    else:
                        continue
                    

def update_obligation(new_obligation, program):
    
    old_obligation = program.obligation
    program.obligation = int(new_obligation)
    program.delta = program.usaspending_obligation - program.obligation
    try:
        program.weighted_delta = program.delta / program.obligation
    except Exception:
        if program.obligation == 0:
            if not program.usaspending_obligation:
                program.weighted_delta = 0
            else:
                program.weighted_delta = 1
    program.corrected = True
    program.save()


    correction = ProgramCorrection(program=program.program, program_obligation=program, corrected_obligation=program.obligation, old_obligation=old_obligation, note="Corrected manually in program run",  correction_date=datetime.today())