feat(app): add html weather view
This commit is contained in:
43
gismeteo/route/view/__init__.py
Normal file
43
gismeteo/route/view/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from gismeteo import dateutil
|
||||
from gismeteo.api import GismeteoApi
|
||||
from gismeteo.mock import MOCK_DATA
|
||||
|
||||
from .filters import cloudness_icon, wind_direction_icon
|
||||
|
||||
|
||||
def mount(app: FastAPI):
|
||||
base_dir = Path(__file__).parent
|
||||
app.mount("/static", StaticFiles(directory=base_dir / "static"), name="static")
|
||||
templates = Jinja2Templates(directory=base_dir / "templates")
|
||||
templates.env.filters["wind_direction_icon"] = wind_direction_icon
|
||||
templates.env.filters["cloudness_icon"] = cloudness_icon
|
||||
|
||||
@app.get("/weather/{location}")
|
||||
async def get_weather_base(location: str):
|
||||
return RedirectResponse(f"{location}/today")
|
||||
|
||||
@app.get("/weather/{location}/{date}", response_class=HTMLResponse)
|
||||
async def get_weather(request: Request, location: str, date: str):
|
||||
if date == "mock":
|
||||
values = MOCK_DATA.values
|
||||
else:
|
||||
api = GismeteoApi()
|
||||
values = await api.get_day(location, dateutil.parse(date))
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="weather.html",
|
||||
context={
|
||||
"datetime": datetime,
|
||||
"location": location,
|
||||
"date": dateutil.parse(date),
|
||||
"values": values,
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user