20
20
from application .data_access .entity_queries import (
21
21
get_entity_query ,
22
22
get_entity_search ,
23
+ get_organisations ,
23
24
lookup_entity_link ,
25
+ get_linked_entities ,
26
+ fetchEntityFromReference ,
24
27
)
25
28
from application .data_access .dataset_queries import get_dataset_names
26
29
@@ -146,12 +149,16 @@ def handle_entity_response(
146
149
dataset_fields = [dataset_field ["dataset" ] for dataset_field in dataset_fields ]
147
150
148
151
dataset = get_dataset_query (session , e .dataset )
152
+
149
153
organisation_entity , _ , _ = get_entity_query (session , e .organisation_entity )
150
154
151
155
entityLinkFields = [
152
156
"article-4-direction" ,
153
157
"permitted-development-rights" ,
154
158
"tree-preservation-order" ,
159
+ "local-plan-boundary" ,
160
+ "local-plan" ,
161
+ "local-plan-event" ,
155
162
]
156
163
157
164
linked_entities = {}
@@ -166,12 +173,19 @@ def handle_entity_response(
166
173
if linked_entity is not None :
167
174
linked_entities [field ] = linked_entity
168
175
176
+ # Fetch linked local plans/document/timetable
177
+ local_plans , local_plan_boundary_geojson = fetch_linked_local_plans (
178
+ session , e_dict_sorted
179
+ )
180
+
169
181
return templates .TemplateResponse (
170
182
"entity.html" ,
171
183
{
172
184
"request" : request ,
173
185
"row" : e_dict_sorted ,
186
+ "local_plan_geojson" : local_plan_boundary_geojson ,
174
187
"linked_entities" : linked_entities ,
188
+ "local_plans" : local_plans ,
175
189
"entity" : e ,
176
190
"pipeline_name" : e .dataset ,
177
191
"references" : [],
@@ -189,6 +203,51 @@ def handle_entity_response(
189
203
)
190
204
191
205
206
+ linked_datasets = {
207
+ "local-plan-boundary" : ["local-plan" ],
208
+ "local-plan" : [
209
+ "local-plan-document" ,
210
+ "local-plan-timetable" ,
211
+ "local-plan-boundary" ,
212
+ ],
213
+ }
214
+
215
+
216
+ def fetch_linked_local_plans (session : Session , e_dict_sorted : Dict = None ):
217
+ results = {}
218
+ local_plan_boundary_geojson = None
219
+ dataset = e_dict_sorted ["dataset" ]
220
+ reference = e_dict_sorted ["reference" ]
221
+ if dataset in linked_datasets :
222
+ linked_dataset_value = linked_datasets [dataset ]
223
+ for linked_dataset in linked_dataset_value :
224
+ if dataset == "local-plan" and linked_dataset == "local-plan-boundary" :
225
+ if linked_dataset in e_dict_sorted :
226
+ local_plan_boundary_geojson = fetchEntityFromReference (
227
+ session , linked_dataset , e_dict_sorted [linked_dataset ]
228
+ )
229
+ linked_entities = get_linked_entities (
230
+ session , linked_dataset , reference , linked_dataset = dataset
231
+ )
232
+ results [linked_dataset ] = linked_entities
233
+
234
+ # Handle special case for "local-plan-timetable"
235
+ if dataset == "local-plan" and linked_dataset == "local-plan-timetable" :
236
+ for entity in linked_entities :
237
+ if (
238
+ hasattr (entity , "local_plan_event" )
239
+ and entity .local_plan_event
240
+ and not entity .local_plan_event .startswith ("estimated" )
241
+ ):
242
+ entity .local_plan_event = fetchEntityFromReference (
243
+ session , "local-plan-event" , entity .local_plan_event
244
+ )
245
+ else :
246
+ entity .local_plan_event = None
247
+
248
+ return results , local_plan_boundary_geojson
249
+
250
+
192
251
def get_entity (
193
252
request : Request ,
194
253
entity : int = Path (default = Required , description = "Entity id" ),
@@ -329,6 +388,12 @@ def search_entities(
329
388
local_authorities = get_local_authorities (session , "local-authority" )
330
389
local_authorities = [la .dict () for la in local_authorities ]
331
390
391
+ organisations = get_organisations (session )
392
+ columns = ["entity" , "organisation_entity" , "name" ]
393
+ organisations_list = [
394
+ organisation .dict (include = set (columns )) for organisation in organisations
395
+ ]
396
+
332
397
if links .get ("prev" ) is not None :
333
398
prev_url = links ["prev" ]
334
399
else :
@@ -350,6 +415,7 @@ def search_entities(
350
415
"datasets" : datasets ,
351
416
"local_authorities" : local_authorities ,
352
417
"typologies" : typologies ,
418
+ "organisations" : organisations_list ,
353
419
"query" : {"params" : params },
354
420
"active_filters" : [
355
421
filter_name
0 commit comments