Skip to content

Commit

Permalink
Merge pull request #376 from sleitner/rram
Browse files Browse the repository at this point in the history
Hierarchy filters by year
  • Loading branch information
contolini committed Mar 4, 2016
2 parents bd4b538 + b560e60 commit 84cac58
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
9 changes: 4 additions & 5 deletions mapusaurus/hmda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ def loan_originations(request):
action_taken_param = request.GET.get('action_taken')
lender_hierarchy = request.GET.get('lh')
peers = request.GET.get('peers')
year = request.GET.get('year')
census_tracts = get_censustract_geos(request)

query = HMDARecord.objects.all()

#if lender param key is passed in
query = HMDARecord.objects.all()
if institution_id:
institution_selected = get_object_or_404(Institution, pk=institution_id)
if lender_hierarchy == 'true':
hierarchy_list = institution_selected.get_lender_hierarchy(False, False)
hierarchy_list = institution_selected.get_lender_hierarchy(False, False, year)
if len(hierarchy_list) > 0:
query = query.filter(institution__in=hierarchy_list)
query = query.filter(institution__in=hierarchy_list)
else:
query = query.filter(institution=institution_selected)
elif peers == 'true' and metro:
Expand Down
2 changes: 1 addition & 1 deletion mapusaurus/mapping/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def map(request, template):

if lender:
context['lender'] = lender
hierarchy_list = lender.get_lender_hierarchy(True, True)
hierarchy_list = lender.get_lender_hierarchy(True, True, year_selected)
context['institution_hierarchy'] = hierarchy_list
if metro:
context['metro'] = metro
Expand Down
4 changes: 2 additions & 2 deletions mapusaurus/respondents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def formatted_name(self):
""" Returns a list of related institutions for the selected institution.
Allows to exclude selected institution/lender and order by institution's assets
"""
def get_lender_hierarchy(self, exclude, order):
def get_lender_hierarchy(self, exclude, order, year):
lender_hierarchy = self.lenderhierarchy_set.first()
if lender_hierarchy:
org_id = lender_hierarchy.organization_id
hierarchy_list = LenderHierarchy.objects.filter(organization_id=org_id).select_related('institution')
hierarchy_list = LenderHierarchy.objects.filter(organization_id=org_id).select_related('institution').filter(institution__year=year)
if exclude:
hierarchy_list = hierarchy_list.exclude(institution=self)
if order:
Expand Down
14 changes: 7 additions & 7 deletions mapusaurus/respondents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,28 @@ class LenderHierarchyTest(TestCase):
def test_get_lender_hierarchy(self):
"""Case: Institution has no hierarchy"""
institution = Institution.objects.filter(institution_id="11000000002").first()
hierarchy_list = institution.get_lender_hierarchy(False, False)
hierarchy_list = institution.get_lender_hierarchy(False, False, 2013)
self.assertEqual(len(hierarchy_list), 0)

"""Case: Institution has no hierarchy but itself.
Returns itself when exclude=False; Returns empy list when exclude=True
"""
institution = Institution.objects.filter(institution_id="91000000003").first()
hierarchy_list = institution.get_lender_hierarchy(False, False)
hierarchy_list = institution.get_lender_hierarchy(False, False, 2013)
self.assertEqual(len(hierarchy_list), 1)
self.assertEqual(hierarchy_list[0].institution_id, "91000000003")
hierarchy_list_exclude = institution.get_lender_hierarchy(True, False)
hierarchy_list_exclude = institution.get_lender_hierarchy(True, False, 2013)
self.assertEqual(len(hierarchy_list_exclude), 0)

"""Case: Institution has valid hierarchy and returns it"""
institution = Institution.objects.filter(institution_id="91000000001").first()
hierarchy_list = institution.get_lender_hierarchy(False, False)
hierarchy_list = institution.get_lender_hierarchy(False, False,2013)
self.assertEqual(len(hierarchy_list), 3)
hierarchy_list_exclude = institution.get_lender_hierarchy(True, False)
hierarchy_list_exclude = institution.get_lender_hierarchy(True, False, 2013)
self.assertEqual(len(hierarchy_list_exclude), 2)
hierarchy_list_order = institution.get_lender_hierarchy(False, True)
hierarchy_list_order = institution.get_lender_hierarchy(False, True, 2013)
self.assertEqual(hierarchy_list_order[0].institution_id, "91000000001")
hierarchy_list_exclude_order = institution.get_lender_hierarchy(True, True)
hierarchy_list_exclude_order = institution.get_lender_hierarchy(True, True, 2013)
self.assertEqual(hierarchy_list_exclude_order[0].institution_id, "91000000002")
self.assertEqual(len(hierarchy_list_exclude_order), 2)

Expand Down

0 comments on commit 84cac58

Please sign in to comment.