-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththing.py
More file actions
650 lines (574 loc) · 18.1 KB
/
thing.py
File metadata and controls
650 lines (574 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# ===============================================================================
# Copyright 2025 ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
from typing import Annotated, Optional
from fastapi import APIRouter, Query, Request
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError, ProgrammingError
from sqlalchemy.orm import selectinload
from starlette.status import (
HTTP_200_OK,
HTTP_201_CREATED,
HTTP_204_NO_CONTENT,
HTTP_409_CONFLICT,
)
from api.pagination import CustomPage
from core.app import public_route
from core.dependencies import (
session_dependency,
admin_dependency,
editor_dependency,
viewer_dependency,
)
from db.deployment import Deployment
from db.thing import Thing, ThingIdLink, WellScreen
from schemas.deployment import DeploymentResponse
from schemas.thing import (
CreateThingIdLink,
CreateWell,
CreateWellScreen,
ThingResponse,
WellResponse,
WellScreenResponse,
UpdateSpring,
UpdateWell,
SpringResponse,
CreateSpring,
ThingIdLinkResponse,
UpdateThingIdLink,
UpdateWellScreen,
)
from schemas.well_details import WellDetailsResponse
from schemas.well_export import WellExportResponse
from services.crud_helper import model_patcher, model_adder, model_deleter
from services.exceptions_helper import PydanticStyleException
from services.lexicon_helper import get_terms_by_category
from services.query_helper import (
simple_get_by_id,
paginated_all_getter,
order_sort_filter,
)
from services.thing_helper import (
add_thing,
patch_thing,
add_well_screen,
get_db_things,
get_thing_of_a_thing_type_by_id,
modify_well_descriptor_tables,
WELL_DESCRIPTOR_MODEL_MAP,
)
from services.well_details_helper import (
get_well_details_payload,
get_well_export_payload,
)
router = APIRouter(prefix="/thing", tags=["thing"])
def database_error_handler(
payload: CreateWell | CreateSpring | CreateWellScreen | CreateThingIdLink,
error: IntegrityError | ProgrammingError,
) -> None:
"""
Handle errors raised by the database when adding or updating a thing.
"""
orig = getattr(error, "orig", None)
if hasattr(orig, "args") and orig.args and isinstance(orig.args[0], dict):
error_message = orig.args[0].get("M", "")
else:
error_message = str(orig or error)
if 'constraint "group_thing_association_group_id_fkey"' in error_message:
detail = {
"loc": ["body", "group_id"],
"msg": f"Group with ID {payload.group_id} not found.",
"type": "value_error",
"input": {"group_id": payload.group_id},
}
elif 'constraint "location_thing_association_location_id_fkey"' in error_message:
detail = {
"loc": ["body", "location_id"],
"msg": f"Location with ID {payload.location_id} not found.",
"type": "value_error",
"input": {"location_id": payload.location_id},
}
elif 'constraint "well_screen_thing_id_fkey"' in error_message:
detail = {
"loc": ["body", "thing_id"],
"msg": f"Thing with ID {payload.thing_id} not found.",
"type": "value_error",
"input": {"thing_id": payload.thing_id},
}
elif 'constraint "well_screen_screen_type_fkey"' in error_message:
valid_screen_types = get_terms_by_category("casing_material")
valid_screen_types_for_msg = " | ".join(valid_screen_types)
detail = {
"loc": ["body", "screen_type"],
"msg": f"{payload.screen_type} is an invalid screen type. Valid types are: {valid_screen_types_for_msg}.",
"type": "value_error",
"input": {"screen_type": payload.screen_type},
}
elif 'constraint "thing_id_link_thing_id_fkey"' in error_message:
detail = {
"loc": ["body", "thing_id"],
"msg": f"Thing with ID {payload.thing_id} not found.",
"type": "value_error",
"input": {"thing_id": payload.thing_id},
}
else:
detail = {
"loc": ["body"],
"msg": error_message,
"type": "value_error",
"input": {},
}
raise PydanticStyleException(status_code=HTTP_409_CONFLICT, detail=[detail])
# GET ==========================================================================
@router.get("/water-well", summary="Get all water wells", status_code=HTTP_200_OK)
def get_water_wells(
user: viewer_dependency,
session: session_dependency,
request: Request,
sort: Optional[str] = None,
order: Optional[str] = None,
filter_params: Annotated[list[str] | None, Query(alias="filter")] = None,
query: Optional[str] = None,
name: Optional[str] = None,
name_contains: Optional[str] = None,
include_contacts: bool = False,
) -> CustomPage[WellResponse]:
"""
Retrieve all wells from the database.
"""
thing_type = request.url.path.split("/")[2].replace("-", " ")
return get_db_things(
None,
order,
query,
session,
sort,
name=name,
thing_type=thing_type,
include_contacts=include_contacts,
filters=filter_params,
name_contains=name_contains,
)
@router.get(
"/water-well/{thing_id}", summary="Get water well by ID", status_code=HTTP_200_OK
)
def get_well_by_id(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
) -> WellResponse:
"""
Retrieve a water well by ID from the database.
"""
return get_thing_of_a_thing_type_by_id(session, request, thing_id)
@router.get(
"/water-well/{thing_id}/details",
summary="Get water well details payload",
status_code=HTTP_200_OK,
)
def get_well_details(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
field_event_limit: int = Query(default=25, ge=1, le=100),
) -> WellDetailsResponse:
"""
Retrieve the consolidated payload needed to render the well details page.
Hydrograph series and map layer loading are intentionally handled separately.
"""
return get_well_details_payload(
session=session,
request=request,
thing_id=thing_id,
field_event_limit=field_event_limit,
)
@router.get(
"/water-well/{thing_id}/export",
summary="Get water well export payload",
status_code=HTTP_200_OK,
)
def get_well_export(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
) -> WellExportResponse:
"""
Retrieve the minimal payload needed for field sheet export generation.
"""
return get_well_export_payload(
session=session,
request=request,
thing_id=thing_id,
)
@router.get(
"/water-well/{thing_id}/well-screen",
summary="Get well screens by water well ID",
status_code=HTTP_200_OK,
)
def get_well_screens_by_well_id(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
) -> CustomPage[WellScreenResponse]:
"""
Retrieve all well screens for a specific water well by its ID.
"""
thing = get_thing_of_a_thing_type_by_id(session, request, thing_id)
sql = select(WellScreen).where(WellScreen.thing_id == thing.id)
return paginate(query=sql, conn=session)
@router.get(
"/well-screen",
summary="Get well screens",
)
def get_well_screens(
user: viewer_dependency,
session: session_dependency,
thing_id: int = None,
) -> CustomPage[WellScreenResponse]:
"""
Retrieve all well screens from the database.
"""
if thing_id:
sql = select(WellScreen).where(WellScreen.thing_id == thing_id)
return paginate(query=sql, conn=session)
return paginated_all_getter(session, WellScreen)
@router.get(
"/well-screen/{wellscreen_id}",
summary="Get well screen by ID",
)
def get_well_screen_by_id(
user: viewer_dependency,
session: session_dependency,
wellscreen_id: int,
) -> WellScreenResponse:
"""
Retrieve a well screen by ID from the database.
"""
well_screen = simple_get_by_id(session, WellScreen, wellscreen_id)
return well_screen
@router.get("/spring", summary="Get all springs")
def get_springs(
user: viewer_dependency,
session: session_dependency,
request: Request,
sort: str = None,
order: str = None,
filter_params: Annotated[list[str] | None, Query(alias="filter")] = None,
query: str = None,
name_contains: Optional[str] = None,
) -> CustomPage[SpringResponse]:
"""
Retrieve all springs from the database.
"""
thing_type = request.url.path.split("/")[2].replace("-", " ")
return get_db_things(
None,
order,
query,
session,
sort,
thing_type=thing_type,
filters=filter_params,
name_contains=name_contains,
)
@router.get("/spring/{thing_id}", summary="Get spring by ID", status_code=HTTP_200_OK)
def get_spring_by_id(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
) -> SpringResponse:
"""
Retrieve a spring by ID from the database.
"""
return get_thing_of_a_thing_type_by_id(session, request, thing_id)
@router.get(
"/id-link",
summary="Get all thing links",
)
def get_thing_id_links(
user: viewer_dependency,
session: session_dependency,
filter_: str = Query(alias="filter", default=None),
sort: str = None,
order: str = None,
) -> CustomPage[ThingIdLinkResponse]:
"""
Retrieve all thing links, optionally filtered and sorted.
"""
sql = select(ThingIdLink)
sql = order_sort_filter(sql, ThingIdLink, sort=sort, order=order, filter_=filter_)
return paginate(query=sql, conn=session)
@public_route
@router.get("/id-link/{link_id}", summary="Get thing links by link ID")
def get_thing_id_links(
user: viewer_dependency,
link_id: int,
session: session_dependency,
) -> ThingIdLinkResponse:
"""
Retrieve all links for a specific thing by its ID.
"""
return simple_get_by_id(session, ThingIdLink, link_id)
@public_route
@router.get("", summary="Get all things", status_code=HTTP_200_OK)
def get_things(
user: viewer_dependency,
session: session_dependency,
within: Optional[str] = None,
query: Optional[str] = None,
sort: Optional[str] = None,
order: Optional[str] = None,
include_contacts: bool = False,
filter_params: Annotated[list[str] | None, Query(alias="filter")] = None,
name_contains: Optional[str] = None,
) -> CustomPage[ThingResponse]:
"""
Retrieve all things or filter by type.
"""
return get_db_things(
None,
order,
query,
session,
sort,
within=within,
include_contacts=include_contacts,
filters=filter_params,
name_contains=name_contains,
)
@router.get("/{thing_id}", summary="Get thing by ID", status_code=HTTP_200_OK)
def get_thing_by_id(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
request: Request,
) -> ThingResponse:
"""
Retrieve a thing by ID from the database.
"""
thing = simple_get_by_id(session, Thing, thing_id)
return thing
@router.get("/{thing_id}/id-link", summary="Get thing links by thing ID")
def get_thing_id_links(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
) -> CustomPage[ThingIdLinkResponse]:
"""
Retrieve all links for a specific thing by its ID.
"""
thing = simple_get_by_id(session, Thing, thing_id)
sql = select(ThingIdLink).where(ThingIdLink.thing_id == thing.id)
return paginate(query=sql, conn=session)
@router.get("/{thing_id}/deployment", summary="Get deployments by thing ID")
def get_thing_deployments(
user: viewer_dependency,
thing_id: int,
session: session_dependency,
) -> CustomPage[DeploymentResponse]:
"""
Retrieve all deployments for a specific thing by its ID.
"""
thing = simple_get_by_id(session, Thing, thing_id)
sql = select(Deployment).where(Deployment.thing_id == thing.id)
sql = sql.options(selectinload(Deployment.sensor))
return paginate(query=sql, conn=session)
# POST ========================================================================
@router.post(
"/id-link", status_code=HTTP_201_CREATED, summary="Create a new thing link"
)
def create_thing_id_link(
link_data: CreateThingIdLink,
session: session_dependency,
user: admin_dependency,
) -> ThingIdLinkResponse:
"""
Create a new link between a thing and an alternate ID.
"""
try:
return model_adder(session, ThingIdLink, link_data, user=user)
except (IntegrityError, ProgrammingError) as e:
database_error_handler(link_data, e)
@router.post(
"/water-well",
summary="Create a water well",
status_code=HTTP_201_CREATED,
)
def create_well(
thing_data: CreateWell,
session: session_dependency,
request: Request,
user: admin_dependency,
) -> WellResponse:
"""
Create a new water well in the database.
"""
try:
thing = add_thing(session=session, data=thing_data, request=request, user=user)
modify_well_descriptor_tables(session, thing, thing_data, user)
return thing
except (IntegrityError, ProgrammingError) as e:
database_error_handler(thing_data, e)
@router.post(
"/spring",
summary="Create a new spring",
status_code=HTTP_201_CREATED,
)
def create_spring(
thing_data: CreateSpring,
session: session_dependency,
request: Request,
user: admin_dependency,
) -> SpringResponse:
"""
Create a new well in the database.
"""
try:
return add_thing(session=session, data=thing_data, request=request, user=user)
except (IntegrityError, ProgrammingError) as e:
database_error_handler(thing_data, e)
@router.post(
"/well-screen",
summary="Create a new well screen",
status_code=HTTP_201_CREATED,
)
def create_wellscreen(
session: session_dependency,
user: admin_dependency,
well_screen_data: CreateWellScreen,
) -> WellScreenResponse:
"""
Create a new well screen in the database.
"""
try:
return add_well_screen(session, well_screen_data, user=user)
except (IntegrityError, ProgrammingError) as e:
database_error_handler(well_screen_data, e)
except PydanticStyleException as e:
raise e
# PATCH ========================================================================
@router.patch(
"/water-well/{thing_id}",
summary="Update well by parent thing ID",
status_code=HTTP_200_OK,
)
def update_water_well(
thing_id: int,
thing_data: UpdateWell,
session: session_dependency,
user: editor_dependency,
request: Request,
) -> WellResponse:
"""
Update an existing well by ID.
"""
well_descriptor_data = thing_data.model_copy(deep=True)
# remove these fields from payload otherwise patch_thing will try to process
# and raise an error because they are not found in the Thing model
for field in WELL_DESCRIPTOR_MODEL_MAP.keys():
if hasattr(thing_data, field):
delattr(thing_data, field)
thing = patch_thing(session, request, thing_id, thing_data, user=user)
modify_well_descriptor_tables(session, thing, well_descriptor_data, user)
return thing
@router.patch(
"/spring/{thing_id}",
summary="Update spring by parent thing ID",
status_code=HTTP_200_OK,
)
def update_spring(
thing_id: int,
thing_data: UpdateSpring,
session: session_dependency,
user: editor_dependency,
request: Request,
) -> SpringResponse:
"""
Update an existing spring by ID.
"""
return patch_thing(session, request, thing_id, thing_data, user=user)
@router.patch(
"/id-link/{link_id}", summary="Update thing link by ID", status_code=HTTP_200_OK
)
def update_thing_id_link(
link_id: int,
link_data: UpdateThingIdLink,
session: session_dependency,
user: editor_dependency,
) -> ThingIdLinkResponse:
return model_patcher(session, ThingIdLink, link_id, link_data, user=user)
@router.patch(
"/well-screen/{well_screen_id}",
summary="Update Well Screen by ID",
status_code=HTTP_200_OK,
)
def update_well_screen(
well_screen_id: int,
well_screen_data: UpdateWellScreen,
session: session_dependency,
user: editor_dependency,
) -> WellScreenResponse:
# TODO: add validation
return model_patcher(
session, WellScreen, well_screen_id, well_screen_data, user=user
)
# DELETE =======================================================================
@router.delete(
"/{thing_id}", summary="Delete thing by ID", status_code=HTTP_204_NO_CONTENT
)
def delete_thing(
thing_id: int,
session: session_dependency,
user: admin_dependency,
) -> None:
"""
Delete a thing by ID.
"""
return model_deleter(session, Thing, thing_id)
@router.delete(
"/well-screen/{well_screen_id}",
summary="Delete well screen by ID",
status_code=HTTP_204_NO_CONTENT,
)
def delete_well_screen(
well_screen_id: int,
session: session_dependency,
user: admin_dependency,
) -> None:
"""
Delete a well screen by ID.
"""
return model_deleter(session, WellScreen, well_screen_id)
@router.delete(
"/id-link/{link_id}",
summary="Delete thing link by ID",
status_code=HTTP_204_NO_CONTENT,
)
def delete_thing_id_link(
link_id: int,
session: session_dependency,
user: admin_dependency,
) -> None:
"""
Delete a thing link by ID.
"""
return model_deleter(session, ThingIdLink, link_id)
# ============= EOF =============================================