implementation using get_part_for_day

This commit is contained in:
Jörn-Michael Miehe 2023-09-12 07:27:59 +00:00
parent a84323afc8
commit 63d88c3a09
5 changed files with 48 additions and 48 deletions

View file

View file

@ -12,16 +12,6 @@ from .sequence_helpers import Random, set_len, shuffle
from .webdav import WebDAV
async def shuffle_solution(
cfg: Config = Depends(get_config),
) -> str:
"""
Lösung: Reihenfolge zufällig bestimmen
"""
return "".join(await shuffle(cfg.puzzle.solution))
async def get_days(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> list[int]:
@ -32,10 +22,10 @@ async def get_days(
return list(set(door.day for door in cal_cfg.doors))
async def get_solution_parts(
async def get_day_parts(
cfg: Config = Depends(get_config),
days: list[int] = Depends(get_days),
) -> dict[int, set[str]]:
) -> dict[int, str]:
"""
Lösung auf vorhandene Tage aufteilen
"""
@ -49,10 +39,10 @@ async def get_solution_parts(
*rnd.sample(days, solution_length % num_days),
]
result: dict[int, set[str]] = {}
result: dict[int, str] = {}
for day, letter in zip(solution_days, cfg.puzzle.solution):
result[day] = result.get(day, set())
result[day].add(letter)
result[day] = result.get(day, "")
result[day] += letter
return result
@ -68,18 +58,18 @@ async def shuffle_images_auto(
return await shuffle(ls)
async def get_part(
async def get_part_for_day(
day: int,
shuffled_solution: str = Depends(shuffle_solution),
parts: dict[int, str] = Depends(get_day_parts),
) -> str:
"""
Heute angezeigter Teil der Lösung
"""
return shuffled_solution[day]
return parts[day]
async def get_random(
async def get_random_for_day(
day: int,
) -> Random:
"""
@ -89,12 +79,12 @@ async def get_random(
return await Random.get(day)
async def gen_auto_image(
async def gen_auto_image_for_day(
day: int,
auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random),
part: str = Depends(get_part),
rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part_for_day),
) -> Image.Image:
"""
Automatisch generiertes Bild erstellen
@ -120,12 +110,12 @@ async def gen_auto_image(
return image.img
async def get_image(
async def get_image_for_day(
day: int,
auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random),
part: str = Depends(get_part),
rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part_for_day),
) -> Image.Image:
"""
Bild für einen Tag abrufen
@ -141,6 +131,6 @@ async def get_image(
except RuntimeError:
# Erstelle automatisch generiertes Bild
return await gen_auto_image(
return await gen_auto_image_for_day(
day=day, auto_images=auto_images, cfg=cfg, rnd=rnd, part=part
)

View file

@ -3,22 +3,15 @@ from datetime import date
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from ..core import depends
from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import Config, get_config
from ..core.depends import get_solution_parts, shuffle_solution
from ..core.settings import SETTINGS
from ._security import require_admin, user_is_admin
router = APIRouter(prefix="/admin", tags=["admin"])
@router.get("/parts")
async def get_parts(
shuffle=Depends(get_solution_parts),
) -> dict[int, set[str]]:
return shuffle
@router.get("/is_admin")
async def is_admin(
is_admin: bool = Depends(user_is_admin),
@ -28,8 +21,12 @@ async def is_admin(
class ConfigModel(BaseModel):
class __Puzzle(BaseModel):
class __Part(BaseModel):
day: int
part: str
solution: str
shuffled: str
day_parts: list[__Part]
begin: date
end: date
closing: date
@ -64,13 +61,16 @@ async def get_config_model(
_: None = Depends(require_admin),
cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(get_calendar_config),
shuffled_solution: str = Depends(shuffle_solution),
day_parts: dict[int, str] = Depends(depends.get_day_parts),
) -> ConfigModel:
return ConfigModel.model_validate(
{
"puzzle": {
"solution": cfg.puzzle.solution,
"shuffled": shuffled_solution,
"day_parts": [
{"day": day, "part": part}
for day, part in sorted(day_parts.items())
],
"begin": date.today(), # TODO
"end": date.today(), # TODO
"closing": date.today(), # TODO

View file

@ -4,8 +4,8 @@ from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse
from PIL import Image
from ..core import depends
from ..core.config import get_config
from ..core.depends import get_image, get_part, shuffle_solution
from ..core.image_helpers import api_return_image
from ._security import user_can_view_door, user_is_admin, user_visible_doors
@ -17,9 +17,6 @@ async def startup() -> None:
cfg = await get_config()
print(cfg.puzzle.solution)
shuffled_solution = await shuffle_solution(cfg)
print(shuffled_solution)
@router.get("/date")
async def get_date() -> str:
@ -43,7 +40,7 @@ async def get_visible_days(
@router.get("/part/{day}")
async def get_part_for_day(
part: str = Depends(get_part),
part: str = Depends(depends.get_part_for_day),
) -> str:
"""
Heutiger Lösungsteil
@ -57,7 +54,7 @@ async def get_part_for_day(
response_class=StreamingResponse,
)
async def get_image_for_day(
image: Image.Image = Depends(get_image),
image: Image.Image = Depends(depends.get_image_for_day),
can_view: bool = Depends(user_can_view_door),
is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse:

View file

@ -13,7 +13,18 @@
<dd>{{ admin_config_model.puzzle.solution }}</dd>
<dt>Reihenfolge</dt>
<dd>{{ admin_config_model.puzzle.shuffled }}</dd>
<dd>
<template
v-for="(day_part, idx) in admin_config_model.puzzle.day_parts"
:key="`part-${idx}`"
>
<span>
<template v-if="idx > 0"> &ndash; </template>
{{ day_part.day }}:
</span>
<span class="is-family-monospace">{{ day_part.part }}</span>
</template>
</dd>
<dt>Offene Türchen</dt>
<dd>10</dd>
@ -50,8 +61,10 @@
<dt>Türchen</dt>
<dd>
<!-- <span>{{ admin_config_model.calendar.doors.join(", ") }}</span> -->
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
<span class="tag is-danger ml-2">
<span class="mr-2">
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
</span>
<span class="tag is-danger">
<span class="icon">
<font-awesome-icon icon="fa-solid fa-bolt" />
</span>
@ -125,7 +138,7 @@ import BulmaDrawer from "./bulma/Drawer.vue";
interface ConfigModel {
puzzle: {
solution: string;
shuffled: string;
day_parts: { day: number; part: string }[];
begin: string;
end: string;
closing: string;
@ -156,7 +169,7 @@ export default class extends Vue {
public admin_config_model: ConfigModel = {
puzzle: {
solution: "ABCDEFGHIJKLMNOPQRSTUVWX",
shuffled: "AGFCINBEWLKQMXDURPOSJVHT",
day_parts: [],
begin: "01.12.2023",
end: "24.12.2023",
closing: "01.04.2024",