Spaces:
Running
Running
Download py/data_mode.py from eatbackhackathon2026/eatback: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/spaces/eatbackhackathon2026/eatback/resolve/main/py/data_mode.py
- Command line
-
hf download hf://spaces/eatbackhackathon2026/eatback/py/data_mode.py
-
curl -L -o data_mode.py https://huggingface.co/spaces/eatbackhackathon2026/eatback/resolve/main/py/data_mode.py
3.18 kB
| """Show or change whether this database holds demo data or a school's own records (stop the server first). | |
| python server/data_mode.py -> show the current mode | |
| python server/data_mode.py real --fresh -> start real use: back up, delete every demo round, opinion, trial, | |
| scale check and log (menus, recipes, settings and PINs stay), then | |
| refuse the reset buttons, reset.py and the start-up demo reload | |
| python server/data_mode.py real --keep -> real data, keeping the rounds already recorded (a school that | |
| has been recording for real while still in demo mode) | |
| python server/data_mode.py demo --confirm -> back to demo data (resetting is allowed again) | |
| Real use must not start on top of the demo history: the grade's normal level, the "high three times in a row" rule | |
| and the principal's report would be computed from made-up rounds. So `real` alone refuses while rounds exist. | |
| There is no switch in the web app on purpose: anyone who can reach the server could otherwise flip it. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import backup # noqa: E402 | |
| from db import LOCK, data_mode, data_mode_info, one, run, set_data_mode # noqa: E402 | |
| # Everything recorded while the database ran on demo data; the menu base, recipes and settings are kept. | |
| RECORDS = ("weighings", "broth_samples", "audits", "sorted_bins", "votes", "actions", "qc_checks", "meals", "events") | |
| args = sys.argv[1:] | |
| sys.stdout.reconfigure(errors="replace") # a cp1252 console can't print Thai paths | |
| with LOCK: | |
| if not args: | |
| info = data_mode_info() | |
| print(f"EatBack data mode: {info['mode']}" + (f" (since {info['since']})" if info["since"] else "")) | |
| elif args[0] == "real": | |
| rounds = one("SELECT COUNT(*) AS n FROM meals")["n"] | |
| if rounds and "--fresh" not in args and "--keep" not in args: | |
| sys.exit(f"This database has {rounds} rounds, most likely demo data. Add --fresh to delete them and start " | |
| "clean (menus, recipes, settings and PINs stay), or --keep to keep them as the school's own records.") | |
| try: | |
| print(f"Backup: {backup.BACKUP_DIR / backup.make('before-real')['name']}") | |
| except backup.BackupError as e: | |
| sys.exit(f"Backup failed, nothing changed: {e}") | |
| if "--fresh" in args: | |
| for t in RECORDS: | |
| run(f"DELETE FROM {t}") | |
| print(f"Deleted {rounds} demo rounds and their records. Each grade starts its baseline from zero.") | |
| set_data_mode("real") | |
| print("EatBack data mode: real. Demo resets are now refused; start the server to get the role PINs.") | |
| elif args[0] == "demo" and "--confirm" in args: | |
| set_data_mode("demo") | |
| print("EatBack data mode: demo. The reset buttons can delete every record again.") | |
| elif args[0] == "demo": | |
| sys.exit(f"Still {data_mode()}. Add --confirm: in demo mode a reset deletes every record in this database.") | |
| else: | |
| sys.exit(__doc__) | |