Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions api/thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,18 +154,26 @@ 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
filter_,
order,
query,
session,
sort,
name=name,
thing_type=thing_type,
include_contacts=include_contacts,
)


Expand Down Expand Up @@ -348,11 +357,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",
Expand All @@ -369,6 +378,7 @@ async def get_things(
session,
sort,
within=within,
include_contacts=include_contacts,
)


Expand Down
23 changes: 18 additions & 5 deletions services/thing_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +43,7 @@
ThingIdLink,
MonitoringFrequencyHistory,
StatusHistory,
search,
)
from services.audit_helper import audit_add
from services.crud_helper import model_patcher
Expand Down Expand Up @@ -116,13 +117,18 @@ 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:
sql = select(Thing).where(make_query(Thing, query))
sql = search(
select(Thing),
query,
vector=Thing.search_vector,
)
else:
sql = select(Thing)

Expand All @@ -135,6 +141,13 @@ 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)

Expand Down
Loading