Files
TallerCiCd/backend/app/services/prices.py
2025-12-16 09:32:14 +01:00

51 lines
1.5 KiB
Python

# CI/CD Workshop
# Copyright (C) 2025 OpenBokeron
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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.",
}