Init
This commit is contained in:
73
backend/app/services/menu.py
Normal file
73
backend/app/services/menu.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import json
|
||||
import random
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
|
||||
ITEMS_PER_SECTION = 3
|
||||
|
||||
|
||||
def _load_json(filename: str) -> Dict:
|
||||
path = DATA_DIR / filename
|
||||
with open(path, encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
MENU_SOURCE = _load_json("menu_items.json")
|
||||
|
||||
|
||||
def _pick_items(options: List[str], count: int) -> List[str]:
|
||||
if count >= len(options):
|
||||
return list(options)
|
||||
return random.sample(options, count)
|
||||
|
||||
|
||||
def _pick_mains(count: int = ITEMS_PER_SECTION) -> List[str]:
|
||||
fish_options = MENU_SOURCE["mains"]["fish"]
|
||||
other_options = MENU_SOURCE["mains"]["others"]
|
||||
|
||||
fish_choice = random.choice(fish_options)
|
||||
remaining_needed = max(count - 1, 0)
|
||||
|
||||
pool = [item for item in fish_options if item != fish_choice] + other_options
|
||||
if remaining_needed > len(pool):
|
||||
remaining_needed = len(pool)
|
||||
|
||||
mains = [fish_choice] + _pick_items(pool, remaining_needed)
|
||||
random.shuffle(mains)
|
||||
return mains
|
||||
|
||||
def _pick_garnish() -> List[str]:
|
||||
garnish_options = MENU_SOURCE["mains"]["garnish"]
|
||||
|
||||
return _pick_items(garnish_options, 2)
|
||||
|
||||
|
||||
def _build_alternative() -> Dict:
|
||||
alternative = MENU_SOURCE.get("alternatives", {})
|
||||
return {
|
||||
"title": alternative.get("title", "Alternativa"),
|
||||
"items": alternative.get("items", []),
|
||||
"price": alternative.get("price"),
|
||||
"note": alternative.get("note", ""),
|
||||
}
|
||||
|
||||
def build_menu(items_per_section: int = ITEMS_PER_SECTION) -> Dict:
|
||||
today = datetime.now()
|
||||
|
||||
return {
|
||||
"day": today.strftime("%A").capitalize(),
|
||||
"starters": _pick_items(MENU_SOURCE["starters"], items_per_section),
|
||||
"mains": _pick_mains(items_per_section),
|
||||
"garnish": _pick_garnish(),
|
||||
"desserts": _pick_items(MENU_SOURCE["desserts"], items_per_section),
|
||||
"notes": _pick_items(MENU_SOURCE["notes"], 3),
|
||||
"menu_price": MENU_SOURCE["university_deal"]["current_price"],
|
||||
"university_deal": MENU_SOURCE["university_deal"],
|
||||
"espetos_tip": random.choice(MENU_SOURCE["espetos_tips"]),
|
||||
"alternative": _build_alternative(),
|
||||
"availability": {
|
||||
"last_updated": today.isoformat(timespec="seconds"),
|
||||
},
|
||||
}
|
||||
34
backend/app/services/prices.py
Normal file
34
backend/app/services/prices.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
import random
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
|
||||
|
||||
|
||||
def _load_json(filename: str) -> Dict:
|
||||
path = DATA_DIR / filename
|
||||
with open(path, encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
PRICE_RANGES = _load_json("price_ranges.json")["items"]
|
||||
|
||||
|
||||
def random_price(item: str) -> Dict:
|
||||
low, high = PRICE_RANGES.get(item, (1.0, 3.0))
|
||||
price = min(round(random.uniform(low, high), 2), 3.0)
|
||||
return {
|
||||
"item": item,
|
||||
"price": price,
|
||||
"currency": "EUR",
|
||||
"generated_at": datetime.now().isoformat(timespec="seconds"),
|
||||
}
|
||||
|
||||
|
||||
def prices_payload() -> Dict:
|
||||
return {
|
||||
"items": [random_price(item) for item in PRICE_RANGES.keys()],
|
||||
"disclaimer": "Depende de como pilles al de cafete.",
|
||||
}
|
||||
Reference in New Issue
Block a user