Browse Source

Add People API to server, rework client to use it

Maarten van den Berg 6 years ago
parent
commit
c97fb25d0d
2 changed files with 29 additions and 19 deletions
  1. 22 15
      piket_client/gui.py
  2. 7 4
      piket_server/__init__.py

+ 22 - 15
piket_client/gui.py

2
 Provides the graphical front-end for Piket.
2
 Provides the graphical front-end for Piket.
3
 """
3
 """
4
 import sys
4
 import sys
5
+from urllib.parse import urljoin
5
 
6
 
6
 from PySide2.QtWidgets import QApplication, QPushButton, QGridLayout, QWidget
7
 from PySide2.QtWidgets import QApplication, QPushButton, QGridLayout, QWidget
7
 from PySide2.QtGui import QFont
8
 from PySide2.QtGui import QFont
15
     PLOP_WAVE.play()
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
 class NameButton(QPushButton):
27
 class NameButton(QPushButton):
29
     """ Wraps a QPushButton to provide a counter. """
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
         super().__init__(self.current_label, *args, **kwargs)
35
         super().__init__(self.current_label, *args, **kwargs)
36
 
36
 
41
         return f"{self.name} ({self.count})"
41
         return f"{self.name} ({self.count})"
42
 
42
 
43
     def plop(self) -> None:
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
 class NameButtons(QWidget):
57
 class NameButtons(QWidget):
56
     def init_ui(self) -> None:
64
     def init_ui(self) -> None:
57
         self.layout = QGridLayout()
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
             self.layout.addWidget(button, index // 2, index % 2)
69
             self.layout.addWidget(button, index // 2, index % 2)
63
 
70
 
64
         self.setLayout(self.layout)
71
         self.setLayout(self.layout)

+ 7 - 4
piket_server/__init__.py

23
     "Foek Lammenschaap",
23
     "Foek Lammenschaap",
24
 ]
24
 ]
25
 
25
 
26
-PEOPLE = {index: {"name": name, "count": 0} for index, name in enumerate(PRESET_NAMES)}
26
+PEOPLE = {
27
+    index: {"id": index, "name": name, "count": 0}
28
+    for index, name in enumerate(PRESET_NAMES)
29
+}
27
 NEXT_ID = len(PEOPLE)
30
 NEXT_ID = len(PEOPLE)
28
 
31
 
29
 
32
 
30
 @app.route("/people", methods=["GET"])
33
 @app.route("/people", methods=["GET"])
31
 def get_people():
34
 def get_people():
32
     """ Return a list of currently known people. """
35
     """ Return a list of currently known people. """
33
-    return jsonify(people=PEOPLE)
36
+    people = [p for p in PEOPLE.values()]
37
+    return jsonify(people=people)
34
 
38
 
35
 
39
 
36
 @app.route("/people/<int:person_id>", methods=["GET"])
40
 @app.route("/people/<int:person_id>", methods=["GET"])
62
 
66
 
63
     if not name:
67
     if not name:
64
         abort(400)
68
         abort(400)
65
-    person = {"name": name, "count": 0}
69
+    person = {"id": NEXT_ID, "name": name, "count": 0}
66
 
70
 
67
     PEOPLE[NEXT_ID] = person
71
     PEOPLE[NEXT_ID] = person
68
 
72
 
69
-    person["id"] = NEXT_ID
70
     NEXT_ID += 1
73
     NEXT_ID += 1
71
 
74
 
72
     return jsonify(person=person)
75
     return jsonify(person=person)