File size: 3,183 Bytes
052adbf
 
 
9aaa92e
 
 
 
 
052adbf
 
9aaa92e
 
052adbf
 
 
 
 
 
 
9aaa92e
 
 
 
 
052adbf
 
9aaa92e
052adbf
 
 
 
 
9aaa92e
 
 
 
 
 
 
 
 
 
 
 
052adbf
9aaa92e
052adbf
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""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__)