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
This commit is contained in:
Leo Hemsted
2020-09-07 18:57:50 +01:00
parent 256d2b2b60
commit bc7d3710ab

View File

@@ -166,17 +166,34 @@ class BroadcastAreasRepository(object):
return areas return areas
def get_all_areas_for_library(self, library_id): def get_all_areas_for_library(self, library_id):
# only interested in areas with children - local authorities, counties, unitary authorities. not wards. is_multi_tier_library = self.query("""
q = """ SELECT exists(
SELECT id, name SELECT 1
FROM broadcast_areas
JOIN (
SELECT DISTINCT broadcast_area_library_group_id
FROM broadcast_areas FROM broadcast_areas
WHERE broadcast_area_library_group_id IS NOT NULL WHERE broadcast_area_library_id = ? AND
) AS parent_broadcast_areas ON parent_broadcast_areas.broadcast_area_library_group_id = broadcast_areas.id broadcast_area_library_group_id IS NOT NULL
WHERE broadcast_area_library_id = ? )
""" """, 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) results = self.query(q, library_id)