From 5cfe3980744ed482c157fa8a046a75aeeda2122c Mon Sep 17 00:00:00 2001 From: Tyler Adam Martinez Date: Fri, 10 Apr 2026 10:57:14 -0500 Subject: [PATCH 1/3] feat(api/thing): Add optional flag to see contacts --- api/thing.py | 25 +++++++++++++------------ services/thing_helper.py | 15 +++++++++++---- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/api/thing.py b/api/thing.py index 0babb871..ce5e6ec2 100644 --- a/api/thing.py +++ b/api/thing.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # =============================================================================== +from typing import Optional from fastapi import APIRouter, Query, Request from fastapi_pagination.ext.sqlalchemy import paginate from sqlalchemy import select @@ -153,19 +154,18 @@ async def get_water_wells( user: viewer_dependency, session: session_dependency, request: Request, - sort: str = None, - order: str = None, + sort: Optional[str] = None, + order: Optional[str] = None, filter_: str = Query(alias="filter", default=None), - query: str = None, - name: str = None, + query: Optional[str] = None, + name: 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( - filter_, order, query, session, sort, name=name, thing_type=thing_type - ) + return get_db_things(filter_, order, query, session, sort, name=name, thing_type=thing_type, include_contacts=include_contacts) @router.get( @@ -348,11 +348,11 @@ async def get_thing_id_links( async def get_things( user: viewer_dependency, session: session_dependency, - # thing_id: int = None, - within: str = None, - query: str = None, - sort: str = None, - order: str = None, + within: Optional[str] = None, + query: Optional[str] = None, + sort: Optional[str] = None, + order: Optional[str] = None, + include_contacts: bool = False, filter_: str = Query( default=None, alias="filter", @@ -369,6 +369,7 @@ async def get_things( session, sort, within=within, + include_contacts=include_contacts, ) diff --git a/services/thing_helper.py b/services/thing_helper.py index 68218a95..e96b208e 100644 --- a/services/thing_helper.py +++ b/services/thing_helper.py @@ -16,7 +16,7 @@ import logging import time from datetime import datetime -from typing import Sequence +from typing import Sequence, Optional from zoneinfo import ZoneInfo from fastapi import Request, HTTPException @@ -116,9 +116,10 @@ def get_db_things( query, session, sort, - thing_type: str = None, - within: str = None, - name: str = None, + thing_type: Optional[str] = None, + within: Optional[str] = None, + name: Optional[str] = None, + include_contacts: bool = False, ) -> list: if query: @@ -135,6 +136,12 @@ def get_db_things( # add all eager loads for generic thing query until/unless GET /thing is deprecated sql = sql.options(*WATER_WELL_LOADER_OPTIONS) + if include_contacts: + sql = sql.options( + selectinload(Thing.contact_associations) + .selectinload(ThingContactAssociation.contact) + ) + if name: sql = sql.where(Thing.name == name) From 03cebb9fb73d5cd2033c444c54537b9b4f560615 Mon Sep 17 00:00:00 2001 From: Tyler Adam Martinez Date: Fri, 10 Apr 2026 13:11:09 -0500 Subject: [PATCH 2/3] feat(thing_helper): Query now go through search --- services/thing_helper.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/thing_helper.py b/services/thing_helper.py index e96b208e..0360babf 100644 --- a/services/thing_helper.py +++ b/services/thing_helper.py @@ -43,6 +43,7 @@ ThingIdLink, MonitoringFrequencyHistory, StatusHistory, + search, ) from services.audit_helper import audit_add from services.crud_helper import model_patcher @@ -123,7 +124,11 @@ def get_db_things( ) -> list: if query: - sql = select(Thing).where(make_query(Thing, query)) + sql = search( + select(Thing), + query, + vector=Thing.search_vector, + ) else: sql = select(Thing) From 284a2f2eb433f72824fdf8f2874f219015283509 Mon Sep 17 00:00:00 2001 From: TylerAdamMartinez <57375362+TylerAdamMartinez@users.noreply.github.com> Date: Fri, 10 Apr 2026 18:12:40 +0000 Subject: [PATCH 3/3] Formatting changes --- api/thing.py | 11 ++++++++++- services/thing_helper.py | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/api/thing.py b/api/thing.py index ce5e6ec2..5b8a52e1 100644 --- a/api/thing.py +++ b/api/thing.py @@ -165,7 +165,16 @@ async def get_water_wells( Retrieve all wells from the database. """ thing_type = request.url.path.split("/")[2].replace("-", " ") - return get_db_things(filter_, order, query, session, sort, name=name, thing_type=thing_type, include_contacts=include_contacts) + return get_db_things( + filter_, + order, + query, + session, + sort, + name=name, + thing_type=thing_type, + include_contacts=include_contacts, + ) @router.get( diff --git a/services/thing_helper.py b/services/thing_helper.py index 0360babf..652a893c 100644 --- a/services/thing_helper.py +++ b/services/thing_helper.py @@ -143,8 +143,9 @@ def get_db_things( if include_contacts: sql = sql.options( - selectinload(Thing.contact_associations) - .selectinload(ThingContactAssociation.contact) + selectinload(Thing.contact_associations).selectinload( + ThingContactAssociation.contact + ) ) if name: