瀏覽代碼

Add command to show settlement

Maarten van den Berg 5 年之前
父節點
當前提交
b67e022de5
共有 1 個文件被更改,包括 41 次插入2 次删除
  1. 41 2
      piket_client/cli.py

+ 41 - 2
piket_client/cli.py

@@ -1,5 +1,6 @@
1 1
 import click
2
-from piket_client.model import ServerStatus, NetworkError
2
+from piket_client.model import ServerStatus, NetworkError, Consumption, Settlement
3
+from prettytable import PrettyTable
3 4
 
4 5
 
5 6
 @click.group()
@@ -23,7 +24,9 @@ def status():
23 24
     open_consumptions = ServerStatus.unsettled_consumptions()
24 25
 
25 26
     if isinstance(open_consumptions, NetworkError):
26
-        print_error(f"Failed to get unsettled consumptions, error {open_consumptions.value}")
27
+        print_error(
28
+            f"Failed to get unsettled consumptions, error {open_consumptions.value}"
29
+        )
27 30
         return
28 31
 
29 32
     click.echo(f"There are {open_consumptions.amount} unsettled consumptions.")
@@ -43,6 +46,42 @@ def settlements():
43 46
     pass
44 47
 
45 48
 
49
+@settlements.command("show")
50
+@click.argument("settlement_id", type=click.INT)
51
+def show_settlement(settlement_id: int) -> None:
52
+    """Get and view the contents of a Settlement."""
53
+    s = Settlement.get(settlement_id)
54
+
55
+    if isinstance(s, NetworkError):
56
+        print_error(f"Could not get Settlement: {s.value}")
57
+        return
58
+
59
+    click.echo(f"Settlement {settlement_id}, \"{s.name}\"")
60
+
61
+    click.echo(f"Summary:")
62
+    for key, value in s.consumption_summary.items():
63
+        click.echo(f" - {value['count']} {value['name']} ({key})")
64
+
65
+    ct_name_by_id = {key: value["name"] for key, value in s.consumption_summary.items()}
66
+
67
+    table = PrettyTable()
68
+    table.field_names = ["Name", *ct_name_by_id.values()]
69
+    table.sortby = "Name"
70
+    table.align = "r"
71
+    table.align["Name"] = "l"  # type: ignore
72
+
73
+    zero_fields = {k: "" for k in ct_name_by_id.values()}
74
+
75
+    for item in s.per_person_counts.values():
76
+        r = {"Name": item["full_name"], **zero_fields}
77
+        for key, value in item["counts"].items():
78
+            r[ct_name_by_id[key]] = value
79
+
80
+        table.add_row(r.values())
81
+
82
+    print(table)
83
+
84
+
46 85
 def print_ok(msg: str) -> None:
47 86
     click.echo(click.style(msg, fg="green"))
48 87