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,100 +11,96 @@ from .sequence_helpers import Random, set_len, shuffle
|
|||
from .webdav import WebDAV
|
||||
|
||||
|
||||
class AllTime:
|
||||
@staticmethod
|
||||
async def shuffle_solution(
|
||||
cfg: Config = Depends(Config.get_config),
|
||||
) -> str:
|
||||
"""
|
||||
Lösung: Reihenfolge zufällig bestimmen
|
||||
"""
|
||||
async def shuffle_solution(
|
||||
cfg: Config = Depends(Config.get_config),
|
||||
) -> str:
|
||||
"""
|
||||
Lösung: Reihenfolge zufällig bestimmen
|
||||
"""
|
||||
|
||||
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)
|
||||
return "".join(await shuffle(cfg.puzzle.solution))
|
||||
|
||||
|
||||
class Today:
|
||||
@staticmethod
|
||||
async def get_part(
|
||||
day: int,
|
||||
shuffled_solution: str = Depends(AllTime.shuffle_solution),
|
||||
) -> str:
|
||||
"""
|
||||
Heute angezeigter Teil der Lösung
|
||||
"""
|
||||
async def shuffle_images_auto(
|
||||
images: list[str] = Depends(list_images_auto),
|
||||
) -> list[str]:
|
||||
"""
|
||||
Bilder: Reihenfolge zufällig bestimmen
|
||||
"""
|
||||
|
||||
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
|
||||
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
|
||||
"""
|
||||
return shuffled_solution[day]
|
||||
|
||||
# Datei existiert garantiert!
|
||||
img = await load_image(images[day])
|
||||
image = await AdventImage.from_img(img)
|
||||
|
||||
font = ImageFont.truetype(
|
||||
font=BytesIO(await WebDAV.read_bytes(f"files/{cfg.server.font}")),
|
||||
size=50,
|
||||
async def get_random(
|
||||
day: int,
|
||||
) -> 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
|
||||
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
|
||||
|
||||
return image.img
|
||||
|
||||
@staticmethod
|
||||
async def get_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:
|
||||
"""
|
||||
Bild für einen Tag abrufen
|
||||
"""
|
||||
async def get_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:
|
||||
"""
|
||||
Bild für einen Tag abrufen
|
||||
"""
|
||||
|
||||
try:
|
||||
# Versuche, aus "manual"-Ordner zu laden
|
||||
return await load_image(f"images_manual/{day}.jpg")
|
||||
try:
|
||||
# Versuche, aus "manual"-Ordner zu laden
|
||||
return await load_image(f"images_manual/{day}.jpg")
|
||||
|
||||
except RuntimeError:
|
||||
# Erstelle automatisch generiertes Bild
|
||||
return await Today.gen_auto_image(
|
||||
day=day, images=images, cfg=cfg, rnd=rnd, part=part
|
||||
)
|
||||
except RuntimeError:
|
||||
# Erstelle automatisch generiertes Bild
|
||||
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