Skip to content

Commit

Permalink
Adjust zwzt models and add api for them
Browse files Browse the repository at this point in the history
  • Loading branch information
micorix committed May 29, 2023
1 parent a2ebacd commit cb0daeb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
8 changes: 1 addition & 7 deletions po8klasie_fastapi/app/api/institution/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from sqlalchemy import and_
from typing import List

from fastapi import APIRouter, Depends, HTTPException, Query
Expand All @@ -11,7 +10,7 @@
SecondarySchoolInstitution,
query_institutions,
)
from po8klasie_fastapi.app.rankings.zwzt.models import ZwzTRankingEntry, zwzt_ranking_entries_base_filters
from po8klasie_fastapi.app.rankings.zwzt.models import ZwzTRankingEntry

from po8klasie_fastapi.app.rspo_institution.models import RspoInstitution
from po8klasie_fastapi.db.db import get_db
Expand Down Expand Up @@ -51,11 +50,6 @@ def route_get_single_school(rspo: str, db: Session = Depends(get_db)):
institution = (
query_institutions(db, with_public_transport=True)
.filter(RspoInstitution.rspo == rspo)
# TODO(micorix): Abstract querying ranking results
.outerjoin(
ZwzTRankingEntry, and_(*zwzt_ranking_entries_base_filters)
)
.options(contains_eager(SecondarySchoolInstitution.zwzt_ranking_entries))
.one()
)

Expand Down
23 changes: 20 additions & 3 deletions po8klasie_fastapi/app/api/institution/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class InstitutionDetailsClassProfilesSectionSchema(BaseModel):

@validator("classes", pre=True)
def group_classes_by_year(
cls,
classes: Iterable[SecondarySchoolInstitutionClass]
| dict[int, list[SecondarySchoolInstitutionClass]],
cls,
classes: Iterable[SecondarySchoolInstitutionClass]
| dict[int, list[SecondarySchoolInstitutionClass]],
) -> dict[int, list[SecondarySchoolInstitutionClass]]:
if isinstance(classes, dict):
return classes
Expand All @@ -104,6 +104,22 @@ def get_year(c):
return grouped_classes


class ZwzTRankingEntrySchema(CamelCasedModel):
year: int

place_in_country: int
place_in_voivodeship: int

indicator_value: float

class Config:
orm_mode = True


class InstitutionDetailsZwzTSectionSchema(CamelCasedModel):
zwzt_ranking_entries: List[ZwzTRankingEntrySchema]


class InstitutionDetailsPublicTransportSectionSchema(BaseModel):
public_transport_stops: List[InstitutionPublicTransportStopAssociationSchema]

