remove "namespace classes" AllTime and Today

This commit is contained in:
Jörn-Michael Miehe 2023-09-08 19:19:08 +00:00
parent af00dafb6c
commit 29f02d2545
2 changed files with 86 additions and 90 deletions

View file

@ -11,21 +11,19 @@ from .sequence_helpers import Random, set_len, shuffle
from .webdav import WebDAV from .webdav import WebDAV
class AllTime: async def shuffle_solution(
@staticmethod
async def shuffle_solution(
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(Config.get_config),
) -> str: ) -> str:
""" """
Lösung: Reihenfolge zufällig bestimmen Lösung: Reihenfolge zufällig bestimmen
""" """
return "".join(await shuffle(cfg.puzzle.solution)) return "".join(await shuffle(cfg.puzzle.solution))
@staticmethod
async def shuffle_images_auto( async def shuffle_images_auto(
images: list[str] = Depends(list_images_auto), images: list[str] = Depends(list_images_auto),
) -> list[str]: ) -> list[str]:
""" """
Bilder: Reihenfolge zufällig bestimmen Bilder: Reihenfolge zufällig bestimmen
""" """
@ -34,42 +32,40 @@ class AllTime:
return await shuffle(ls) return await shuffle(ls)
class Today: async def get_part(
@staticmethod
async def get_part(
day: int, day: int,
shuffled_solution: str = Depends(AllTime.shuffle_solution), shuffled_solution: str = Depends(shuffle_solution),
) -> str: ) -> str:
""" """
Heute angezeigter Teil der Lösung Heute angezeigter Teil der Lösung
""" """
return shuffled_solution[day] return shuffled_solution[day]
@staticmethod
async def get_random( async def get_random(
day: int, day: int,
) -> Random: ) -> Random:
""" """
Tagesabhängige Zufallszahlen Tagesabhängige Zufallszahlen
""" """
return await Random.get(day) return await Random.get(day)
@staticmethod
async def gen_auto_image( async def gen_auto_image(
day: int, day: int,
images: list[str] = Depends(AllTime.shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(Config.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:
""" """
Automatisch generiertes Bild erstellen Automatisch generiertes Bild erstellen
""" """
# Datei existiert garantiert! # Datei existiert garantiert!
img = await load_image(images[day]) img = await load_image(auto_images[day])
image = await AdventImage.from_img(img) image = await AdventImage.from_img(img)
font = ImageFont.truetype( font = ImageFont.truetype(
@ -87,14 +83,14 @@ class Today:
return image.img return image.img
@staticmethod
async def get_image( async def get_image(
day: int, day: int,
images: list[str] = Depends(AllTime.shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(Config.get_config), cfg: Config = Depends(Config.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:
""" """
Bild für einen Tag abrufen Bild für einen Tag abrufen
""" """
@ -105,6 +101,6 @@ class Today:
except RuntimeError: except RuntimeError:
# Erstelle automatisch generiertes Bild # Erstelle automatisch generiertes Bild
return await Today.gen_auto_image( return await gen_auto_image(
day=day, images=images, cfg=cfg, rnd=rnd, part=part day=day, auto_images=auto_images, cfg=cfg, rnd=rnd, part=part
) )

View file

@ -5,7 +5,7 @@ from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ..core.config import Config from ..core.config import Config
from ..core.depends import AllTime, Today 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 .user import user_is_admin from .user import user_is_admin
@ -17,15 +17,10 @@ async def startup() -> None:
cfg = await Config.get_config() cfg = await Config.get_config()
print(cfg.puzzle.solution) print(cfg.puzzle.solution)
shuffled_solution = await AllTime.shuffle_solution(cfg) shuffled_solution = await shuffle_solution(cfg)
print(shuffled_solution) print(shuffled_solution)
@router.get("/part/{day}")
async def get_part(part: str = Depends(Today.get_part)) -> str:
return part
@router.get("/date") @router.get("/date")
async def get_date() -> str: async def get_date() -> str:
return date.today().isoformat() return date.today().isoformat()
@ -50,12 +45,17 @@ async def user_can_view(
return day < await get_visible_days() return day < await get_visible_days()
@router.get("/part/{day}")
async def get_part_for_day(part: str = Depends(get_part)) -> str:
return part
@router.get( @router.get(
"/image/{day}", "/image/{day}",
response_class=StreamingResponse, response_class=StreamingResponse,
) )
async def get_image_for_day( async def get_image_for_day(
image: Image.Image = Depends(Today.get_image), image: Image.Image = Depends(get_image),
can_view: bool = Depends(user_can_view), can_view: bool = Depends(user_can_view),
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse: ) -> StreamingResponse: