Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter species at api endpoints #73

Merged
merged 2 commits into from
Oct 30, 2023
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
7 changes: 4 additions & 3 deletions app/api/v1/endpoints/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.api import deps
from app.db.base_class import Base
from app.models import SpeciesCommonNames, SpeciesSynonyms, stations_species_table
from app.utils.species import binomial_only

router = APIRouter()

Expand All @@ -30,7 +31,7 @@ def read_all_species(
) -> Any:
"""Retrieve all species."""
species = crud.species.get_all(db, order_by=order_by)
return species
return binomial_only(species)


@router.post("/search/", response_model=List[schemas.SpeciesSummary])
Expand All @@ -47,7 +48,7 @@ def read_species_by_search(
order_by=order_by,
limit=limit,
)
return species
return binomial_only(species)


@router.get("/fuzzymatch/", response_model=List[schemas.SpeciesSummary])
Expand Down Expand Up @@ -120,7 +121,7 @@ def read_fuzzy_species_by_search(
order_by=order_by,
limit=limit,
)
return species
return binomial_only(species)


@router.get(
Expand Down
9 changes: 7 additions & 2 deletions app/crud/crud_station.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from typing import Any, Optional
from typing import Any, Optional, cast

from sqlalchemy.orm import Session

from app.crud.base import CRUDBase
from app.models import Station
from app.schemas import StationCreate, StationSummaryPagination, StationUpdate
from app.utils.species import binomial_only


class CRUDStation(
CRUDBase[Station, StationCreate, StationUpdate, StationSummaryPagination]
):
def get(self, db: Session, id: Any) -> Optional[Station]:
return db.query(self.model).filter(self.model.name == id).first()
station = cast(
Station, db.query(self.model).filter(self.model.name == id).first()
)
station.species = binomial_only(station.species)
return station


station = CRUDStation(Station)
17 changes: 17 additions & 0 deletions app/utils/species.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List

from app.models import Species


def binomial_only(species: List[Species]) -> List[Species]:
"""
TODO: This is a temporary fix. In the future, we could consider filtering out genera
when loading data into the database or as part of the OCR workflow.
"""
# Unlike `current_name`, `current_canonical_full_name` doesn't include
# author(s) and year of publication, so for a genus, there won't be any spaces.
return [
sp
for sp in species
if sp.current_canonical_simple_name and " " in sp.current_canonical_simple_name
]