| 
				
			 | 
			
			
				@@ -3,7 +3,6 @@ Provides the graphical front-end for Piket. 
			 | 
		
	
		
			
			| 
				3
			 | 
			
				3
			 | 
			
			
				 """ 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				4
			 | 
			
			
				 import os 
			 | 
		
	
		
			
			| 
				5
			 | 
			
				5
			 | 
			
			
				 import sys 
			 | 
		
	
		
			
			| 
				6
			 | 
			
				
			 | 
			
			
				-from urllib.parse import urljoin 
			 | 
		
	
		
			
			| 
				7
			 | 
			
				6
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				8
			 | 
			
				7
			 | 
			
			
				 # pylint: disable=E0611 
			 | 
		
	
		
			
			| 
				9
			 | 
			
				8
			 | 
			
			
				 from PySide2.QtWidgets import ( 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -20,7 +19,6 @@ from PySide2.QtGui import QIcon 
			 | 
		
	
		
			
			| 
				20
			 | 
			
				19
			 | 
			
			
				 from PySide2.QtCore import QSize, Qt 
			 | 
		
	
		
			
			| 
				21
			 | 
			
				20
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				22
			 | 
			
				21
			 | 
			
			
				 # pylint: enable=E0611 
			 | 
		
	
		
			
			| 
				23
			 | 
			
				
			 | 
			
			
				-import requests 
			 | 
		
	
		
			
			| 
				24
			 | 
			
				22
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				25
			 | 
			
				23
			 | 
			
			
				 try: 
			 | 
		
	
		
			
			| 
				26
			 | 
			
				24
			 | 
			
			
				     import dbus 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -28,6 +26,7 @@ except ImportError: 
			 | 
		
	
		
			
			| 
				28
			 | 
			
				26
			 | 
			
			
				     dbus = None 
			 | 
		
	
		
			
			| 
				29
			 | 
			
				27
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				30
			 | 
			
				28
			 | 
			
			
				 from piket_client.sound import PLOP_WAVE 
			 | 
		
	
		
			
			| 
				
			 | 
			
				29
			 | 
			
			
				+from piket_client.model import Person 
			 | 
		
	
		
			
			| 
				31
			 | 
			
				30
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				32
			 | 
			
				31
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				33
			 | 
			
				32
			 | 
			
			
				 def plop() -> None: 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -35,46 +34,31 @@ def plop() -> None: 
			 | 
		
	
		
			
			| 
				35
			 | 
			
				34
			 | 
			
			
				     PLOP_WAVE.play() 
			 | 
		
	
		
			
			| 
				36
			 | 
			
				35
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				37
			 | 
			
				36
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				38
			 | 
			
				
			 | 
			
			
				-SERVER_URL = "http://127.0.0.1:5000" 
			 | 
		
	
		
			
			| 
				39
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				40
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				41
			 | 
			
				
			 | 
			
			
				-def get_people() -> [dict]: 
			 | 
		
	
		
			
			| 
				42
			 | 
			
				
			 | 
			
			
				-    """ Request list of active people from the server. """ 
			 | 
		
	
		
			
			| 
				43
			 | 
			
				
			 | 
			
			
				-    request = requests.get(urljoin(SERVER_URL, "/people")) 
			 | 
		
	
		
			
			| 
				44
			 | 
			
				
			 | 
			
			
				-    return request.json()["people"] 
			 | 
		
	
		
			
			| 
				45
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				46
			 | 
			
				
			 | 
			
			
				- 
			 | 
		
	
		
			
			| 
				47
			 | 
			
				37
			 | 
			
			
				 class NameButton(QPushButton): 
			 | 
		
	
		
			
			| 
				48
			 | 
			
				38
			 | 
			
			
				     """ Wraps a QPushButton to provide a counter. """ 
			 | 
		
	
		
			
			| 
				49
			 | 
			
				39
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				50
			 | 
			
				
			 | 
			
			
				-    def __init__(self, person: dict, *args, **kwargs) -> None: 
			 | 
		
	
		
			
			| 
				51
			 | 
			
				
			 | 
			
			
				-        self.person_id = person["person_id"] 
			 | 
		
	
		
			
			| 
				52
			 | 
			
				
			 | 
			
			
				-        self.name = person["name"] 
			 | 
		
	
		
			
			| 
				53
			 | 
			
				
			 | 
			
			
				-        self.count = person["consumptions"]["1"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				40
			 | 
			
			
				+    def __init__(self, person: Person, *args, **kwargs) -> None: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				41
			 | 
			
			
				+        self.person = person 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+        self.count = person.consumptions["1"] 
			 | 
		
	
		
			
			| 
				54
			 | 
			
				43
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				55
			 | 
			
				44
			 | 
			
			
				         super().__init__(self.current_label, *args, **kwargs) 
			 | 
		
	
		
			
			| 
				56
			 | 
			
				45
			 | 
			
			
				         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 
			 | 
		
	
		
			
			| 
				57
			 | 
			
				46
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				58
			 | 
			
				
			 | 
			
			
				-        self.clicked.connect(self.plop) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+        self.clicked.connect(self.process_click) 
			 | 
		
	
		
			
			| 
				59
			 | 
			
				48
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				60
			 | 
			
				49
			 | 
			
			
				     @property 
			 | 
		
	
		
			
			| 
				61
			 | 
			
				50
			 | 
			
			
				     def current_label(self) -> str: 
			 | 
		
	
		
			
			| 
				62
			 | 
			
				51
			 | 
			
			
				         """ Return the label to show on the button. """ 
			 | 
		
	
		
			
			| 
				63
			 | 
			
				
			 | 
			
			
				-        return f"{self.name} ({self.count})" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+        return f"{self.person.name} ({self.count})" 
			 | 
		
	
		
			
			| 
				64
			 | 
			
				53
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				65
			 | 
			
				
			 | 
			
			
				-    def plop(self) -> None: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+    def process_click(self) -> None: 
			 | 
		
	
		
			
			| 
				66
			 | 
			
				55
			 | 
			
			
				         """ Process a click on this button. """ 
			 | 
		
	
		
			
			| 
				67
			 | 
			
				
			 | 
			
			
				-        req = requests.post( 
			 | 
		
	
		
			
			| 
				68
			 | 
			
				
			 | 
			
			
				-            urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption") 
			 | 
		
	
		
			
			| 
				69
			 | 
			
				
			 | 
			
			
				-        ) 
			 | 
		
	
		
			
			| 
				70
			 | 
			
				
			 | 
			
			
				-        if req.status_code == 200: 
			 | 
		
	
		
			
			| 
				71
			 | 
			
				
			 | 
			
			
				-            json = req.json() 
			 | 
		
	
		
			
			| 
				72
			 | 
			
				
			 | 
			
			
				-            person = json["person"] 
			 | 
		
	
		
			
			| 
				73
			 | 
			
				
			 | 
			
			
				-            self.count = person["consumptions"]["1"] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+        if self.person.add_consumption(): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				57
			 | 
			
			
				+            self.count = self.person.consumptions["1"] 
			 | 
		
	
		
			
			| 
				74
			 | 
			
				58
			 | 
			
			
				             self.setText(self.current_label) 
			 | 
		
	
		
			
			| 
				75
			 | 
			
				59
			 | 
			
			
				             plop() 
			 | 
		
	
		
			
			| 
				76
			 | 
			
				60
			 | 
			
			
				         else: 
			 | 
		
	
		
			
			| 
				77
			 | 
			
				
			 | 
			
			
				-            print("Oh shit kapot") 
			 | 
		
	
		
			
			| 
				
			 | 
			
				61
			 | 
			
			
				+            print("Jantoeternuitje, kapot") 
			 | 
		
	
		
			
			| 
				78
			 | 
			
				62
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				79
			 | 
			
				63
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				80
			 | 
			
				64
			 | 
			
			
				 class NameButtons(QWidget): 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -92,7 +76,7 @@ class NameButtons(QWidget): 
			 | 
		
	
		
			
			| 
				92
			 | 
			
				76
			 | 
			
			
				         for each. """ 
			 | 
		
	
		
			
			| 
				93
			 | 
			
				77
			 | 
			
			
				         self.layout = QGridLayout() 
			 | 
		
	
		
			
			| 
				94
			 | 
			
				78
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				95
			 | 
			
				
			 | 
			
			
				-        for index, person in enumerate(get_people()): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				79
			 | 
			
			
				+        for index, person in enumerate(Person.get_all()): 
			 | 
		
	
		
			
			| 
				96
			 | 
			
				80
			 | 
			
			
				             button = NameButton(person) 
			 | 
		
	
		
			
			| 
				97
			 | 
			
				81
			 | 
			
			
				             self.layout.addWidget(button, index // 2, index % 2) 
			 | 
		
	
		
			
			| 
				98
			 | 
			
				82
			 | 
			
			
				  
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -175,9 +159,8 @@ class PiketMainWindow(QMainWindow): 
			 | 
		
	
		
			
			| 
				175
			 | 
			
				159
			 | 
			
			
				         ) 
			 | 
		
	
		
			
			| 
				176
			 | 
			
				160
			 | 
			
			
				         self.hide_keyboard() 
			 | 
		
	
		
			
			| 
				177
			 | 
			
				161
			 | 
			
			
				         if ok and name: 
			 | 
		
	
		
			
			| 
				178
			 | 
			
				
			 | 
			
			
				-            req = requests.post( 
			 | 
		
	
		
			
			| 
				179
			 | 
			
				
			 | 
			
			
				-                urljoin(SERVER_URL, "people"), json={"person": {"name": name}} 
			 | 
		
	
		
			
			| 
				180
			 | 
			
				
			 | 
			
			
				-            ) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				162
			 | 
			
			
				+            person = Person(name=name) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				163
			 | 
			
			
				+            person = person.save() 
			 | 
		
	
		
			
			| 
				181
			 | 
			
				164
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				182
			 | 
			
				165
			 | 
			
			
				             self.main_widget = NameButtons() 
			 | 
		
	
		
			
			| 
				183
			 | 
			
				166
			 | 
			
			
				             self.setCentralWidget(self.main_widget) 
			 |