Browse Source

Add new name fields to client

Maarten van den Berg 5 years ago
parent
commit
9405cec96a
1 changed files with 11 additions and 5 deletions
  1. 11 5
      piket_client/model.py

+ 11 - 5
piket_client/model.py

3
 """
3
 """
4
 import datetime
4
 import datetime
5
 import logging
5
 import logging
6
-from typing import NamedTuple, Sequence
6
+from typing import NamedTuple, Sequence, Tuple, Any, Optional
7
 from urllib.parse import urljoin
7
 from urllib.parse import urljoin
8
 
8
 
9
 import requests
9
 import requests
19
     """ Provides helper classes to check whether the server is up. """
19
     """ Provides helper classes to check whether the server is up. """
20
 
20
 
21
     @classmethod
21
     @classmethod
22
-    def is_server_running(cls) -> bool:
22
+    def is_server_running(cls) -> Tuple[bool, Any]:
23
         try:
23
         try:
24
             req = requests.get(urljoin(SERVER_URL, "ping"))
24
             req = requests.get(urljoin(SERVER_URL, "ping"))
25
 
25
 
50
 class Person(NamedTuple):
50
 class Person(NamedTuple):
51
     """ Represents a Person, as retrieved from the database. """
51
     """ Represents a Person, as retrieved from the database. """
52
 
52
 
53
-    name: str
53
+    full_name: str
54
+    display_name: Optional[str]
54
     active: bool = True
55
     active: bool = True
55
-    person_id: int = None
56
+    person_id: Optional[int] = None
56
     consumptions: dict = {}
57
     consumptions: dict = {}
57
 
58
 
59
+    @property
60
+    def name(self) -> str:
61
+        return self.display_name or self.full_name
62
+
58
     def add_consumption(self, type_id: str) -> bool:
63
     def add_consumption(self, type_id: str) -> bool:
59
         """ Register a consumption for this Person. """
64
         """ Register a consumption for this Person. """
60
         req = requests.post(
65
         req = requests.post(
182
     def from_dict(cls, data: dict) -> "Person":
187
     def from_dict(cls, data: dict) -> "Person":
183
         """ Reconstruct a Person object from a dict. """
188
         """ Reconstruct a Person object from a dict. """
184
         return Person(
189
         return Person(
185
-            name=data["name"],
190
+            full_name=data["full_name"],
191
+            display_name=data["display_name"],
186
             active=data["active"],
192
             active=data["active"],
187
             person_id=data["person_id"],
193
             person_id=data["person_id"],
188
             consumptions=data["consumptions"],
194
             consumptions=data["consumptions"],