|
@@ -69,26 +69,26 @@ class Settlement(db.Model):
|
69
|
69
|
@property
|
70
|
70
|
def as_dict(self) -> dict:
|
71
|
71
|
return {
|
72
|
|
- 'settlement_id': self.settlement_id,
|
73
|
|
- 'name': self.name,
|
74
|
|
- 'consumption_summary': self.consumption_summary
|
|
72
|
+ "settlement_id": self.settlement_id,
|
|
73
|
+ "name": self.name,
|
|
74
|
+ "consumption_summary": self.consumption_summary,
|
75
|
75
|
}
|
76
|
76
|
|
77
|
77
|
@property
|
78
|
78
|
def consumption_summary(self) -> dict:
|
79
|
|
- q = Consumption.\
|
80
|
|
- query.\
|
81
|
|
- filter_by(settlement=self).\
|
82
|
|
- group_by(Consumption.consumption_type_id).\
|
83
|
|
- outerjoin(ConsumptionType).\
|
84
|
|
- with_entities(
|
85
|
|
- Consumption.consumption_type_id,
|
86
|
|
- ConsumptionType.name,
|
87
|
|
- func.count(Consumption.consumption_id),
|
88
|
|
- ).\
|
89
|
|
- all()
|
|
79
|
+ q = (
|
|
80
|
+ Consumption.query.filter_by(settlement=self)
|
|
81
|
+ .group_by(Consumption.consumption_type_id)
|
|
82
|
+ .outerjoin(ConsumptionType)
|
|
83
|
+ .with_entities(
|
|
84
|
+ Consumption.consumption_type_id,
|
|
85
|
+ ConsumptionType.name,
|
|
86
|
+ func.count(Consumption.consumption_id),
|
|
87
|
+ )
|
|
88
|
+ .all()
|
|
89
|
+ )
|
90
|
90
|
|
91
|
|
- return {r[0]: {'name': r[1], 'count': r[2]} for r in q}
|
|
91
|
+ return {r[0]: {"name": r[1], "count": r[2]} for r in q}
|
92
|
92
|
|
93
|
93
|
|
94
|
94
|
class ConsumptionType(db.Model):
|
|
@@ -157,34 +157,29 @@ def ping() -> None:
|
157
|
157
|
""" Return a status ping. """
|
158
|
158
|
return "Pong"
|
159
|
159
|
|
|
160
|
+
|
160
|
161
|
@app.route("/status")
|
161
|
162
|
def status() -> None:
|
162
|
163
|
""" Return a status dict with info about the database. """
|
163
|
|
- unsettled_q = Consumption.query.\
|
164
|
|
- filter_by(settlement=None).\
|
165
|
|
- filter_by(reversed=False)
|
|
164
|
+ unsettled_q = Consumption.query.filter_by(settlement=None).filter_by(reversed=False)
|
166
|
165
|
|
167
|
166
|
unsettled = unsettled_q.count()
|
168
|
167
|
|
169
|
168
|
first = None
|
170
|
169
|
last = None
|
171
|
170
|
if unsettled:
|
172
|
|
- last = unsettled_q.\
|
173
|
|
- order_by(Consumption.created_at.desc()).\
|
174
|
|
- first().\
|
175
|
|
- created_at.isoformat()
|
176
|
|
- first = unsettled_q.\
|
177
|
|
- order_by(Consumption.created_at.asc())\
|
178
|
|
- .first()\
|
179
|
|
- .created_at.isoformat()
|
180
|
|
-
|
181
|
|
- return jsonify({
|
182
|
|
- 'unsettled': {
|
183
|
|
- 'amount': unsettled,
|
184
|
|
- 'first': first,
|
185
|
|
- 'last': last,
|
186
|
|
- },
|
187
|
|
- })
|
|
171
|
+ last = (
|
|
172
|
+ unsettled_q.order_by(Consumption.created_at.desc())
|
|
173
|
+ .first()
|
|
174
|
+ .created_at.isoformat()
|
|
175
|
+ )
|
|
176
|
+ first = (
|
|
177
|
+ unsettled_q.order_by(Consumption.created_at.asc())
|
|
178
|
+ .first()
|
|
179
|
+ .created_at.isoformat()
|
|
180
|
+ )
|
|
181
|
+
|
|
182
|
+ return jsonify({"unsettled": {"amount": unsettled, "first": first, "last": last}})
|
188
|
183
|
|
189
|
184
|
|
190
|
185
|
# Person
|
|
@@ -222,7 +217,8 @@ def add_person():
|
222
|
217
|
return jsonify({"error": "Could not parse JSON."}), 400
|
223
|
218
|
|
224
|
219
|
data = json.get("person") or {}
|
225
|
|
- person = Person(name=data.get("name"))
|
|
220
|
+ person = Person(name=data.get("name"), active=data.get("active", False))
|
|
221
|
+
|
226
|
222
|
try:
|
227
|
223
|
db.session.add(person)
|
228
|
224
|
db.session.commit()
|
|
@@ -347,6 +343,7 @@ def add_consumption_type():
|
347
|
343
|
|
348
|
344
|
return jsonify(consumption_type=ct.as_dict), 201
|
349
|
345
|
|
|
346
|
+
|
350
|
347
|
# Settlement
|
351
|
348
|
@app.route("/settlements", methods=["GET"])
|
352
|
349
|
def get_settlements():
|
|
@@ -354,6 +351,7 @@ def get_settlements():
|
354
|
351
|
result = Settlement.query.all()
|
355
|
352
|
return jsonify(settlements=[s.as_dict for s in result])
|
356
|
353
|
|
|
354
|
+
|
357
|
355
|
@app.route("/settlements", methods=["POST"])
|
358
|
356
|
def add_settlement():
|
359
|
357
|
""" Create a Settlement, and link all un-settled Consumptions to it. """
|
|
@@ -362,14 +360,15 @@ def add_settlement():
|
362
|
360
|
if not json:
|
363
|
361
|
return jsonify({"error": "Could not parse JSON."}), 400
|
364
|
362
|
|
365
|
|
- data = json.get('settlement') or {}
|
366
|
|
- s = Settlement(name=data['name'])
|
|
363
|
+ data = json.get("settlement") or {}
|
|
364
|
+ s = Settlement(name=data["name"])
|
367
|
365
|
|
368
|
366
|
db.session.add(s)
|
369
|
367
|
db.session.commit()
|
370
|
368
|
|
371
|
|
- Consumption.query.filter_by(settlement=None).update({'settlement_id':
|
372
|
|
- s.settlement_id})
|
|
369
|
+ Consumption.query.filter_by(settlement=None).update(
|
|
370
|
+ {"settlement_id": s.settlement_id}
|
|
371
|
+ )
|
373
|
372
|
|
374
|
373
|
db.session.commit()
|
375
|
374
|
|