| 
				
			 | 
			
			
				@@ -2,7 +2,7 @@ 
			 | 
		
	
		
			
			| 
				2
			 | 
			
				2
			 | 
			
			
				 Piket server, handles events generated by the client. 
			 | 
		
	
		
			
			| 
				3
			 | 
			
				3
			 | 
			
			
				 """ 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				4
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				5
			 | 
			
				
			 | 
			
			
				-from flask import Flask, jsonify 
			 | 
		
	
		
			
			| 
				
			 | 
			
				5
			 | 
			
			
				+from flask import Flask, jsonify, abort, request 
			 | 
		
	
		
			
			| 
				6
			 | 
			
				6
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				7
			 | 
			
				7
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				8
			 | 
			
				8
			 | 
			
			
				 app = Flask("piket_server") 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -24,9 +24,62 @@ PRESET_NAMES = [ 
			 | 
		
	
		
			
			| 
				24
			 | 
			
				24
			 | 
			
			
				 ] 
			 | 
		
	
		
			
			| 
				25
			 | 
			
				25
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				26
			 | 
			
				26
			 | 
			
			
				 PEOPLE = {index: {"name": name, "count": 0} for index, name in enumerate(PRESET_NAMES)} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				27
			 | 
			
			
				+NEXT_ID = len(PEOPLE) 
			 | 
		
	
		
			
			| 
				27
			 | 
			
				28
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				28
			 | 
			
				29
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				29
			 | 
			
				
			 | 
			
			
				-@app.route("/people") 
			 | 
		
	
		
			
			| 
				30
			 | 
			
				
			 | 
			
			
				-def get_people() -> None: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				30
			 | 
			
			
				+@app.route("/people", methods=["GET"]) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				31
			 | 
			
			
				+def get_people(): 
			 | 
		
	
		
			
			| 
				31
			 | 
			
				32
			 | 
			
			
				     """ Return a list of currently known people. """ 
			 | 
		
	
		
			
			| 
				32
			 | 
			
				
			 | 
			
			
				-    return jsonify(PEOPLE) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				33
			 | 
			
			
				+    return jsonify(people=PEOPLE) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				34
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				35
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				36
			 | 
			
			
				+@app.route("/people/<int:person_id>", methods=["GET"]) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				37
			 | 
			
			
				+def get_person(person_id: int): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				38
			 | 
			
			
				+    person = PEOPLE.get(person_id) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				39
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				40
			 | 
			
			
				+    if not person: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				41
			 | 
			
			
				+        abort(404) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				43
			 | 
			
			
				+    return jsonify(person=person) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				44
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				45
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				46
			 | 
			
			
				+@app.route("/people", methods=["POST"]) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+def add_person(): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				48
			 | 
			
			
				+    """ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				49
			 | 
			
			
				+    Add a new person. 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				51
			 | 
			
			
				+    Required parameters: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+    - name (str) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				53
			 | 
			
			
				+    """ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+    global NEXT_ID 
			 | 
		
	
		
			
			| 
				
			 | 
			
				55
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+    data = request.get_json() 
			 | 
		
	
		
			
			| 
				
			 | 
			
				57
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				58
			 | 
			
			
				+    if not data: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				59
			 | 
			
			
				+        abort(400) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				60
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				61
			 | 
			
			
				+    name = data.get("name") 
			 | 
		
	
		
			
			| 
				
			 | 
			
				62
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				63
			 | 
			
			
				+    if not name: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				64
			 | 
			
			
				+        abort(400) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				65
			 | 
			
			
				+    person = {"name": name, "count": 0} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				66
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				67
			 | 
			
			
				+    PEOPLE[NEXT_ID] = person 
			 | 
		
	
		
			
			| 
				
			 | 
			
				68
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				69
			 | 
			
			
				+    person["id"] = NEXT_ID 
			 | 
		
	
		
			
			| 
				
			 | 
			
				70
			 | 
			
			
				+    NEXT_ID += 1 
			 | 
		
	
		
			
			| 
				
			 | 
			
				71
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				72
			 | 
			
			
				+    return jsonify(person=person) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				73
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				74
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				75
			 | 
			
			
				+@app.route("/people/<int:person_id>/add_consumption", methods=["POST"]) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				76
			 | 
			
			
				+def add_consumption(person_id: int): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				77
			 | 
			
			
				+    person = PEOPLE.get(person_id) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				78
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				79
			 | 
			
			
				+    if not person: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				80
			 | 
			
			
				+        abort(404) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				81
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				82
			 | 
			
			
				+    increment = int(request.form.get("amount", 1)) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				83
			 | 
			
			
				+    person["count"] += increment 
			 | 
		
	
		
			
			| 
				
			 | 
			
				84
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				85
			 | 
			
			
				+    return jsonify(person=person) 
			 |