Browse Source

Expose active, add patch route for ctypes

Maarten van den Berg 5 years ago
parent
commit
4d67672ed1
2 changed files with 22 additions and 1 deletions
  1. 1 0
      piket_server/models.py
  2. 21 1
      piket_server/routes/consumption_types.py

+ 1 - 0
piket_server/models.py

209
             "consumption_type_id": self.consumption_type_id,
209
             "consumption_type_id": self.consumption_type_id,
210
             "name": self.name,
210
             "name": self.name,
211
             "icon": self.icon,
211
             "icon": self.icon,
212
+            "active": self.active,
212
         }
213
         }
213
 
214
 
214
 
215
 

+ 21 - 1
piket_server/routes/consumption_types.py

12
 @app.route("/consumption_types", methods=["GET"])
12
 @app.route("/consumption_types", methods=["GET"])
13
 def get_consumption_types():
13
 def get_consumption_types():
14
     """ Return a list of currently active consumption types. """
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
     result = [ct.as_dict for ct in ctypes]
22
     result = [ct.as_dict for ct in ctypes]
17
     return jsonify(consumption_types=result)
23
     return jsonify(consumption_types=result)
18
 
24
 
42
         return jsonify({"error": "Invalid arguments for ConsumptionType."}), 400
48
         return jsonify({"error": "Invalid arguments for ConsumptionType."}), 400
43
 
49
 
44
     return jsonify(consumption_type=ct.as_dict), 201
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