Browse Source

Allow extracting per-person counts per settlement

Maarten van den Berg 6 years ago
parent
commit
e4e40c1ddf
1 changed files with 41 additions and 0 deletions
  1. 41 0
      piket_server/__init__.py

+ 41 - 0
piket_server/__init__.py

114
             Consumption.query.filter_by(settlement=self)
114
             Consumption.query.filter_by(settlement=self)
115
             .filter_by(reversed=False)
115
             .filter_by(reversed=False)
116
             .group_by(Consumption.consumption_type_id)
116
             .group_by(Consumption.consumption_type_id)
117
+            .order_by(ConsumptionType.name)
117
             .outerjoin(ConsumptionType)
118
             .outerjoin(ConsumptionType)
118
             .with_entities(
119
             .with_entities(
119
                 Consumption.consumption_type_id,
120
                 Consumption.consumption_type_id,
125
 
126
 
126
         return {r[0]: {"name": r[1], "count": r[2]} for r in q}
127
         return {r[0]: {"name": r[1], "count": r[2]} for r in q}
127
 
128
 
129
+    @property
130
+    def per_person(self) -> dict:
131
+        # Get keys of seen consumption_types
132
+        c_types = self.consumption_summary.keys()
133
+
134
+        result = {}
135
+        for type in c_types:
136
+            c_type = ConsumptionType.query.get(type)
137
+            result[type] = {"consumption_type": c_type.as_dict, "counts": {}}
138
+
139
+            q = (
140
+                Consumption.query.filter_by(settlement=self)
141
+                .filter_by(reversed=False)
142
+                .filter_by(consumption_type=c_type)
143
+                .group_by(Consumption.person_id)
144
+                .order_by(Person.name)
145
+                .outerjoin(Person)
146
+                .with_entities(
147
+                    Person.person_id,
148
+                    Person.name,
149
+                    func.count(Consumption.consumption_id),
150
+                )
151
+                .all()
152
+            )
153
+
154
+            for row in q:
155
+                result[type]["counts"][row[0]] = {"name": row[1], "count": row[2]}
156
+
157
+        return result
158
+
128
 
159
 
129
 class ConsumptionType(db.Model):
160
 class ConsumptionType(db.Model):
130
     """ Represents a type of consumption to be counted. """
161
     """ Represents a type of consumption to be counted. """
387
     return jsonify(settlements=[s.as_dict for s in result])
418
     return jsonify(settlements=[s.as_dict for s in result])
388
 
419
 
389
 
420
 
421
+@app.route("/settlements/<int:settlement_id>", methods=["GET"])
422
+def get_settlement(settlement_id: int):
423
+    """ Show full details for a single Settlement. """
424
+    s = Settlement.query.get_or_404(settlement_id)
425
+
426
+    per_person = s.per_person
427
+
428
+    return jsonify(settlement=s.as_dict, count_info=per_person)
429
+
430
+
390
 @app.route("/settlements", methods=["POST"])
431
 @app.route("/settlements", methods=["POST"])
391
 def add_settlement():
432
 def add_settlement():
392
     """ Create a Settlement, and link all un-settled Consumptions to it. """
433
     """ Create a Settlement, and link all un-settled Consumptions to it. """