Skip to content

Commit

Permalink
Merge pull request #1 from sleitner/table-download
Browse files Browse the repository at this point in the history
Change basic table from dict to list and add test
  • Loading branch information
contolini committed Mar 14, 2016
2 parents 772fe84 + 8295876 commit 35a72b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
26 changes: 22 additions & 4 deletions mapusaurus/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from mock import Mock

from utils import use_GET_in
from api.views import msas, tables
from api.views import msas, tables, tables_csv

class ConversionTest(TestCase):
def test_use_GET_in(self):
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_api_msas_endpoint(self):

def test_api_tables_endpoint(self):
"""should return table_data json for a lender/MSA pair"""
params = {'lender': '90000451965', 'metro': '49180'}
params = {'lender': '90000451965', 'metro': '49180', 'year':'2013'}
url = reverse(tables)
resp = self.client.get(url, params)
result_dict = json.loads(resp.content)
Expand All @@ -95,7 +95,25 @@ def test_api_tables_endpoint(self):
for key in keys:
self.assertTrue(key in result_dict.keys())
for key in lender_keys:
self.assertTrue(key in result_dict['msa'].keys())
self.assertTrue(key in result_dict['msa'].keys())
self.assertTrue(len(result_dict['msa']) > 0)


def test_api_tables_csv(self):
import csv, StringIO
"""should return table_data csv for a lender/MSA pair"""
params = {'lender': '90000451965', 'metro': '49180', 'year': '2013'}
url = reverse(tables_csv)
resp = self.client.get(url, params)
f = StringIO.StringIO(resp.content)
result = csv.DictReader(f, delimiter=',')
for result_dict in result:
pass
self.assertTrue(isinstance(result_dict, dict))
input_keys = ['msa_or_county_id', 'peer_lar_total', 'name']
lender_keys = ['hma_pct', 'lma_pct', 'mma_pct', 'lma', 'mma', 'hma', 'lar_total',
'peer_hma_pct', 'peer_lma_pct', 'peer_mma_pct', 'peer_lma', 'peer_mma',
'peer_hma', 'peer_lar_total', 'odds_lma', 'odds_mma', 'odds_hma']
keys = input_keys + lender_keys
for key in keys:
self.assertTrue(key in result_dict.keys())
self.assertTrue(len(result_dict['msa_or_county_id']) > 0)
27 changes: 17 additions & 10 deletions mapusaurus/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ def tables_csv(request):
metro = request.GET.get('metro')
year = request.GET.get('year')

aggregation = minority_aggregation_as_json(request)
msa = aggregation['msa']
counties = aggregation['counties']
file_name = 'HMDA-Summary-Table_Year%s_Lender%s_MSA%s.csv' % (year, institution_id, metro)
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % file_name

writer = csv.writer(response, csv.excel)
writer.writerow(['msa_or_county_id'] + counties.itervalues().next().keys())
aggregation = minority_aggregation_as_json(request)
msa = aggregation['msa']
counties = aggregation['counties']
input_keys = ['msa_or_county_id', 'peer_lar_total', 'name']
lender_keys = ['hma_pct', 'lma_pct', 'mma_pct', 'lma', 'mma', 'hma', 'lar_total',
'peer_hma_pct', 'peer_lma_pct', 'peer_mma_pct', 'peer_lma', 'peer_mma',
'peer_hma', 'peer_lar_total', 'odds_lma', 'odds_mma', 'odds_hma']
keys = input_keys + lender_keys
msa['msa_or_county_id'] = institution_id
# MSA has no county name so insert the word "MSA"
msa = msa.values()
msa.insert(1, 'MSA')
writer.writerow([institution_id] + msa)
for county in counties:
writer.writerow([county] + counties[county].values())
msa['name'] = 'MSA'
writer = csv.writer(response, csv.excel)
writer.writerow(keys)
writer.writerow([msa[k] for k in keys])
for id,county in counties.iteritems():
county = counties[id]
county['msa_or_county_id'] = id
writer.writerow([county[k] for k in keys])
return response

def msas(request):
Expand Down

0 comments on commit 35a72b7

Please sign in to comment.