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,100 +11,96 @@ from .sequence_helpers import Random, set_len, shuffle
from .webdav import WebDAV from .webdav import WebDAV
class AllTime: async def shuffle_solution(
@staticmethod cfg: Config = Depends(Config.get_config),
async def shuffle_solution( ) -> str:
cfg: Config = Depends(Config.get_config), """
) -> 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(
images: list[str] = Depends(list_images_auto),
) -> list[str]:
"""
Bilder: Reihenfolge zufällig bestimmen
"""
ls = set_len(images, 24)
return await shuffle(ls)
class Today: async def shuffle_images_auto(
@staticmethod images: list[str] = Depends(list_images_auto),
async def get_part( ) -> list[str]:
day: int, """
shuffled_solution: str = Depends(AllTime.shuffle_solution), Bilder: Reihenfolge zufällig bestimmen
) -> str: """
"""
Heute angezeigter Teil der Lösung
"""
return shuffled_solution[day] ls = set_len(images, 24)
return await shuffle(ls)
@staticmethod
async def get_random(
day: int,
) -> Random:
"""
Tagesabhängige Zufallszahlen
"""
return await Random.get(day) async def get_part(
day: int,
shuffled_solution: str = Depends(shuffle_solution),
) -> str:
"""
Heute angezeigter Teil der Lösung
"""
@staticmethod return shuffled_solution[day]
async def gen_auto_image(
day: int,
images: list[str] = Depends(AllTime.shuffle_images_auto),
cfg: Config = Depends(Config.get_config),
rnd: Random = Depends(get_random),
part: str = Depends(get_part),
) -> Image.Image:
"""
Automatisch generiertes Bild erstellen
"""
# Datei existiert garantiert!
img = await load_image(images[day])
image = await AdventImage.from_img(img)
font = ImageFont.truetype( async def get_random(
font=BytesIO(await WebDAV.read_bytes(f"files/{cfg.server.font}")), day: int,
size=50, ) -> Random:
"""
Tagesabhängige Zufallszahlen
"""
return await Random.get(day)
async def gen_auto_image(
day: int,
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:
"""
Automatisch generiertes Bild erstellen
"""
# Datei existiert garantiert!
img = await load_image(auto_images[day])
image = await AdventImage.from_img(img)
font = ImageFont.truetype(
font=BytesIO(await WebDAV.read_bytes(f"files/{cfg.server.font}")),
size=50,
)
# Buchstaben verstecken
for letter in part:
await image.hide_text(
xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))),
text=letter,
font=font,
) )
# Buchstaben verstecken return image.img
for letter in part:
await image.hide_text(
xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))),
text=letter,
font=font,
)
return image.img
@staticmethod async def get_image(
async def get_image( day: int,
day: int, auto_images: list[str] = Depends(shuffle_images_auto),
images: list[str] = Depends(AllTime.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 """
"""
try: try:
# Versuche, aus "manual"-Ordner zu laden # Versuche, aus "manual"-Ordner zu laden
return await load_image(f"images_manual/{day}.jpg") return await load_image(f"images_manual/{day}.jpg")
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: