feat(api): translate GIAS codes to names at the query boundary
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+39
-5
@@ -21,6 +21,38 @@ from .models import (
|
||||
FactDeprivation, FactFinance, FactPupilCharacteristics,
|
||||
)
|
||||
from .schemas import SCHOOL_TYPE_MAP
|
||||
from .gias_codes import (
|
||||
ADMISSIONS_POLICY,
|
||||
ESTABLISHMENT_STATUS,
|
||||
PHASE_OF_EDUCATION,
|
||||
RELIGIOUS_CHARACTER,
|
||||
SCHOOL_TYPE,
|
||||
translate,
|
||||
)
|
||||
|
||||
# mart code column -> (API name column, dictionary)
|
||||
_GIAS_CODE_COLUMNS = {
|
||||
"phase_code": ("phase", PHASE_OF_EDUCATION),
|
||||
"school_type_code": ("school_type", SCHOOL_TYPE),
|
||||
"status_code": ("status", ESTABLISHMENT_STATUS),
|
||||
"religious_character_code": ("religious_denomination", RELIGIOUS_CHARACTER),
|
||||
"admissions_policy_code": ("admissions_policy", ADMISSIONS_POLICY),
|
||||
}
|
||||
|
||||
|
||||
def translate_gias_code_columns(df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Map GIAS code columns to today's name columns (API contract).
|
||||
|
||||
Runs immediately after pd.read_sql so every downstream consumer —
|
||||
filters, PHASE_GROUPS, payloads, /api/filters — keeps seeing names.
|
||||
DataFrames without the code columns (old schema, test fixtures) pass
|
||||
through unchanged.
|
||||
"""
|
||||
for code_col, (name_col, mapping) in _GIAS_CODE_COLUMNS.items():
|
||||
if code_col in df.columns:
|
||||
df[name_col] = df[code_col].map(lambda c: translate(c, mapping))
|
||||
return df
|
||||
|
||||
|
||||
_postcode_cache: Dict[str, Tuple[float, float]] = {}
|
||||
_typesense_client = None
|
||||
@@ -121,16 +153,16 @@ _MAIN_QUERY = text("""
|
||||
SELECT
|
||||
s.urn,
|
||||
s.school_name,
|
||||
s.phase,
|
||||
s.school_type,
|
||||
s.phase_code,
|
||||
s.school_type_code,
|
||||
s.academy_trust_name AS trust_name,
|
||||
s.academy_trust_uid AS trust_uid,
|
||||
s.religious_character AS religious_denomination,
|
||||
s.religious_character_code,
|
||||
s.gender,
|
||||
s.age_range,
|
||||
s.has_sixth_form,
|
||||
s.status,
|
||||
s.admissions_policy,
|
||||
s.status_code,
|
||||
s.admissions_policy_code,
|
||||
s.capacity,
|
||||
s.total_pupils AS gias_total_pupils,
|
||||
s.headteacher_name,
|
||||
@@ -256,6 +288,8 @@ def load_school_data_as_dataframe() -> pd.DataFrame:
|
||||
if df.empty:
|
||||
return df
|
||||
|
||||
df = translate_gias_code_columns(df)
|
||||
|
||||
# Build address string
|
||||
df["address"] = df.apply(
|
||||
lambda r: ", ".join(
|
||||
|
||||
Reference in New Issue
Block a user