Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgorrie committed Nov 28, 2024
1 parent 5061ac3 commit 561d627
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 280 deletions.
2 changes: 1 addition & 1 deletion app/api/api_v1/routers/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async def search_family(request: Request) -> list[FamilyReadDTO]:

query_params = set_default_query_params(query_params)

VALID_PARAMS = ["q", "title", "summary", "geography", "status", "max_results"]
VALID_PARAMS = ["q", "title", "summary", "geographies", "status", "max_results"]
validate_query_params(query_params, VALID_PARAMS)

try:
Expand Down
8 changes: 4 additions & 4 deletions app/model/family.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Optional, Union
from typing import Optional

from pydantic import BaseModel

Expand All @@ -12,7 +12,7 @@ class FamilyReadDTO(BaseModel):
import_id: str
title: str
summary: str
geography: str
geographies: list[str]
category: str
status: str
metadata: Json
Expand Down Expand Up @@ -41,7 +41,7 @@ class FamilyWriteDTO(BaseModel):

title: str
summary: str
geography: str
geographies: list[str]
category: str
metadata: Json
collections: list[str]
Expand All @@ -60,7 +60,7 @@ class FamilyCreateDTO(BaseModel):
import_id: Optional[str] = None
title: str
summary: str
geography: Union[str, list[str]]
geographies: list[str]
category: str
metadata: Json
collections: list[str]
Expand Down
34 changes: 12 additions & 22 deletions app/repository/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from db_client.models.organisation.users import Organisation
from sqlalchemy import Column, and_
from sqlalchemy import delete as db_delete
from sqlalchemy import desc, func, or_
from sqlalchemy import desc, or_
from sqlalchemy import update as db_update
from sqlalchemy.exc import NoResultFound, OperationalError
from sqlalchemy.orm import Query, Session
Expand All @@ -34,30 +34,24 @@

_LOGGER = logging.getLogger(__name__)

FamilyGeoMetaOrg = Tuple[Family, str, FamilyMetadata, Corpus, Organisation]
FamilyGeoMetaOrg = Tuple[Family, Geography, FamilyMetadata, Corpus, Organisation]


def _get_query(db: Session) -> Query:
# NOTE: SqlAlchemy will make a complete hash of query generation
# if columns are used in the query() call. Therefore, entire
# objects are returned.
geo_subquery = (
db.query(
func.min(Geography.value).label("value"),
FamilyGeography.family_import_id,
)
.join(FamilyGeography, FamilyGeography.geography_id == Geography.id)
.filter(FamilyGeography.family_import_id == Family.import_id)
.group_by(Geography.value, FamilyGeography.family_import_id)
).subquery("geo_subquery")

return (
db.query(Family, geo_subquery.c.value, FamilyMetadata, Corpus, Organisation) # type: ignore
db.query(Family, Geography, FamilyMetadata, Corpus, Organisation) # type: ignore
.join(FamilyGeography, FamilyGeography.family_import_id == Family.import_id)
.join(
Geography,
Geography.id == FamilyGeography.geography_id,
)
.join(FamilyMetadata, FamilyMetadata.family_import_id == Family.import_id)
.join(FamilyCorpus, FamilyCorpus.family_import_id == Family.import_id)
.join(Corpus, Corpus.import_id == FamilyCorpus.corpus_import_id)
.join(Organisation, Corpus.organisation_id == Organisation.id)
.filter(geo_subquery.c.family_import_id == Family.import_id) # type: ignore
)


Expand All @@ -72,7 +66,7 @@ def _family_to_dto(
import_id=str(fam.import_id),
title=str(fam.title),
summary=str(fam.description),
geography=geo_value,
geographies=[str(geo_value.display_value)],
category=str(fam.family_category),
status=str(fam.family_status),
metadata=metadata,
Expand Down Expand Up @@ -201,13 +195,9 @@ def search(
term = f"%{escape_like(search_params['summary'])}%"
search.append(Family.description.ilike(term))

if "geography" in search_params.keys():
term = cast(str, search_params["geography"])
search.append(
or_(
Geography.display_value == term.title(), Geography.value == term.upper()
)
)
if "geographies" in search_params.keys():
term = cast(str, search_params["geographies"])
search.append(Geography.display_value == term.title())

if "status" in search_params.keys():
term = cast(str, search_params["status"])
Expand Down
Loading

0 comments on commit 561d627

Please sign in to comment.