diff --git a/gallery/easel/__init__.py b/gallery/easel/__init__.py index 5348817..79bc2e3 100644 --- a/gallery/easel/__init__.py +++ b/gallery/easel/__init__.py @@ -1,23 +1,31 @@ -import locale +import locale as _locale from fastapi import FastAPI +from gallery.sketch.schedule.api import ScheduleApi from gallery.sketch.weather.api import WeatherApi from .route import doc +from .route.api import schedule as schedule_api_route from .route.api import weather as weather_api_route +from .route.view import schedule as schedule_view_route from .route.view import weather as weather_view_route -def build_app(weather_api: WeatherApi) -> FastAPI: - locale.setlocale(locale.LC_TIME, "ru_RU.UTF-8") +def build_app( + weather_api: WeatherApi, schedule_api: ScheduleApi, *, locale: str = "ru_RU.UTF-8" +) -> FastAPI: + _locale.setlocale(_locale.LC_TIME, locale) app = FastAPI( title="Gallery", docs_url=None, redoc_url=None, ) app.state.weather_api = weather_api + app.state.schedule_api = schedule_api doc.mount(app) weather_api_route.mount(app) + schedule_api_route.mount(app) weather_view_route.mount(app) + schedule_view_route.mount(app) return app diff --git a/gallery/easel/route/api/schedule.py b/gallery/easel/route/api/schedule.py new file mode 100644 index 0000000..e515adf --- /dev/null +++ b/gallery/easel/route/api/schedule.py @@ -0,0 +1,5 @@ +from fastapi import FastAPI + + +def mount(app: FastAPI): + pass diff --git a/gallery/easel/route/view/schedule/__init__.py b/gallery/easel/route/view/schedule/__init__.py new file mode 100644 index 0000000..9d23d11 --- /dev/null +++ b/gallery/easel/route/view/schedule/__init__.py @@ -0,0 +1,52 @@ +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 gallery.easel.route.view.weather.util import TagType, TagUtil +from gallery.sketch.schedule.api import ScheduleApi + + +def mount(app: FastAPI): + base_dir = Path(__file__).parent + app.mount( + "/schedule/static", StaticFiles(directory=base_dir / "static"), name="static" + ) + templates = Jinja2Templates(directory=base_dir / "templates") + + @app.get("/schedule", response_class=HTMLResponse) + async def get_schedule_list(request: Request): + schedule_api: ScheduleApi = request.app.state.schedule_api + channels = await schedule_api.get_channels() + return templates.TemplateResponse( + request=request, + name="index.html", + context={ + "channels": channels, + }, + ) + + @app.get("/schedule/{channel}", response_class=RedirectResponse) + async def get_schedule_default(channel: str): + return RedirectResponse(f"{channel}/tag/today") + + @app.get("/schedule/{channel}/tag/{tag}", response_class=HTMLResponse) + async def get_schedule_tag(request: Request, channel: str, tag: str): + tag_value = TagUtil.parse_tag(tag) + schedule_api: ScheduleApi = request.app.state.schedule_api + if tag_value.type == TagType.DAY: + response = await schedule_api.get_channel_schedule(channel, tag_value.date) + else: + raise ValueError(tag) + return templates.TemplateResponse( + request=request, + name="schedule.html", + context={ + "tag_util": TagUtil, + "datetime": datetime, + "response": response, + }, + ) diff --git a/gallery/easel/route/view/schedule/static/favicon.ico b/gallery/easel/route/view/schedule/static/favicon.ico new file mode 100644 index 0000000..a283ecb Binary files /dev/null and b/gallery/easel/route/view/schedule/static/favicon.ico differ diff --git a/gallery/easel/route/view/weather/static/index.js b/gallery/easel/route/view/schedule/static/index.js similarity index 100% rename from gallery/easel/route/view/weather/static/index.js rename to gallery/easel/route/view/schedule/static/index.js diff --git a/gallery/easel/route/view/schedule/static/style.css b/gallery/easel/route/view/schedule/static/style.css new file mode 100644 index 0000000..e435974 --- /dev/null +++ b/gallery/easel/route/view/schedule/static/style.css @@ -0,0 +1,21 @@ +body { + font-size: 1.5rem; +} + +.app-container { + display: flex; + flex-direction: column; + flex-wrap: nowrap; + align-items: center; +} + +table, +th, +td { + /* border: 1px solid rgba(0, 0, 0, 0.2); */ + text-align: left; +} + +td { + padding: 0.1rem 0.4rem; +} diff --git a/gallery/easel/route/view/schedule/templates/index.html b/gallery/easel/route/view/schedule/templates/index.html new file mode 100644 index 0000000..e6acc56 --- /dev/null +++ b/gallery/easel/route/view/schedule/templates/index.html @@ -0,0 +1,26 @@ + + + +
+ + + +| TIME | +NAME | +
| {{value.start.strftime('%H:%M')}} | +{{value.label}} | +