diff --git a/gallery/easel/depends/__init__.py b/gallery/easel/depends/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gallery/easel/depends/weather.py b/gallery/easel/depends/weather.py new file mode 100644 index 0000000..e0db605 --- /dev/null +++ b/gallery/easel/depends/weather.py @@ -0,0 +1,13 @@ +from typing import Annotated + +from fastapi import Depends + +from gallery.easel.core import AppRequest +from gallery.sketch.weather.api import WeatherApi + + +async def get_weather_api(request: AppRequest, provider: str | None = None) -> WeatherApi: + return request.app.state.api.get_weather(provider) + + +WeatherApiDepends = Annotated[WeatherApi, Depends(get_weather_api)] diff --git a/gallery/easel/route/api/weather.py b/gallery/easel/route/api/weather.py index 4c7e1f7..ee745ca 100644 --- a/gallery/easel/route/api/weather.py +++ b/gallery/easel/route/api/weather.py @@ -3,6 +3,7 @@ import datetime from fastapi import APIRouter from gallery.easel.core import AppRequest +from gallery.easel.depends.weather import WeatherApiDepends from gallery.sketch.weather.model import Location, WeatherResponse router = APIRouter(prefix="/weather") @@ -13,19 +14,16 @@ async def get_api_weather_providers(request: AppRequest) -> list[str]: return request.app.state.api.get_api_providers("weather") -@router.get("/locations") -async def get_api_weather_locations(request: AppRequest, query: str) -> list[Location]: - weather_api = request.app.state.api.weather +@router.get("/{provider}/locations") +async def get_api_weather_locations(weather_api: WeatherApiDepends, query: str) -> list[Location]: return await weather_api.find_locations(query) -@router.get("/{location}/day/{date}") -async def get_api_weather_day(request: AppRequest, location: str, date: datetime.date) -> WeatherResponse: - weather_api = request.app.state.api.weather +@router.get("/{provider}/{location}/day/{date}") +async def get_api_weather_day(weather_api: WeatherApiDepends, location: str, date: datetime.date) -> WeatherResponse: return await weather_api.get_day(location, date) -@router.get("/{location}/days/{days}") -async def get_api_weather_days(request: AppRequest, location: str, days: int) -> WeatherResponse: - weather_api = request.app.state.api.weather +@router.get("/{provider}/{location}/days/{days}") +async def get_api_weather_days(weather_api: WeatherApiDepends, location: str, days: int) -> WeatherResponse: return await weather_api.get_days(location, days) diff --git a/gallery/easel/route/view/weather/__init__.py b/gallery/easel/route/view/weather/__init__.py index eaebf8f..5da3f61 100644 --- a/gallery/easel/route/view/weather/__init__.py +++ b/gallery/easel/route/view/weather/__init__.py @@ -1,11 +1,11 @@ import datetime from pathlib import Path -from typing import Annotated -from fastapi import APIRouter, Depends +from fastapi import APIRouter from fastapi.responses import HTMLResponse, RedirectResponse from gallery.easel.core import AppRequest +from gallery.easel.depends.weather import WeatherApiDepends from gallery.sketch.weather.api import WeatherApi from gallery.sketch.weather.model import WeatherResponse @@ -14,13 +14,6 @@ from ..common.utils.template import build_templates from .filters import cloudness_icon, wind_direction_icon -async def get_weather_api(request: AppRequest, provider: str | None = None) -> WeatherApi: - return request.app.state.api.get_weather(provider) - - -WeatherApiDepends = Annotated[WeatherApi, Depends(get_weather_api)] - - def context_procesor(request: AppRequest) -> dict: return { "providers": request.app.state.api.get_api_providers(WeatherApi.TYPE), @@ -94,3 +87,10 @@ async def get_weather_tag(request: AppRequest, weather_api: WeatherApiDepends, l else: raise ValueError(tag) return build_weather_response(request, response) + + +@router.get("/zmiyevka-184640/tag/{tag}", response_class=RedirectResponse) +async def get_zmiyevka_weather_compat(request: AppRequest): + path_parts = request.url.path.split("/") + path_parts.insert(2, "gismeteo") + return RedirectResponse("/".join(path_parts)) diff --git a/gallery/painting/openweather/openweather.py b/gallery/painting/openweather/openweather.py index afe34a5..3f53807 100644 --- a/gallery/painting/openweather/openweather.py +++ b/gallery/painting/openweather/openweather.py @@ -69,7 +69,7 @@ class Forecast(Model): class Location(Model): name: str - local_names: dict[str, str] + local_names: dict[str, str] | None = None lat: float lon: float country: str diff --git a/static/src/weather/weather.ts b/static/src/weather/weather.ts index 9a0195c..878fae3 100644 --- a/static/src/weather/weather.ts +++ b/static/src/weather/weather.ts @@ -85,6 +85,7 @@ class WeatherLocationElement extends HTMLElement { disconnectedCallback() { this.removeEventListener("click", this.handleClick); + this.querySelector("[data-remove]")?.removeEventListener("click", this.handleRemoveClick); } handleClick(event: MouseEvent) {