# Path: sitegeist/data/contributions/models.py
# class Contribution(models.Model):
#     zipcode = models.CharField(max_length=5)
#     total = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
#     republicans = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
#     democrats = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)\
# 
#     class Meta:
#         ordering = ('zipcode',)
# 
#     def __unicode__(self):
#         return self.zipcode


import csv
import os
from django.conf import settings
from sitegeist.data.contributions.models import Contribution

def load_contributions():

    cache_dir = settings.SITEGEIST['DATA_CACHE']
    inpath = os.path.join(cache_dir, 'contributions', 'contribs_by_zip.csv')

    with open(inpath) as infile:

        reader = csv.DictReader(infile)

        for record in reader:
            Contribution.objects.create(
                zipcode=record['substring'],
                total=record['amount'],
                republicans=record['republican_amount'],
                democrats=record['democratic_amount']
            )


def load(force=False):

    if force:
        reset()

    reset()  # reset anyway

    load_contributions()


if __name__ == '__main__':

# Path: sitegeist/data/contributions/models.py
# class Contribution(models.Model):
#     zipcode = models.CharField(max_length=5)
#     total = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
#     republicans = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
#     democrats = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)\
# 
#     class Meta:
#         ordering = ('zipcode',)
# 
#     def __unicode__(self):
#         return self.zipcode


# Path: sitegeist/data/contributions/loading.py
import csv
import os
from django.conf import settings
from sitegeist.data.contributions.models import Contribution


def load_contributions():

    cache_dir = settings.SITEGEIST['DATA_CACHE']
    inpath = os.path.join(cache_dir, 'contributions', 'contribs_by_zip.csv')

    with open(inpath) as infile:

        reader = csv.DictReader(infile)

        for record in reader:
            Contribution.objects.create(
                zipcode=record['substring'],
                total=record['amount'],
                republicans=record['republican_amount'],
                democrats=record['democratic_amount']
            )


def load(force=False):

    if force:
        reset()

    reset()  # reset anyway

    load_contributions()


if __name__ == '__main__':

    load()