Преглед на файлове

Add People API to server, rework client to use it

Maarten van den Berg преди 6 години
родител
ревизия
c97fb25d0d
променени са 2 файла, в които са добавени 29 реда и са изтрити 19 реда
  1. 22 15
      piket_client/gui.py
  2. 7 4
      piket_server/__init__.py

+ 22 - 15
piket_client/gui.py

@@ -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)

+ 7 - 4
piket_server/__init__.py

@@ -23,14 +23,18 @@ PRESET_NAMES = [
23 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 30
 NEXT_ID = len(PEOPLE)
28 31
 
29 32
 
30 33
 @app.route("/people", methods=["GET"])
31 34
 def get_people():
32 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 40
 @app.route("/people/<int:person_id>", methods=["GET"])
@@ -62,11 +66,10 @@ def add_person():
62 66
 
63 67
     if not name:
64 68
         abort(400)
65
-    person = {"name": name, "count": 0}
69
+    person = {"id": NEXT_ID, "name": name, "count": 0}
66 70
 
67 71
     PEOPLE[NEXT_ID] = person
68 72
 
69
-    person["id"] = NEXT_ID
70 73
     NEXT_ID += 1
71 74
 
72 75
     return jsonify(person=person)