Просмотр исходного кода

Add logic for adding buttons for people

Maarten van den Berg лет назад: 6
Родитель
Сommit
45471f1598
3 измененных файлов с 40 добавлено и 15 удалено
  1. 2 1
      .pylintrc
  2. 37 14
      piket_client/gui.py
  3. 1 0
      piket_server/__init__.py

+ 2 - 1
.pylintrc

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

+ 37 - 14
piket_client/gui.py

@@ -8,15 +8,17 @@ from urllib.parse import urljoin
8 8
 # pylint: disable=E0611
9 9
 from PySide2.QtWidgets import (
10 10
     QApplication,
11
-    QPushButton,
12 11
     QGridLayout,
13
-    QWidget,
12
+    QInputDialog,
14 13
     QMainWindow,
14
+    QPushButton,
15 15
     QSizePolicy,
16 16
     QToolBar,
17
+    QWidget,
17 18
 )
18 19
 from PySide2.QtGui import QIcon
19 20
 from PySide2.QtCore import QSize
21
+
20 22
 # pylint: enable=E0611
21 23
 import requests
22 24
 
@@ -32,7 +34,7 @@ SERVER_URL = "http://127.0.0.1:5000"
32 34
 
33 35
 
34 36
 def get_people() -> [dict]:
35
-    ''' Request list of active people from the server. '''
37
+    """ Request list of active people from the server. """
36 38
     request = requests.get(urljoin(SERVER_URL, "/people"))
37 39
     return request.json()["people"]
38 40
 
@@ -52,11 +54,11 @@ class NameButton(QPushButton):
52 54
 
53 55
     @property
54 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 58
         return f"{self.name} ({self.count})"
57 59
 
58 60
     def plop(self) -> None:
59
-        ''' Process a click on this button. '''
61
+        """ Process a click on this button. """
60 62
         req = requests.post(
61 63
             urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption")
62 64
         )
@@ -71,8 +73,9 @@ class NameButton(QPushButton):
71 73
 
72 74
 
73 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 79
     def __init__(self) -> None:
77 80
         super().__init__()
78 81
 
@@ -80,8 +83,8 @@ class NameButtons(QWidget):
80 83
         self.init_ui()
81 84
 
82 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 88
         self.layout = QGridLayout()
86 89
 
87 90
         for index, person in enumerate(get_people()):
@@ -92,8 +95,9 @@ class NameButtons(QWidget):
92 95
 
93 96
 
94 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 101
     def __init__(self) -> None:
98 102
         super().__init__()
99 103
 
@@ -102,7 +106,7 @@ class PiketMainWindow(QMainWindow):
102 106
         self.init_ui()
103 107
 
104 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 110
         self.main_widget = NameButtons()
107 111
         self.setCentralWidget(self.main_widget)
108 112
 
@@ -114,7 +118,9 @@ class PiketMainWindow(QMainWindow):
114 118
         self.toolbar.setIconSize(QSize(icon_size, icon_size))
115 119
 
116 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 124
         self.toolbar.addAction(self.load_icon("undo.svg"), "Heydrich")
119 125
 
120 126
         self.toolbar.addWidget(self.create_spacer())
@@ -123,6 +129,23 @@ class PiketMainWindow(QMainWindow):
123 129
         self.toolbar.addAction(self.load_icon("beer_bottle.svg"), "Bierrr")
124 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 149
     @staticmethod
127 150
     def create_spacer() -> QWidget:
128 151
         """ Return an empty QWidget that automatically expands. """
@@ -141,7 +164,7 @@ class PiketMainWindow(QMainWindow):
141 164
 
142 165
 
143 166
 def main() -> None:
144
-    ''' Main entry point of GUI client. '''
167
+    """ Main entry point of GUI client. """
145 168
     app = QApplication(sys.argv)
146 169
     font = app.font()
147 170
     size = font.pointSize()

+ 1 - 0
piket_server/__init__.py

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