Expand All @@ -115,6 +131,7 @@ class InstitutionDetailsSchema(
InstitutionDetailsClassProfilesSectionSchema,
InstitutionDetailsSportsSectionSchema,
InstitutionDetailsPartnersSectionSchema,
InstitutionDetailsZwzTSectionSchema,
InstitutionDetailsPublicTransportSectionSchema,
CamelCasedModel,
InstitutionSourcingSchemaMixin,
Expand Down
56 changes: 36 additions & 20 deletions po8klasie_fastapi/app/api/search/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
from po8klasie_fastapi.app.rspo_institution.models import RspoInstitution


bbox_regex = r"^\d+\.\d+,\d+\.\d+,\d+\.\d+,\d+\.\d+$"


Expand All @@ -46,6 +45,7 @@ class FiltersQuerySchema(BaseModel):
rspo_institution_type: Optional[List[str]]
public_transportation_stop: Optional[List[str]]
extended_subjects: Optional[list[list[str]]]
featured_in_zwzt_ranking: Optional[bool]

@validator("extended_subjects", pre=True)
def preprocess_extended_subjects(cls, raw: str):
Expand All @@ -62,16 +62,17 @@ class FiltersQuery:
model: FiltersQuerySchema = None

def __init__(
self,
project_id: str | None = None,
query: str | None = None,
is_public: bool | None = None,
languages: List[str] = Query(default=None),
points_threshold: List[int] = Query(default=None),
rspo_institution_type: list[str] = Query(default=None),
public_transportation_stop: list[str] = Query(default=None),
extended_subjects: str | None = None,
bbox: str | None = Query(regex=bbox_regex, default=None),
self,
project_id: str | None = None,
query: str | None = None,
is_public: bool | None = None,
languages: List[str] = Query(default=None),
points_threshold: List[int] = Query(default=None),
rspo_institution_type: list[str] = Query(default=None),
public_transportation_stop: list[str] = Query(default=None),
extended_subjects: str | None = None,
bbox: str | None = Query(regex=bbox_regex, default=None),
featured_in_zwzt_ranking: bool | None = None
):
self.filters_query_dict = {
"project_id": project_id,
Expand All @@ -82,6 +83,7 @@ def __init__(
"rspo_institution_type": rspo_institution_type,
"public_transportation_stop": public_transportation_stop,
"extended_subjects": extended_subjects,
"featured_in_zwzt_ranking": featured_in_zwzt_ranking,
"bbox": bbox,
}
self.model = FiltersQuerySchema.parse_obj(self.filters_query_dict)
Expand All @@ -92,7 +94,7 @@ def get_qs(self) -> str:


def filter_by_query(
institutions: SQLAlchemyQuery, query: str | None
institutions: SQLAlchemyQuery, query: str | None
) -> SQLAlchemyQuery:
if not query:
return institutions
Expand All @@ -102,15 +104,15 @@ def filter_by_query(


def filter_by_project_id(
institutions: SQLAlchemyQuery, project_id: str | None
institutions: SQLAlchemyQuery, project_id: str | None
) -> SQLAlchemyQuery:
if not project_id:
return institutions
return institutions.filter(SecondarySchoolInstitution.project_id == project_id)


def filter_by_languages(
institutions: SQLAlchemyQuery, languages: List[str] | None
institutions: SQLAlchemyQuery, languages: List[str] | None
) -> SQLAlchemyQuery:
if not languages:
return institutions
Expand All @@ -120,15 +122,15 @@ def filter_by_languages(


def filter_by_is_public(
institutions: SQLAlchemyQuery, is_public: bool | None
institutions: SQLAlchemyQuery, is_public: bool | None
) -> SQLAlchemyQuery:
if is_public is None:
return institutions
return institutions.filter(RspoInstitution.is_public == is_public)


def filter_by_rspo_institution_type(
institutions: SQLAlchemyQuery, rspo_institution_type: list[str] | None
institutions: SQLAlchemyQuery, rspo_institution_type: list[str] | None
):
if not rspo_institution_type:
return institutions
Expand All @@ -150,7 +152,7 @@ def filter_by_bbox(institutions: SQLAlchemyQuery, bbox: str | None) -> SQLAlchem


def filter_by_points_threshold(
institutions: SQLAlchemyQuery, points_threshold: list[int] | None
institutions: SQLAlchemyQuery, points_threshold: list[int] | None
) -> SQLAlchemyQuery:
if not points_threshold or len(points_threshold) != 2:
return institutions
Expand All @@ -169,7 +171,7 @@ def filter_by_points_threshold(


def filter_by_extended_subjects(
institutions: SQLAlchemyQuery, extended_subjects_list: list[list[str]] | None
institutions: SQLAlchemyQuery, extended_subjects_list: list[list[str]] | None
) -> SQLAlchemyQuery:
if not extended_subjects_list:
return institutions
Expand All @@ -191,8 +193,18 @@ def filter_by_extended_subjects(
return institutions.filter(SecondarySchoolInstitution.classes.any(or_(*conditions)))


def filter_by_featured_in_zwzt_ranking(
institutions: SQLAlchemyQuery, featured_in_zwzt_ranking: bool | None
) -> SQLAlchemyQuery:
if not featured_in_zwzt_ranking:
return institutions
return institutions.filter(
SecondarySchoolInstitution.zwzt_ranking_entries.any()
)


def filter_by_public_transport_route_type(
institutions: SQLAlchemyQuery, public_transport_route_type: list[str] | None
institutions: SQLAlchemyQuery, public_transport_route_type: list[str] | None
) -> SQLAlchemyQuery:
if not public_transport_route_type:
return institutions
Expand All @@ -204,7 +216,7 @@ def filter_by_public_transport_route_type(


def filter_institutions(
db: Session, filters_query: FiltersQuerySchema
db: Session, filters_query: FiltersQuerySchema
) -> SQLAlchemyQuery:
institutions: SQLAlchemyQuery = query_institutions(db, with_public_transport=True)

Expand Down Expand Up @@ -232,6 +244,10 @@ def filter_institutions(
institutions, filters_query.public_transportation_stop
)

institutions = filter_by_featured_in_zwzt_ranking(
institutions, filters_query.featured_in_zwzt_ranking
)

institutions = filter_by_bbox(institutions, filters_query.bbox)

return institutions
6 changes: 5 additions & 1 deletion po8klasie_fastapi/app/institution/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from po8klasie_fastapi.app.rspo_institution.models import RspoInstitution

import po8klasie_fastapi.app.rankings.zwzt.models
from po8klasie_fastapi.app.rankings.zwzt.models import ZwzTRankingEntry


class InstitutionTypeGeneralizedEnum(enum.Enum):
Expand Down Expand Up @@ -115,6 +115,10 @@ def query_institutions(
SecondarySchoolInstitutionClass, and_(*classes_base_filters)
)
.options(contains_eager(SecondarySchoolInstitution.classes))
.outerjoin(
ZwzTRankingEntry, ZwzTRankingEntry.institution_rspo == RspoInstitution.rspo
)
.options(contains_eager(SecondarySchoolInstitution.zwzt_ranking_entries))
.populate_existing()
)

Expand Down
1 change: 0 additions & 1 deletion po8klasie_fastapi/app/rankings/zwzt/consts.py

This file was deleted.

8 changes: 0 additions & 8 deletions po8klasie_fastapi/app/rankings/zwzt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship

from po8klasie_fastapi.app.rankings.zwzt.consts import ZWZT_LATEST_RANKING_EDITION_YEAR
from po8klasie_fastapi.app.rspo_institution.models import RspoInstitution
from po8klasie_fastapi.db.base import Base


Expand All @@ -24,9 +22,3 @@ class ZwzTRankingEntry(Base):

institution_rspo = Column(String, ForeignKey("secondary_school_institutions.rspo"))
institution = relationship("SecondarySchoolInstitution", back_populates="zwzt_ranking_entries")


zwzt_ranking_entries_base_filters = [
RspoInstitution.rspo == ZwzTRankingEntry.institution_rspo,
ZwzTRankingEntry.year == ZWZT_LATEST_RANKING_EDITION_YEAR,
]

0 comments on commit cb0daeb

Please sign in to comment.