Просмотр исходного кода

Add script to dump exports to CSV

Maarten van den Berg лет назад: 6
Родитель
Сommit
aa724f1c40
1 измененных файлов с 96 добавлено и 0 удалено
  1. 96 0
      piket_client/export.py

+ 96 - 0
piket_client/export.py

@@ -0,0 +1,96 @@
1
+"""
2
+Provides functionality for creating Export files.
3
+"""
4
+import csv
5
+import os
6
+import sys
7
+from piket_client.model import Settlement, Export, ConsumptionType, Person
8
+
9
+
10
+if len(sys.argv) > 1:
11
+    eid = int(sys.argv[1])
12
+    export = Export.get(eid)
13
+else:
14
+    export = Export.create()
15
+
16
+filename_base = export.created_at.strftime("Export %Y-%m-%d")
17
+
18
+# Determine which consumption types exist
19
+consumption_types = [
20
+    ConsumptionType.get(id)
21
+    for id in {
22
+        type_id
23
+        for s in export.settlements
24
+        for type_id in s["consumption_summary"].keys()
25
+    }
26
+]
27
+
28
+consumption_types.sort(key=lambda x: x.name)
29
+
30
+extended_settlements = [
31
+    Settlement.get(id) for id in map(lambda x: x["settlement_id"], export.settlements)
32
+]
33
+extended_settlements.sort(key=lambda x: x.name)
34
+
35
+person_ids = set()
36
+for settlement in extended_settlements:
37
+    for ctype in settlement.count_info.values():
38
+        for person_id in ctype["counts"].keys():
39
+            person_ids.add(person_id)
40
+
41
+persons = [Person.get(person_id) for person_id in person_ids]
42
+persons.sort(key=lambda p: p.name)
43
+
44
+fieldnames = [
45
+    "Naam",
46
+    *map(lambda x: f"{x.name} ({x.settlement_id})", extended_settlements),
47
+    "Totaal",
48
+]
49
+
50
+# Create overview table
51
+with open(f"{filename_base} Overzicht.csv", "w") as outf:
52
+
53
+    writer = csv.DictWriter(outf, fieldnames)
54
+
55
+    writer.writeheader()
56
+
57
+    for consumption_type in consumption_types:
58
+        row = {"Naam": consumption_type.name}
59
+        total = 0
60
+        for settlement in extended_settlements:
61
+            strid = str(consumption_type.consumption_type_id)
62
+            info = settlement.consumption_summary.get(strid, {})
63
+
64
+            cell = info.get("count", "")
65
+            if cell:
66
+                total += cell
67
+
68
+            row[f"{settlement.name} ({settlement.settlement_id})"] = cell
69
+
70
+        row["Totaal"] = total
71
+        writer.writerow(row)
72
+
73
+# Create table per consumption type
74
+for c_type in consumption_types:
75
+    strid = str(c_type.consumption_type_id)
76
+    with open(f"{filename_base} {c_type.name}.csv", "w") as outf:
77
+        writer = csv.DictWriter(outf, fieldnames)
78
+        writer.writeheader()
79
+
80
+        for person in persons:
81
+            row = {"Naam": person.name}
82
+            total = 0
83
+
84
+            for settlement in extended_settlements:
85
+                info = settlement.count_info.get(strid, {})
86
+                countinfo = info.get("counts", {})
87
+                pinfo = countinfo.get(str(person.person_id), {})
88
+
89
+                cell = pinfo.get("count", "")
90
+                if cell:
91
+                    total += cell
92
+
93
+                row[f"{settlement.name} ({settlement.settlement_id})"] = cell
94
+
95
+            row["Totaal"] = total
96
+            writer.writerow(row)