| 
				
			 | 
			
			
				@@ -2,6 +2,7 @@ 
			 | 
		
	
		
			
			| 
				2
			 | 
			
				2
			 | 
			
			
				 Provides the graphical front-end for Piket. 
			 | 
		
	
		
			
			| 
				3
			 | 
			
				3
			 | 
			
			
				 """ 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				4
			 | 
			
			
				 import sys 
			 | 
		
	
		
			
			| 
				
			 | 
			
				5
			 | 
			
			
				+from urllib.parse import urljoin 
			 | 
		
	
		
			
			| 
				5
			 | 
			
				6
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				6
			 | 
			
				7
			 | 
			
			
				 from PySide2.QtWidgets import QApplication, QPushButton, QGridLayout, QWidget 
			 | 
		
	
		
			
			| 
				7
			 | 
			
				8
			 | 
			
			
				 from PySide2.QtGui import QFont 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -15,22 +16,21 @@ def plop() -> None: 
			 | 
		
	
		
			
			| 
				15
			 | 
			
				16
			 | 
			
			
				     PLOP_WAVE.play() 
			 | 
		
	
		
			
			| 
				16
			 | 
			
				17
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				17
			 | 
			
				18
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				18
			 | 
			
				
			 | 
			
			
				-from piket_server import PEOPLE 
			 | 
		
	
		
			
			| 
				
			 | 
			
				19
			 | 
			
			
				+SERVER_URL = "http://127.0.0.1:5000" 
			 | 
		
	
		
			
			| 
				19
			 | 
			
				20
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				20
			 | 
			
				21
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				21
			 | 
			
				
			 | 
			
			
				-FONT = QFont("Helvetica", 15) 
			 | 
		
	
		
			
			| 
				22
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				23
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				24
			 | 
			
				
			 | 
			
			
				-def get_names() -> {int: (str, int)}: 
			 | 
		
	
		
			
			| 
				25
			 | 
			
				
			 | 
			
			
				-    return PEOPLE 
			 | 
		
	
		
			
			| 
				
			 | 
			
				22
			 | 
			
			
				+def get_people(): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				23
			 | 
			
			
				+    request = requests.get(urljoin(SERVER_URL, "/people")) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				24
			 | 
			
			
				+    return request.json()["people"] 
			 | 
		
	
		
			
			| 
				26
			 | 
			
				25
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				27
			 | 
			
				26
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				28
			 | 
			
				27
			 | 
			
			
				 class NameButton(QPushButton): 
			 | 
		
	
		
			
			| 
				29
			 | 
			
				28
			 | 
			
			
				     """ Wraps a QPushButton to provide a counter. """ 
			 | 
		
	
		
			
			| 
				30
			 | 
			
				29
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				31
			 | 
			
				
			 | 
			
			
				-    def __init__(self, name: str, count: int, *args, **kwargs) -> None: 
			 | 
		
	
		
			
			| 
				32
			 | 
			
				
			 | 
			
			
				-        self.name = name 
			 | 
		
	
		
			
			| 
				33
			 | 
			
				
			 | 
			
			
				-        self.count = count 
			 | 
		
	
		
			
			| 
				
			 | 
			
				30
			 | 
			
			
				+    def __init__(self, person: dict, *args, **kwargs) -> None: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				31
			 | 
			
			
				+        self.person_id = person["id"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				32
			 | 
			
			
				+        self.name = person["name"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				33
			 | 
			
			
				+        self.count = person["count"] 
			 | 
		
	
		
			
			| 
				34
			 | 
			
				34
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				35
			 | 
			
				35
			 | 
			
			
				         super().__init__(self.current_label, *args, **kwargs) 
			 | 
		
	
		
			
			| 
				36
			 | 
			
				36
			 | 
			
			
				  
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -41,9 +41,17 @@ class NameButton(QPushButton): 
			 | 
		
	
		
			
			| 
				41
			 | 
			
				41
			 | 
			
			
				         return f"{self.name} ({self.count})" 
			 | 
		
	
		
			
			| 
				42
			 | 
			
				42
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				43
			 | 
			
				43
			 | 
			
			
				     def plop(self) -> None: 
			 | 
		
	
		
			
			| 
				44
			 | 
			
				
			 | 
			
			
				-        self.count += 1 
			 | 
		
	
		
			
			| 
				45
			 | 
			
				
			 | 
			
			
				-        plop() 
			 | 
		
	
		
			
			| 
				46
			 | 
			
				
			 | 
			
			
				-        self.setText(self.current_label) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				44
			 | 
			
			
				+        r = requests.post( 
			 | 
		
	
		
			
			| 
				
			 | 
			
				45
			 | 
			
			
				+            urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption") 
			 | 
		
	
		
			
			| 
				
			 | 
			
				46
			 | 
			
			
				+        ) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+        if r.status_code == 200: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				48
			 | 
			
			
				+            json = r.json() 
			 | 
		
	
		
			
			| 
				
			 | 
			
				49
			 | 
			
			
				+            person = json["person"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+            self.count = person["count"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				51
			 | 
			
			
				+            self.setText(self.current_label) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+            plop() 
			 | 
		
	
		
			
			| 
				
			 | 
			
				53
			 | 
			
			
				+        else: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+            print("Oh shit kapot") 
			 | 
		
	
		
			
			| 
				47
			 | 
			
				55
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				48
			 | 
			
				56
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				49
			 | 
			
				57
			 | 
			
			
				 class NameButtons(QWidget): 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -56,9 +64,8 @@ class NameButtons(QWidget): 
			 | 
		
	
		
			
			| 
				56
			 | 
			
				64
			 | 
			
			
				     def init_ui(self) -> None: 
			 | 
		
	
		
			
			| 
				57
			 | 
			
				65
			 | 
			
			
				         self.layout = QGridLayout() 
			 | 
		
	
		
			
			| 
				58
			 | 
			
				66
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				59
			 | 
			
				
			 | 
			
			
				-        for index, id in enumerate(PEOPLE): 
			 | 
		
	
		
			
			| 
				60
			 | 
			
				
			 | 
			
			
				-            person = PEOPLE[id] 
			 | 
		
	
		
			
			| 
				61
			 | 
			
				
			 | 
			
			
				-            button = NameButton(person["name"], person["count"]) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				67
			 | 
			
			
				+        for index, person in enumerate(get_people()): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				68
			 | 
			
			
				+            button = NameButton(person) 
			 | 
		
	
		
			
			| 
				62
			 | 
			
				69
			 | 
			
			
				             self.layout.addWidget(button, index // 2, index % 2) 
			 | 
		
	
		
			
			| 
				63
			 | 
			
				70
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				64
			 | 
			
				71
			 | 
			
			
				         self.setLayout(self.layout) 
			 |