From bc7d3710abcc8f34e7698284c3478773817258df Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Mon, 7 Sep 2020 18:57:50 +0100 Subject: [PATCH] make sure countries library still returns values to recap the previous commit, in the ward->local authority->county library we want to return all local authorities and counties. We do this by excluding anything that doesn't have children. However, in the countries library, all four countries don't have children. I can't think of a generic way to separate these so just filter on the library id --- app/broadcast_areas/repo.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app/broadcast_areas/repo.py b/app/broadcast_areas/repo.py index e1aec1333..c6cb275c8 100644 --- a/app/broadcast_areas/repo.py +++ b/app/broadcast_areas/repo.py @@ -166,17 +166,34 @@ class BroadcastAreasRepository(object): return areas def get_all_areas_for_library(self, library_id): - # only interested in areas with children - local authorities, counties, unitary authorities. not wards. - q = """ - SELECT id, name - FROM broadcast_areas - JOIN ( - SELECT DISTINCT broadcast_area_library_group_id + is_multi_tier_library = self.query(""" + SELECT exists( + SELECT 1 FROM broadcast_areas - WHERE broadcast_area_library_group_id IS NOT NULL - ) AS parent_broadcast_areas ON parent_broadcast_areas.broadcast_area_library_group_id = broadcast_areas.id - WHERE broadcast_area_library_id = ? - """ + WHERE broadcast_area_library_id = ? AND + broadcast_area_library_group_id IS NOT NULL + ) + """, library_id)[0][0] + + if is_multi_tier_library: + # only interested in areas with children - eg local authorities, counties, unitary authorities. not wards. + q = """ + SELECT id, name + FROM broadcast_areas + JOIN ( + SELECT DISTINCT broadcast_area_library_group_id + FROM broadcast_areas + WHERE broadcast_area_library_group_id IS NOT NULL + ) AS parent_broadcast_areas ON parent_broadcast_areas.broadcast_area_library_group_id = broadcast_areas.id + WHERE broadcast_area_library_id = ? + """ + else: + # Countries don't have any children, so the above query wouldn't return anything. + q = """ + SELECT id, name + FROM broadcast_areas + WHERE broadcast_area_library_id = ? + """ results = self.query(q, library_id)