Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #269 from caciviclab/wip_spreadsheet_import
Browse files Browse the repository at this point in the history
Add django command to import from the spreadsheet
  • Loading branch information
tdooner authored Aug 24, 2016
2 parents 85185ee + 836b27b commit 1b20131
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
16 changes: 16 additions & 0 deletions disclosure/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
NETFILE_DOWNLOAD_DIR = op.join(DATA_DIR, 'netfile')


LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
},
}

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
Expand Down
108 changes: 108 additions & 0 deletions finance/management/commands/importoaklandcandidates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import csv
import datetime
import urllib2

from django.core.management.base import BaseCommand

from ballot.models import Ballot, Candidate, Office, OfficeElection
from locality.models import City, State
from ballot.models.referendum import Referendum


HUMAN_CANDIDATES_URL = \
'https://docs.google.com/spreadsheets/d/1272oaLyQhKwQa6RicA5tBso6wFruum-mgrNm3O3VogI' \
'/pub?gid=0&single=true&output=tsv'
BALLOT_MEASURES_URL = \
'https://docs.google.com/spreadsheets/d/1272oaLyQhKwQa6RicA5tBso6wFruum-mgrNm3O3VogI' \
'/pub?gid=1693935349&single=true&output=tsv'
NON_CONTROLLED_COMMITTEE_URL = \
'https://docs.google.com/spreadsheets/d/1272oaLyQhKwQa6RicA5tBso6wFruum-mgrNm3O3VogI' \
'/pub?gid=1995437960&single=true&output=tsv'


class Command(BaseCommand):
help = 'Import the Oakland Candidates from the spreadsheet'

def handle(self, *args, **options):
# BALLOT:
state, _ = State.objects.get_or_create(name='California', short_name='CA')
oakland, _ = City.objects.get_or_create(
name='Oakland',
short_name='COAK',
state=state
)

ballot, _ = Ballot.objects.get_or_create(
locality=oakland
)
ballot.date = datetime.date(2016, 11, 6)
ballot.save()

# HUMAN CANDIDATES
# =====================================================================
self.process_humans(ballot, oakland)

# BALLOT MEASURES
# =====================================================================
self.process_ballot_measures(ballot)

def process_ballot_measures(self, ballot):
resp = urllib2.urlopen(BALLOT_MEASURES_URL)
rows = csv.DictReader(resp, delimiter='\t')
title_column = 'Ballot Measures: ' \
'https://drive.google.com/open?id=0BzmHYSKNqcR_TjdjY21icWt6VUE'
for row in rows:
referendum, created = Referendum.objects.get_or_create(
title=row[title_column],
number=row['Measure alpha-numeric designation'],
contest_type='R',
ballot=ballot
)
if created:
print 'created!'
else:
print referendum
resp.close()

def process_humans(self, ballot, oakland):
resp = urllib2.urlopen(HUMAN_CANDIDATES_URL)
rows = csv.DictReader(resp, delimiter='\t')
for row in rows:
# TODO: Update the spreadsheet to handle split names
names = row['Candidate'].split(' ', 3)
if len(names) == 2:
first_name = names[0]
middle_name = ''
last_name = names[1]
elif len(names) > 2:
first_name = names[0]
middle_name = " ".join(names[1:-1])
last_name = " ".join(names[-1:])

candidate_office, _ = Office.objects.get_or_create(
name=row['Office'],
locality=oakland
)
office_election, _ = OfficeElection.objects.get_or_create(
office=candidate_office,
ballot=ballot
)

candidate = Candidate.objects.filter(
first_name=first_name,
middle_name=middle_name,
last_name=last_name,
office_election=office_election
)

if candidate:
print "Found existing candidate: " + str(candidate)
else:
candidate.get_or_create(
first_name=first_name,
middle_name=middle_name,
last_name=last_name,
office_election=office_election
)
print "Creating candidate: " + str(candidate)
resp.close()

0 comments on commit 1b20131

Please sign in to comment.