Browse Source

Add "people list"

Maarten van den Berg 5 years ago
parent
commit
786fa37b68
2 changed files with 35 additions and 14 deletions
  1. 23 0
      piket_client/cli.py
  2. 12 14
      piket_client/model.py

+ 23 - 0
piket_client/cli.py

@@ -3,6 +3,7 @@ from piket_client.model import (
3 3
     ServerStatus,
4 4
     NetworkError,
5 5
     Consumption,
6
+    Person,
6 7
     Settlement,
7 8
     ConsumptionType,
8 9
 )
@@ -47,6 +48,28 @@ def people():
47 48
     pass
48 49
 
49 50
 
51
+@people.command("list")
52
+@click.option("--active/--inactive", default=None)
53
+def list_people(active: bool) -> None:
54
+    people = Person.get_all(active=active)
55
+
56
+    if isinstance(people, NetworkError):
57
+        print_error(f"Could not get people: {people.value}")
58
+        return
59
+
60
+    table = PrettyTable()
61
+    table.field_names = ["ID", "Full name", "Display name", "Active"]
62
+    table.align["ID"] = "r"
63
+    table.align["Full name"] = "l"
64
+    table.align["Display name"] = "l"
65
+    table.sortby = "Full name"
66
+
67
+    for p in people:
68
+        table.add_row([p.person_id, p.full_name, p.display_name, p.active])
69
+
70
+    print(table)
71
+
72
+
50 73
 @cli.group()
51 74
 def settlements():
52 75
     pass

+ 12 - 14
piket_client/model.py

@@ -201,29 +201,28 @@ class Person(NamedTuple):
201 201
             return None
202 202
 
203 203
     @classmethod
204
-    def get_all(cls, active=None) -> Optional[List[Person]]:
204
+    def get_all(cls, active=None) -> Union[List[Person], NetworkError]:
205 205
         """ Get all active People. """
206 206
         params = {}
207 207
         if active is not None:
208 208
             params["active"] = int(active)
209 209
 
210
-        req = requests.get(urljoin(SERVER_URL, "/people"), params=params)
211
-
212 210
         try:
211
+            req = requests.get(urljoin(SERVER_URL, "/people"), params=params)
212
+            req.raise_for_status()
213 213
             data = req.json()
214
+            return [Person.from_dict(item) for item in data["people"]]
214 215
 
215
-            if "error" in data:
216
-                LOG.warning("Could not get people (%s): %s", req.status_code, data)
216
+        except requests.ConnectionError as e:
217
+            LOG.exception(e)
218
+            return NetworkError.ConnectionFailure
217 219
 
218
-            return [Person.from_dict(item) for item in data["people"]]
220
+        except requests.HTTPError as e:
221
+            LOG.exception(e)
222
+            return NetworkError.HttpFailure
219 223
 
220
-        except ValueError:
221
-            LOG.error(
222
-                "Did not get JSON from server on getting People (%s): %s",
223
-                req.status_code,
224
-                req.content,
225
-            )
226
-            return None
224
+        except ValueError as e:
225
+            return NetworkError.InvalidData
227 226
 
228 227
     @classmethod
229 228
     def from_dict(cls, data: dict) -> "Person":
@@ -355,7 +354,6 @@ class ConsumptionType(NamedTuple):
355 354
             LOG.exception(e)
356 355
             return NetworkError.InvalidData
357 356
 
358
-
359 357
     @classmethod
360 358
     def get(cls, consumption_type_id: int) -> Union[ConsumptionType, NetworkError]:
361 359
         """ Retrieve a ConsumptionType by id. """