Browse Source

Add logic for adding buttons for people

Maarten van den Berg 6 years ago
parent
commit
45471f1598
3 changed files with 40 additions and 15 deletions
  1. 2 1
      .pylintrc
  2. 37 14
      piket_client/gui.py
  3. 1 0
      piket_server/__init__.py

+ 2 - 1
.pylintrc

140
         deprecated-sys-function,
140
         deprecated-sys-function,
141
         exception-escape,
141
         exception-escape,
142
         comprehension-escape,
142
         comprehension-escape,
143
-		too-few-public-methods
143
+		too-few-public-methods,
144
+		global-statement
144
 
145
 
145
 # Enable the message, report, category or checker with the given id(s). You can
146
 # Enable the message, report, category or checker with the given id(s). You can
146
 # either give multiple identifier separated by comma (,) or put this option
147
 # either give multiple identifier separated by comma (,) or put this option

+ 37 - 14
piket_client/gui.py

8
 # pylint: disable=E0611
8
 # pylint: disable=E0611
9
 from PySide2.QtWidgets import (
9
 from PySide2.QtWidgets import (
10
     QApplication,
10
     QApplication,
11
-    QPushButton,
12
     QGridLayout,
11
     QGridLayout,
13
-    QWidget,
12
+    QInputDialog,
14
     QMainWindow,
13
     QMainWindow,
14
+    QPushButton,
15
     QSizePolicy,
15
     QSizePolicy,
16
     QToolBar,
16
     QToolBar,
17
+    QWidget,
17
 )
18
 )
18
 from PySide2.QtGui import QIcon
19
 from PySide2.QtGui import QIcon
19
 from PySide2.QtCore import QSize
20
 from PySide2.QtCore import QSize
21
+
20
 # pylint: enable=E0611
22
 # pylint: enable=E0611
21
 import requests
23
 import requests
22
 
24
 
32
 
34
 
33
 
35
 
34
 def get_people() -> [dict]:
36
 def get_people() -> [dict]:
35
-    ''' Request list of active people from the server. '''
37
+    """ Request list of active people from the server. """
36
     request = requests.get(urljoin(SERVER_URL, "/people"))
38
     request = requests.get(urljoin(SERVER_URL, "/people"))
37
     return request.json()["people"]
39
     return request.json()["people"]
38
 
40
 
52
 
54
 
53
     @property
55
     @property
54
     def current_label(self) -> str:
56
     def current_label(self) -> str:
55
-        ''' Return the label to show on the button. '''
57
+        """ Return the label to show on the button. """
56
         return f"{self.name} ({self.count})"
58
         return f"{self.name} ({self.count})"
57
 
59
 
58
     def plop(self) -> None:
60
     def plop(self) -> None:
59
-        ''' Process a click on this button. '''
61
+        """ Process a click on this button. """
60
         req = requests.post(
62
         req = requests.post(
61
             urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption")
63
             urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption")
62
         )
64
         )
71
 
73
 
72
 
74
 
73
 class NameButtons(QWidget):
75
 class NameButtons(QWidget):
74
-    ''' Main widget responsible for capturing presses and registering them.
75
-    '''
76
+    """ Main widget responsible for capturing presses and registering them.
77
+    """
78
+
76
     def __init__(self) -> None:
79
     def __init__(self) -> None:
77
         super().__init__()
80
         super().__init__()
78
 
81
 
80
         self.init_ui()
83
         self.init_ui()
81
 
84
 
82
     def init_ui(self) -> None:
85
     def init_ui(self) -> None:
83
-        ''' Initialize UI: build GridLayout, retrieve People and build a button
84
-        for each. '''
86
+        """ Initialize UI: build GridLayout, retrieve People and build a button
87
+        for each. """
85
         self.layout = QGridLayout()
88
         self.layout = QGridLayout()
86
 
89
 
87
         for index, person in enumerate(get_people()):
