Name an unknown circuit, layout or year with a 404 instead of a 500

The circuit and track routes walked circuits.json with chained lookups, so an unknown country, locality, circuit
or layout, or a year no layout covers, raised and answered 500 (/circuits/photos.json among them). One lookup now
names the first unknown part and the API answers it 404. numpy loads only where a script uses it, so the models,
and this lookup's test, need the standard library alone.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
2026-11-27 15:13:34 +01:00
parent db5c19f0d4
commit d514eb57a3
5 changed files with 106 additions and 44 deletions
+51 -13
View File
@@ -12,6 +12,11 @@ from models.model_builder import parse_circuits_json
from models.track_layout import TrackLayout
class NotFound(LookupError):
"""A country, locality, circuit or layout circuits.json does not hold, or a year no layout covers: answered 404,
naming it."""
# Define supported formats
class Format(str, Enum):
PNG = "png"
@@ -69,6 +74,31 @@ class CircuitService:
return layout_slug
return None
def resolve(self, country_slug: str, locality_slug: Optional[str] = None, circuit_slug: Optional[str] = None,
layout_slug: Optional[str] = None):
"""The country, locality, circuit or layout the given slugs lead to (the deepest one given); NotFound names
the first that circuits.json does not hold."""
found = self.circuit_data.get(country_slug)
if found is None:
raise NotFound(f"No country {country_slug!r}")
for kind, slug, children in (("locality", locality_slug, "localities"), ("circuit", circuit_slug, "circuits"),
("layout", layout_slug, "layouts")):
if slug is None:
return found
parent = found
found = getattr(parent, children).get(slug)
if found is None:
raise NotFound(f"No {kind} {slug!r} in {getattr(parent, 'name', None) or getattr(parent, 'slug', '')}")
return found
def layout_for_year(self, country_slug: str, locality_slug: str, circuit_slug: str, year: int) -> TrackLayout:
"""The layout a circuit used in a year; NotFound when the circuit is unknown or no layout covers the year."""
circuit = self.resolve(country_slug, locality_slug, circuit_slug)
layout_slug = self.find_layout_slug_for_year(circuit, year)
if layout_slug is None:
raise NotFound(f"No layout of {circuit.name} covers {year}")
return circuit.layouts[layout_slug]
def get_countries_list(self) -> Optional[list[Country]]:
"""Get list of all available countries with their slugs"""
return [country for _, country in self.circuit_data.items()]
@@ -77,26 +107,34 @@ class CircuitService:
"""Get details for a specific country"""
return self.circuit_data.get(country_slug)
def get_localities_list(self, country_slug: str) -> Optional[list[Locality]]:
"""Get list of cities for a specific country"""
return [locality for _, locality in self.circuit_data[country_slug].localities.items()]
def get_localities_list(self, country_slug: str) -> list[Locality]:
"""The localities of a country; NotFound when it is unknown"""
return list(self.resolve(country_slug).localities.values())
def get_locality_details(self, country_slug: str, locality_slug: str) -> Optional[Locality]:
"""Get details for a specific city"""
return self.circuit_data[country_slug].localities.get(locality_slug)
"""Get details for a specific city; None when it is unknown"""
try:
return self.resolve(country_slug, locality_slug)
except NotFound:
return None
def get_circuits_list(self, country_slug: str, locality_slug: str) -> Optional[list[Circuit]]:
"""Get list of circuits for a specific city"""
return [circuit_data for _, circuit_data in self.circuit_data[country_slug].localities.get(locality_slug).circuits.items()]
def get_circuits_list(self, country_slug: str, locality_slug: str) -> list[Circuit]:
"""The circuits of a locality; NotFound when it is unknown"""
return list(self.resolve(country_slug, locality_slug).circuits.values())
def get_circuit_details(self, country_slug: str, locality_slug: str, circuit_slug: str) -> Optional[Circuit]:
"""Get circuit details and available formats"""
return self.circuit_data[country_slug].localities.get(locality_slug).circuits.get(circuit_slug)
"""Get circuit details and available formats; None when it is unknown"""
try:
return self.resolve(country_slug, locality_slug, circuit_slug)
except NotFound:
return None
def get_layout_details(self, country_slug: str, locality_slug: str, circuit_slug: str, layout_slug: str) -> Optional[TrackLayout]:
"""Get details for a specific layout"""
return self.circuit_data[country_slug].localities.get(locality_slug).circuits.get(circuit_slug).layouts.get(
layout_slug)
"""Get details for a specific layout; None when it is unknown"""
try:
return self.resolve(country_slug, locality_slug, circuit_slug, layout_slug)
except NotFound:
return None
def get_circuit_layout_by_ergast_data(self, grand_prix_name: str, season: int) -> Optional[TrackLayout]:
"""