from django import db
from django.conf import settings
from django.core.management.base import NoArgsCommand
from data.models import StateLaborForceParticipation
import csv

# National Priorities Project Data Repository
# import_state_labor_force_participation.py
# Updated 6/30/2010, Barb Chalfonte, National Priorities Project

# Imports BLS State Labor Force Participation
# source info: http://www.bls.gov/lau/#tables (accurate as of 6/30/2010)
# npp csv: http://assets.nationalpriorities.org/raw_data/bls.gov/state_labor_force_participation.csv (updated 6/30/2010)
# destination model:  StateLaborForceParticipation

# HOWTO:
# 1) Download source files from url listed above
# 2) Convert source file to .csv with same formatting as npp csv
# 3) change SOURCE_FILE variable to the the path of the source file you just created
# 5) Run as Django management command from your project path "python manage.py import_state_labor_force_participation"

SOURCE_FILE = '%s/bls.gov/state_labor_force_participation.csv' % (settings.LOCAL_DATA_ROOT)

class Command(NoArgsCommand):
    
    def handle_noargs(self, **options):
        data_reader = csv.reader(open(SOURCE_FILE))
        
        for i, row in enumerate(data_reader):
            if i == 0:
                header_row = row;            
            else:
                record = StateLaborForceParticipation()
                record.year = row[0]
                record.state = row[1]
                record.value = row[2]
                record.save()