90
         for index, person in enumerate(get_people()):
92
 
95
 
93
 
96
 
94
 class PiketMainWindow(QMainWindow):
97
 class PiketMainWindow(QMainWindow):
95
-    ''' QMainWindow subclass responsible for showing the main application
96
-    window. '''
98
+    """ QMainWindow subclass responsible for showing the main application
99
+    window. """
100
+
97
     def __init__(self) -> None:
101
     def __init__(self) -> None:
98
         super().__init__()
102
         super().__init__()
99
 
103
 
102
         self.init_ui()
106
         self.init_ui()
103
 
107
 
104
     def init_ui(self) -> None:
108
     def init_ui(self) -> None:
105
-        ''' Initialize the UI: construct main widget and toolbar. '''
109
+        """ Initialize the UI: construct main widget and toolbar. """
106
         self.main_widget = NameButtons()
110
         self.main_widget = NameButtons()
107
         self.setCentralWidget(self.main_widget)
111
         self.setCentralWidget(self.main_widget)
108
 
112
 
114
         self.toolbar.setIconSize(QSize(icon_size, icon_size))
118
         self.toolbar.setIconSize(QSize(icon_size, icon_size))
115
 
119
 
116
         # Left
120
         # Left
117
-        self.toolbar.addAction(self.load_icon("add_person.svg"), "Nieuw persoon")
121
+        self.toolbar.addAction(
122
+            self.load_icon("add_person.svg"), "Nieuw persoon", self.add_person
123
+        )
118
         self.toolbar.addAction(self.load_icon("undo.svg"), "Heydrich")
124
         self.toolbar.addAction(self.load_icon("undo.svg"), "Heydrich")
119
 
125
 
120
         self.toolbar.addWidget(self.create_spacer())
126
         self.toolbar.addWidget(self.create_spacer())
123
         self.toolbar.addAction(self.load_icon("beer_bottle.svg"), "Bierrr")
129
         self.toolbar.addAction(self.load_icon("beer_bottle.svg"), "Bierrr")
124
         self.addToolBar(self.toolbar)
130
         self.addToolBar(self.toolbar)
125
 
131
 
132
+    def add_person(self) -> None:
133
+        """ Ask for a new Person and register it, then rebuild the central
134
+        widget. """
135
+        name, ok = QInputDialog.getItem(
136
+            self,
137
+            "Persoon toevoegen",
138
+            "Voer de naam van de nieuwe persoon in, of kies uit de lijst.",
139
+            ["Cas", "Frenk"],
140
+            0,
141
+            True,
142
+        )
143
+        if ok and name:
144
+            req = requests.post(urljoin(SERVER_URL, "people"), json={"name": name})
145
+
146
+            self.main_widget = NameButtons()
147
+            self.setCentralWidget(self.main_widget)
148
+
126
     @staticmethod
149
     @staticmethod
127
     def create_spacer() -> QWidget:
150
     def create_spacer() -> QWidget:
128
         """ Return an empty QWidget that automatically expands. """
151
         """ Return an empty QWidget that automatically expands. """
141
 
164
 
142
 
165
 
143
 def main() -> None:
166
 def main() -> None:
144
-    ''' Main entry point of GUI client. '''
167
+    """ Main entry point of GUI client. """
145
     app = QApplication(sys.argv)
168
     app = QApplication(sys.argv)
146
     font = app.font()
169
     font = app.font()
147
     size = font.pointSize()
170
     size = font.pointSize()

+ 1 - 0
piket_server/__init__.py

34
 def get_people():
34
 def get_people():
35
     """ Return a list of currently known people. """
35
     """ Return a list of currently known people. """
36
     people = [p for p in PEOPLE.values()]
36
     people = [p for p in PEOPLE.values()]
37
+    people.sort(key=lambda p: p["name"])
37
     return jsonify(people=people)
38
     return jsonify(people=people)
38
 
39
 
39
 
40