Parcourir la Source

Expose active, add patch route for ctypes

Maarten van den Berg il y a 5 ans
Parent
commit
fe58af3700
2 fichiers modifiés avec 22 ajouts et 1 suppressions
  1. 1 0
      piket_server/models.py
  2. 21 1
      piket_server/routes/consumption_types.py

+ 1 - 0
piket_server/models.py

@@ -209,6 +209,7 @@ class ConsumptionType(db.Model):
209 209
             "consumption_type_id": self.consumption_type_id,
210 210
             "name": self.name,
211 211
             "icon": self.icon,
212
+            "active": self.active,
212 213
         }
213 214
 
214 215
 

+ 21 - 1
piket_server/routes/consumption_types.py

@@ -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