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 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( async def get_days(
cal_cfg: CalendarConfig = Depends(get_calendar_config), cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> list[int]: ) -> list[int]:
@ -32,10 +22,10 @@ async def get_days(
return list(set(door.day for door in cal_cfg.doors)) 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), cfg: Config = Depends(get_config),
days: list[int] = Depends(get_days), days: list[int] = Depends(get_days),
) -> dict[int, set[str]]: ) -> dict[int, str]:
""" """
Lösung auf vorhandene Tage aufteilen Lösung auf vorhandene Tage aufteilen
""" """
@ -49,10 +39,10 @@ async def get_solution_parts(
*rnd.sample(days, solution_length % num_days), *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): for day, letter in zip(solution_days, cfg.puzzle.solution):
result[day] = result.get(day, set()) result[day] = result.get(day, "")
result[day].add(letter) result[day] += letter
return result return result
@ -68,18 +58,18 @@ async def shuffle_images_auto(
return await shuffle(ls) return await shuffle(ls)
async def get_part( async def get_part_for_day(
day: int, day: int,
shuffled_solution: str = Depends(shuffle_solution), parts: dict[int, str] = Depends(get_day_parts),
) -> str: ) -> str:
""" """
Heute angezeigter Teil der Lösung 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, day: int,
) -> Random: ) -> Random:
""" """
@ -89,12 +79,12 @@ async def get_random(
return await Random.get(day) return await Random.get(day)
async def gen_auto_image( async def gen_auto_image_for_day(
day: int, day: int,
auto_images: list[str] = Depends(shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random), rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part), part: str = Depends(get_part_for_day),
) -> Image.Image: ) -> Image.Image:
""" """
Automatisch generiertes Bild erstellen Automatisch generiertes Bild erstellen
@ -120,12 +110,12 @@ async def gen_auto_image(
return image.img return image.img
async def get_image( async def get_image_for_day(
day: int, day: int,
auto_images: list[str] = Depends(shuffle_images_auto), auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random), rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part), part: str = Depends(get_part_for_day),
) -> Image.Image: ) -> Image.Image:
""" """
Bild für einen Tag abrufen Bild für einen Tag abrufen
@ -141,6 +131,6 @@ async def get_image(
except RuntimeError: except RuntimeError:
# Erstelle automatisch generiertes Bild # 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 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 fastapi import APIRouter, Depends
from pydantic import BaseModel from pydantic import BaseModel
from ..core import depends
from ..core.calendar_config import CalendarConfig, get_calendar_config from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import Config, get_config from ..core.config import Config, get_config
from ..core.depends import get_solution_parts, shuffle_solution
from ..core.settings import SETTINGS from ..core.settings import SETTINGS
from ._security import require_admin, user_is_admin from ._security import require_admin, user_is_admin
router = APIRouter(prefix="/admin", tags=["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") @router.get("/is_admin")
async def is_admin( async def is_admin(
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),
@ -28,8 +21,12 @@ async def is_admin(
class ConfigModel(BaseModel): class ConfigModel(BaseModel):
class __Puzzle(BaseModel): class __Puzzle(BaseModel):
class __Part(BaseModel):
day: int
part: str
solution: str solution: str
shuffled: str day_parts: list[__Part]
begin: date begin: date
end: date end: date
closing: date closing: date
@ -64,13 +61,16 @@ async def get_config_model(
_: None = Depends(require_admin), _: None = Depends(require_admin),
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(get_calendar_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: ) -> ConfigModel:
return ConfigModel.model_validate( return ConfigModel.model_validate(
{ {
"puzzle": { "puzzle": {
"solution": cfg.puzzle.solution, "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 "begin": date.today(), # TODO
"end": date.today(), # TODO "end": date.today(), # TODO
"closing": 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 fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ..core import depends
from ..core.config import get_config 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 ..core.image_helpers import api_return_image
from ._security import user_can_view_door, user_is_admin, user_visible_doors 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() cfg = await get_config()
print(cfg.puzzle.solution) print(cfg.puzzle.solution)
shuffled_solution = await shuffle_solution(cfg)
print(shuffled_solution)
@router.get("/date") @router.get("/date")
async def get_date() -> str: async def get_date() -> str:
@ -43,7 +40,7 @@ async def get_visible_days(
@router.get("/part/{day}") @router.get("/part/{day}")
async def get_part_for_day( async def get_part_for_day(
part: str = Depends(get_part), part: str = Depends(depends.get_part_for_day),
) -> str: ) -> str:
""" """
Heutiger Lösungsteil Heutiger Lösungsteil
@ -57,7 +54,7 @@ async def get_part_for_day(
response_class=StreamingResponse, response_class=StreamingResponse,
) )
async def get_image_for_day( 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), can_view: bool = Depends(user_can_view_door),
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse: ) -> StreamingResponse:

View file

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