remove (Calendar)Config's static methods for good measure

This commit is contained in:
Jörn-Michael Miehe 2023-09-08 19:53:35 +00:00
parent 21279f747c
commit f9f9989414
7 changed files with 37 additions and 37 deletions

View file

@ -5,7 +5,7 @@ import tomli_w
from fastapi import Depends from fastapi import Depends
from pydantic import BaseModel from pydantic import BaseModel
from .config import Config from .config import Config, get_config
from .webdav import WebDAV from .webdav import WebDAV
@ -30,18 +30,7 @@ class CalendarConfig(BaseModel):
# Türen für die UI # Türen für die UI
doors: DoorsSaved = [] doors: DoorsSaved = []
@staticmethod async def change(self, cfg: Config) -> None:
async def get_calendar_config(
cfg: Config = Depends(Config.get_config),
) -> "CalendarConfig":
"""
Kalender Konfiguration lesen
"""
txt = await WebDAV.read_str(path=f"files/{cfg.puzzle.calendar}")
return CalendarConfig.model_validate(tomllib.loads(txt))
async def set_calendar_config(self, cfg: Config) -> None:
""" """
Kalender Konfiguration ändern Kalender Konfiguration ändern
""" """
@ -55,3 +44,14 @@ class CalendarConfig(BaseModel):
) )
), ),
) )
async def get_calendar_config(
cfg: Config = Depends(get_config),
) -> CalendarConfig:
"""
Kalender Konfiguration lesen
"""
txt = await WebDAV.read_str(path=f"files/{cfg.puzzle.calendar}")
return CalendarConfig.model_validate(tomllib.loads(txt))

View file

@ -44,7 +44,7 @@ class Config(BaseModel):
server: Server server: Server
puzzle: Puzzle puzzle: Puzzle
@staticmethod
async def get_config() -> "Config": async def get_config() -> "Config":
""" """
Globale Konfiguration lesen Globale Konfiguration lesen

View file

@ -5,14 +5,14 @@ from fastapi import Depends
from PIL import Image, ImageFont from PIL import Image, ImageFont
from .advent_image import _XY, AdventImage from .advent_image import _XY, AdventImage
from .config import Config from .config import Config, get_config
from .image_helpers import list_images_auto, load_image from .image_helpers import list_images_auto, load_image
from .sequence_helpers import Random, set_len, shuffle from .sequence_helpers import Random, set_len, shuffle
from .webdav import WebDAV from .webdav import WebDAV
async def shuffle_solution( async def shuffle_solution(
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(get_config),
) -> str: ) -> str:
""" """
Lösung: Reihenfolge zufällig bestimmen Lösung: Reihenfolge zufällig bestimmen
@ -56,7 +56,7 @@ async def get_random(
async def gen_auto_image( async def gen_auto_image(
day: int, day: int,
auto_images: list[str] = Depends(shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random), rnd: Random = Depends(get_random),
part: str = Depends(get_part), part: str = Depends(get_part),
) -> Image.Image: ) -> Image.Image:
@ -87,7 +87,7 @@ async def gen_auto_image(
async def get_image( async def get_image(
day: int, day: int,
auto_images: list[str] = Depends(shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random), rnd: Random = Depends(get_random),
part: str = Depends(get_part), part: str = Depends(get_part),
) -> Image.Image: ) -> Image.Image:

View file

@ -2,13 +2,13 @@ import itertools
import random import random
from typing import Any, Self, Sequence from typing import Any, Self, Sequence
from .config import Config from .config import get_config
class Random(random.Random): class Random(random.Random):
@classmethod @classmethod
async def get(cls, bonus_salt: Any = "") -> Self: async def get(cls, bonus_salt: Any = "") -> Self:
cfg = await Config.get_config() cfg = await get_config()
return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_pepper}{bonus_salt}") return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_pepper}{bonus_salt}")

View file

@ -4,14 +4,14 @@ from datetime import date
from fastapi import Depends, HTTPException, status from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.security import HTTPBasic, HTTPBasicCredentials
from ..core.config import Config from ..core.config import Config, get_config
security = HTTPBasic() security = HTTPBasic()
async def user_is_admin( async def user_is_admin(
credentials: HTTPBasicCredentials = Depends(security), credentials: HTTPBasicCredentials = Depends(security),
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(get_config),
) -> bool: ) -> bool:
""" """
True iff der user "admin" ist True iff der user "admin" ist

View file

@ -4,7 +4,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ..core.config import Config from ..core.config import get_config
from ..core.depends import get_image, get_part, shuffle_solution from ..core.depends import get_image, get_part, shuffle_solution
from ..core.image_helpers import api_return_image from ..core.image_helpers import api_return_image
from ._security import user_can_view_door, user_is_admin, user_visible_doors from ._security import user_can_view_door, user_is_admin, user_visible_doors
@ -14,7 +14,7 @@ router = APIRouter(prefix="/days", tags=["days"])
@router.on_event("startup") @router.on_event("startup")
async def startup() -> None: async def startup() -> None:
cfg = await Config.get_config() cfg = await get_config()
print(cfg.puzzle.solution) print(cfg.puzzle.solution)
shuffled_solution = await shuffle_solution(cfg) shuffled_solution = await shuffle_solution(cfg)

View file

@ -1,8 +1,8 @@
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from ..core.calendar_config import CalendarConfig, DoorsSaved from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config
from ..core.config import Config from ..core.config import Config, get_config
from ..core.image_helpers import api_return_image, load_image from ..core.image_helpers import api_return_image, load_image
router = APIRouter(prefix="/general", tags=["general"]) router = APIRouter(prefix="/general", tags=["general"])
@ -13,7 +13,7 @@ router = APIRouter(prefix="/general", tags=["general"])
response_class=StreamingResponse, response_class=StreamingResponse,
) )
async def get_image_for_day( async def get_image_for_day(
cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> StreamingResponse: ) -> StreamingResponse:
""" """
Hintergrundbild laden Hintergrundbild laden
@ -24,7 +24,7 @@ async def get_image_for_day(
@router.get("/doors") @router.get("/doors")
async def get_doors( async def get_doors(
cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> DoorsSaved: ) -> DoorsSaved:
""" """
Türchen lesen Türchen lesen
@ -36,8 +36,8 @@ async def get_doors(
@router.put("/doors") @router.put("/doors")
async def put_doors( async def put_doors(
doors: DoorsSaved, doors: DoorsSaved,
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> None: ) -> None:
""" """
Türchen setzen Türchen setzen
@ -47,4 +47,4 @@ async def put_doors(
doors, doors,
key=lambda door: door.day, key=lambda door: door.day,
) )
await cal_cfg.set_calendar_config(cfg) await cal_cfg.change(cfg)