|
@@ -12,7 +12,13 @@ from piket_server.flask import app, db
|
12
|
12
|
@app.route("/consumption_types", methods=["GET"])
|
13
|
13
|
def get_consumption_types():
|
14
|
14
|
""" Return a list of currently active consumption types. """
|
15
|
|
- ctypes = ConsumptionType.query.filter_by(active=True).all()
|
|
15
|
+ try:
|
|
16
|
+ active = int(request.args.get("active", 1))
|
|
17
|
+
|
|
18
|
+ except ValueError:
|
|
19
|
+ return {}, 400
|
|
20
|
+
|
|
21
|
+ ctypes = ConsumptionType.query.filter_by(active=active).all()
|
16
|
22
|
result = [ct.as_dict for ct in ctypes]
|
17
|
23
|
return jsonify(consumption_types=result)
|
18
|
24
|
|
|
@@ -42,3 +48,17 @@ def add_consumption_type():
|
42
|
48
|
return jsonify({"error": "Invalid arguments for ConsumptionType."}), 400
|
43
|
49
|
|
44
|
50
|
return jsonify(consumption_type=ct.as_dict), 201
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+@app.route("/consumption_types/<int:consumption_type_id>", methods=["PATCH"])
|
|
54
|
+def activate_consumption_type(consumption_type_id: int):
|
|
55
|
+ ct = ConsumptionType.query.get_or_404(consumption_type_id)
|
|
56
|
+
|
|
57
|
+ data = request.json["consumption_type"]
|
|
58
|
+ new_active = data.get("active", True)
|
|
59
|
+
|
|
60
|
+ ct.active = new_active
|
|
61
|
+ db.session.add(ct)
|
|
62
|
+ db.session.commit()
|
|
63
|
+
|
|
64
|
+ return jsonify(consumption_type=ct.as_dict), 200
|