9
9
from fastapi .responses import HTMLResponse , RedirectResponse
10
10
from fastapi .exceptions import RequestValidationError
11
11
from sqlalchemy .orm import Session
12
-
13
12
from application .core .models import GeoJSON , EntityModel
14
13
from application .data_access .digital_land_queries import (
15
14
get_datasets ,
@@ -70,6 +69,120 @@ def _get_entity_json(data: List[EntityModel], include: Optional[Set] = None):
70
69
return entities
71
70
72
71
72
+ def handle_gone_entity (
73
+ request : Request , entity : int , extension : Optional [SuffixEntity ]
74
+ ):
75
+ if extension :
76
+ raise HTTPException (
77
+ detail = f"Entity { entity } has been removed" ,
78
+ status_code = 410 ,
79
+ )
80
+ return templates .TemplateResponse (
81
+ "entity-gone.html" ,
82
+ {"request" : request , "entity" : str (entity )},
83
+ status_code = 410 ,
84
+ )
85
+
86
+
87
+ def handle_moved_entity (
88
+ entity : int , new_entity_id : int , extension : Optional [SuffixEntity ]
89
+ ):
90
+ if extension :
91
+ return RedirectResponse (f"/entity/{ new_entity_id } .{ extension } " , status_code = 301 )
92
+ return RedirectResponse (f"/entity/{ new_entity_id } " , status_code = 301 )
93
+
94
+
95
+ def prepare_geojson (e ):
96
+ if e .geojson is not None :
97
+ geojson = e .geojson
98
+ if geojson :
99
+ properties = e .dict (exclude = {"geojson" , "geometry" , "point" }, by_alias = True )
100
+ geojson .properties = properties
101
+ return geojson
102
+ return None
103
+
104
+
105
+ def handle_entity_response (
106
+ request : Request , e , extension : Optional [SuffixEntity ], session : Session
107
+ ):
108
+ if extension is not None and extension .value == "json" :
109
+ return e .dict (by_alias = True , exclude = {"geojson" })
110
+
111
+ geojson = None
112
+
113
+ if extension is not None and extension .value == "geojson" :
114
+ geojson = prepare_geojson (e )
115
+ if geojson :
116
+ return geojson
117
+ else :
118
+ raise HTTPException (
119
+ status_code = 406 , detail = "geojson for entity not available"
120
+ )
121
+
122
+ e_dict = e .dict (by_alias = True , exclude = {"geojson" })
123
+ e_dict_sorted = {
124
+ key : e_dict [key ] for key in sorted (e_dict .keys (), key = entity_attribute_sort_key )
125
+ }
126
+
127
+ # need to remove any dependency on facts this should be changed when fields added to postgis
128
+ fields = None
129
+ # get field specifications and convert to dictionary to easily access
130
+ # fields = get_field_specifications(e_dict_sorted.keys())
131
+ # if fields:
132
+ # fields = [field.dict(by_alias=True) for field in fields]
133
+ # fields = {field["field"]: field for field in fields}
134
+
135
+ # get dictionary of fields which have linked datasets
136
+ dataset_fields = get_datasets (session , datasets = e_dict_sorted .keys ())
137
+ dataset_fields = [
138
+ dataset_field .dict (by_alias = True ) for dataset_field in dataset_fields
139
+ ]
140
+ dataset_fields = [dataset_field ["dataset" ] for dataset_field in dataset_fields ]
141
+
142
+ dataset = get_dataset_query (session , e .dataset )
143
+ organisation_entity , _ , _ = get_entity_query (session , e .organisation_entity )
144
+
145
+ entityLinkFields = [
146
+ "article-4-direction" ,
147
+ "permitted-development-rights" ,
148
+ "tree-preservation-order" ,
149
+ ]
150
+
151
+ linked_entities = {}
152
+
153
+ # for each entityLinkField, if that key exists in the entity dict, then
154
+ # lookup the entity and add it to the linked_entities dict
155
+ for field in entityLinkFields :
156
+ if field in e_dict_sorted :
157
+ linked_entity = lookup_entity_link (
158
+ session , e_dict_sorted [field ], field , e_dict_sorted ["dataset" ]
159
+ )
160
+ if linked_entity is not None :
161
+ linked_entities [field ] = linked_entity
162
+
163
+ return templates .TemplateResponse (
164
+ "entity.html" ,
165
+ {
166
+ "request" : request ,
167
+ "row" : e_dict_sorted ,
168
+ "linked_entities" : linked_entities ,
169
+ "entity" : e ,
170
+ "pipeline_name" : e .dataset ,
171
+ "references" : [],
172
+ "breadcrumb" : [],
173
+ "schema" : None ,
174
+ "typology" : e .typology ,
175
+ "entity_prefix" : "" ,
176
+ "geojson_features" : e .geojson if e .geojson is not None else None ,
177
+ "geojson" : geojson .dict () if geojson else None ,
178
+ "fields" : fields ,
179
+ "dataset_fields" : dataset_fields ,
180
+ "dataset" : dataset ,
181
+ "organisation_entity" : organisation_entity ,
182
+ },
183
+ )
184
+
185
+
73
186
def get_entity (
74
187
request : Request ,
75
188
entity : int = Path (default = Required , description = "Entity id" ),
@@ -79,114 +192,11 @@ def get_entity(
79
192
e , old_entity_status , new_entity_id = get_entity_query (session , entity )
80
193
81
194
if old_entity_status == 410 :
82
- if extension :
83
- raise HTTPException (
84
- detail = f"Entity { entity } has been removed" ,
85
- status_code = 410 ,
86
- )
87
- else :
88
- return templates .TemplateResponse (
89
- "entity-gone.html" ,
90
- {
91
- "request" : request ,
92
- "entity" : str (entity ),
93
- },
94
- status_code = 410 ,
95
- )
195
+ return handle_gone_entity (request , entity , extension )
96
196
elif old_entity_status == 301 :
97
- if extension :
98
- return RedirectResponse (
99
- f"/entity/{ new_entity_id } .{ extension } " , status_code = 301
100
- )
101
- else :
102
- return RedirectResponse (f"/entity/{ new_entity_id } " , status_code = 301 )
197
+ return handle_moved_entity (entity , new_entity_id , extension )
103
198
elif e is not None :
104
- if extension is not None and extension .value == "json" :
105
- return e .dict (by_alias = True , exclude = {"geojson" })
106
-
107
- if e .geojson is not None :
108
- geojson = e .geojson
109
- properties = e .dict (exclude = {"geojson" , "geometry" , "point" }, by_alias = True )
110
- geojson .properties = properties
111
- else :
112
- geojson = None
113
-
114
- if extension is not None and extension .value == "geojson" :
115
- if geojson is not None :
116
- return geojson
117
- else :
118
- raise HTTPException (
119
- status_code = 406 , detail = "geojson for entity not available"
120
- )
121
-
122
- e_dict = e .dict (by_alias = True , exclude = {"geojson" })
123
- e_dict_sorted = {
124
- key : e_dict [key ]
125
- for key in sorted (e_dict .keys (), key = entity_attribute_sort_key )
126
- }
127
-
128
- if geojson is not None :
129
- geojson_dict = dict (geojson )
130
- else :
131
- geojson_dict = None
132
-
133
- # need to remove any dependency on facts this should be changed when fields added to postgis
134
- fields = None
135
- # get field specifications and convert to dictionary to easily access
136
- # fields = get_field_specifications(e_dict_sorted.keys())
137
- # if fields:
138
- # fields = [field.dict(by_alias=True) for field in fields]
139
- # fields = {field["field"]: field for field in fields}
140
-
141
- # get dictionary of fields which have linked datasets
142
- dataset_fields = get_datasets (session , datasets = e_dict_sorted .keys ())
143
- dataset_fields = [
144
- dataset_field .dict (by_alias = True ) for dataset_field in dataset_fields
145
- ]
146
- dataset_fields = [dataset_field ["dataset" ] for dataset_field in dataset_fields ]
147
-
148
- dataset = get_dataset_query (session , e .dataset )
149
- organisation_entity , _ , _ = get_entity_query (session , e .organisation_entity )
150
-
151
- entityLinkFields = [
152
- "article-4-direction" ,
153
- "permitted-development-rights" ,
154
- "tree-preservation-order" ,
155
- ]
156
-
157
- linked_entities = {}
158
-
159
- # for each entityLinkField, if that key exists in the entity dict, then
160
- # lookup the entity and add it to the linked_entities dict
161
- for field in entityLinkFields :
162
- if field in e_dict_sorted :
163
- linked_entity = lookup_entity_link (
164
- session , e_dict_sorted [field ], field , e_dict_sorted ["dataset" ]
165
- )
166
- if linked_entity is not None :
167
- linked_entities [field ] = linked_entity
168
-
169
- return templates .TemplateResponse (
170
- "entity.html" ,
171
- {
172
- "request" : request ,
173
- "row" : e_dict_sorted ,
174
- "linked_entities" : linked_entities ,
175
- "entity" : e ,
176
- "pipeline_name" : e .dataset ,
177
- "references" : [],
178
- "breadcrumb" : [],
179
- "schema" : None ,
180
- "typology" : e .typology ,
181
- "entity_prefix" : "" ,
182
- "geojson_features" : e .geojson if e .geojson is not None else None ,
183
- "geojson" : geojson_dict ,
184
- "fields" : fields ,
185
- "dataset_fields" : dataset_fields ,
186
- "dataset" : dataset ,
187
- "organisation_entity" : organisation_entity ,
188
- },
189
- )
199
+ return handle_entity_response (request , e , extension , session )
190
200
else :
191
201
raise HTTPException (status_code = 404 , detail = "entity not found" )
192
202
0 commit comments