mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 00:03:07 +00:00
remove "namespace classes" AllTime and Today
This commit is contained in:
parent
af00dafb6c
commit
29f02d2545
2 changed files with 86 additions and 90 deletions
|
@ -11,21 +11,19 @@ from .sequence_helpers import Random, set_len, shuffle
|
|||
from .webdav import WebDAV
|
||||
|
||||
|
||||
class AllTime:
|
||||
@staticmethod
|
||||
async def shuffle_solution(
|
||||
async def shuffle_solution(
|
||||
cfg: Config = Depends(Config.get_config),
|
||||
) -> str:
|
||||
) -> str:
|
||||
"""
|
||||
Lösung: Reihenfolge zufällig bestimmen
|
||||
"""
|
||||
|
||||
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),
|
||||
) -> list[str]:
|
||||
) -> list[str]:
|
||||
"""
|
||||
Bilder: Reihenfolge zufällig bestimmen
|
||||
"""
|
||||
|
@ -34,42 +32,40 @@ class AllTime:
|
|||
return await shuffle(ls)
|
||||
|
||||
|
||||
class Today:
|
||||
@staticmethod
|
||||
async def get_part(
|
||||
async def get_part(
|
||||
day: int,
|
||||
shuffled_solution: str = Depends(AllTime.shuffle_solution),
|
||||
) -> str:
|
||||
shuffled_solution: str = Depends(shuffle_solution),
|
||||
) -> str:
|
||||
"""
|
||||
Heute angezeigter Teil der Lösung
|
||||
"""
|
||||
|
||||
return shuffled_solution[day]
|
||||
|
||||
@staticmethod
|
||||
async def get_random(
|
||||
|
||||
async def get_random(
|
||||
day: int,
|
||||
) -> Random:
|
||||
) -> Random:
|
||||
"""
|
||||
Tagesabhängige Zufallszahlen
|
||||
"""
|
||||
|
||||
return await Random.get(day)
|
||||
|
||||
@staticmethod
|
||||
async def gen_auto_image(
|
||||
|
||||
async def gen_auto_image(
|
||||
day: int,
|
||||
images: list[str] = Depends(AllTime.shuffle_images_auto),
|
||||
auto_images: list[str] = Depends(shuffle_images_auto),
|
||||
cfg: Config = Depends(Config.get_config),
|
||||
rnd: Random = Depends(get_random),
|
||||
part: str = Depends(get_part),
|
||||
) -> Image.Image:
|
||||
) -> Image.Image:
|
||||
"""
|
||||
Automatisch generiertes Bild erstellen
|
||||
"""
|
||||
|
||||
# Datei existiert garantiert!
|
||||
img = await load_image(images[day])
|
||||
img = await load_image(auto_images[day])
|
||||
image = await AdventImage.from_img(img)
|
||||
|
||||
font = ImageFont.truetype(
|
||||
|
@ -87,14 +83,14 @@ class Today:
|
|||
|
||||
return image.img
|
||||
|
||||
@staticmethod
|
||||
async def get_image(
|
||||
|
||||
async def get_image(
|
||||
day: int,
|
||||
images: list[str] = Depends(AllTime.shuffle_images_auto),
|
||||
auto_images: list[str] = Depends(shuffle_images_auto),
|
||||
cfg: Config = Depends(Config.get_config),
|
||||
rnd: Random = Depends(get_random),
|
||||
part: str = Depends(get_part),
|
||||
) -> Image.Image:
|
||||
) -> Image.Image:
|
||||
"""
|
||||
Bild für einen Tag abrufen
|
||||
"""
|
||||
|
@ -105,6 +101,6 @@ class Today:
|
|||
|
||||
except RuntimeError:
|
||||
# Erstelle automatisch generiertes Bild
|
||||
return await Today.gen_auto_image(
|
||||
day=day, images=images, cfg=cfg, rnd=rnd, part=part
|
||||
return await gen_auto_image(
|
||||
day=day, auto_images=auto_images, cfg=cfg, rnd=rnd, part=part
|
||||
)
|
||||
|
|
|
@ -5,7 +5,7 @@ from fastapi.responses import StreamingResponse
|
|||
from PIL import Image
|
||||
|
||||
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 .user import user_is_admin
|
||||
|
||||
|
@ -17,15 +17,10 @@ async def startup() -> None:
|
|||
cfg = await Config.get_config()
|
||||
print(cfg.puzzle.solution)
|
||||
|
||||
shuffled_solution = await AllTime.shuffle_solution(cfg)
|
||||
shuffled_solution = await shuffle_solution(cfg)
|
||||
print(shuffled_solution)
|
||||
|
||||
|
||||
@router.get("/part/{day}")
|
||||
async def get_part(part: str = Depends(Today.get_part)) -> str:
|
||||
return part
|
||||
|
||||
|
||||
@router.get("/date")
|
||||
async def get_date() -> str:
|
||||
return date.today().isoformat()
|
||||
|
@ -50,12 +45,17 @@ async def user_can_view(
|
|||
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(
|
||||
"/image/{day}",
|
||||
response_class=StreamingResponse,
|
||||
)
|
||||
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),
|
||||
is_admin: bool = Depends(user_is_admin),
|
||||
) -> StreamingResponse:
|
||||
|
|
Loading…
Reference in a new issue