| 
				
			 | 
			
			
				@@ -114,6 +114,7 @@ class Settlement(db.Model): 
			 | 
		
	
		
			
			| 
				114
			 | 
			
				114
			 | 
			
			
				             Consumption.query.filter_by(settlement=self) 
			 | 
		
	
		
			
			| 
				115
			 | 
			
				115
			 | 
			
			
				             .filter_by(reversed=False) 
			 | 
		
	
		
			
			| 
				116
			 | 
			
				116
			 | 
			
			
				             .group_by(Consumption.consumption_type_id) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				117
			 | 
			
			
				+            .order_by(ConsumptionType.name) 
			 | 
		
	
		
			
			| 
				117
			 | 
			
				118
			 | 
			
			
				             .outerjoin(ConsumptionType) 
			 | 
		
	
		
			
			| 
				118
			 | 
			
				119
			 | 
			
			
				             .with_entities( 
			 | 
		
	
		
			
			| 
				119
			 | 
			
				120
			 | 
			
			
				                 Consumption.consumption_type_id, 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -125,6 +126,36 @@ class Settlement(db.Model): 
			 | 
		
	
		
			
			| 
				125
			 | 
			
				126
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				126
			 | 
			
				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
			 | 
			
				160
			 | 
			
			
				 class ConsumptionType(db.Model): 
			 | 
		
	
		
			
			| 
				130
			 | 
			
				161
			 | 
			
			
				     """ Represents a type of consumption to be counted. """ 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -387,6 +418,16 @@ def get_settlements(): 
			 | 
		
	
		
			
			| 
				387
			 | 
			
				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
			 | 
			
				431
			 | 
			
			
				 @app.route("/settlements", methods=["POST"]) 
			 | 
		
	
		
			
			| 
				391
			 | 
			
				432
			 | 
			
			
				 def add_settlement(): 
			 | 
		
	
		
			
			| 
				392
			 | 
			
				433
			 | 
			
			
				     """ Create a Settlement, and link all un-settled Consumptions to it. """ 
			 |