def tables(request): try: table_data = minority_aggregation_as_json(request) context = {'table_data': table_data} return HttpResponse(json.dumps(context), content_type='application/json') except: return HttpResponseBadRequest("the following request failed: %s" % request)
def test_minority_aggregation_as_json(self): """should return a dict of 5 dicts returning minority values""" keys = ['counties', 'msa'] 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'] result_dict = minority_aggregation_as_json(request) self.assertTrue(isinstance(result_dict, dict)) 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(len(result_dict['msa']) > 0) self.assertEqual(result_dict['msa']['lar_total'], 0) self.assertIsNone(result_dict['msa']['odds_lma'])
def tables_csv(request): institution_id = request.GET.get('lender') metro = request.GET.get('metro') year = request.GET.get('year') 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 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 header_dict = {"msa_or_county_id":"MSA or County ID", "peer_lar_total":"Total LAR of Peers","name":"County Name", "hma_pct":"Pct in HMA","lma_pct":"Pct in LMA","mma_pct":"Pct in MMA", "lma":"LAR Count in LMA","mma":"LAR Count in MMA","hma":"LAR Count in HMA", "lar_total":"Total LAR in MSA","peer_hma_pct":"Odds Ratio in HMA", "peer_mma_pct":"Odds Ratio in MMA","peer_lma_pct":"Odds Ratio in LMA", "peer_lma":"Total Peer LAR in LMA" ,"peer_mma":"Total Peer LAR in MMA", "peer_hma":"Total Peer LAR in HMA", "odds_lma":"Odds LMA", "odds_mma":"Odds MMA", "odds_hma":"Odds HMA"} msa['msa_or_county_id'] = institution_id # MSA has no county name so insert the word "MSA" msa['name'] = 'MSA' writer = csv.writer(response, csv.excel) writer.writerow([header_dict[k] for k in 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 tables(request): table_data = minority_aggregation_as_json(request) return HttpResponse(json.dumps(table_data), content_type='application/json')