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
     ServerStatus,
3
     ServerStatus,
4
     NetworkError,
4
     NetworkError,
5
     Consumption,
5
     Consumption,
6
+    Person,
6
     Settlement,
7
     Settlement,
7
     ConsumptionType,
8
     ConsumptionType,
8
 )
9
 )
47
     pass
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
 @cli.group()
73
 @cli.group()
51
 def settlements():
74
 def settlements():
52
     pass
75
     pass

+ 12 - 14
piket_client/model.py

201
             return None
201
             return None
202
 
202
 
203
     @classmethod
203
     @classmethod
204
-    def get_all(cls, active=None) -> Optional[List[Person]]:
204
+    def get_all(cls, active=None) -> Union[List[Person], NetworkError]:
205
         """ Get all active People. """
205
         """ Get all active People. """
206
         params = {}
206
         params = {}
207
         if active is not None:
207
         if active is not None:
208
             params["active"] = int(active)
208
             params["active"] = int(active)
209
 
209
 
210
-        req = requests.get(urljoin(SERVER_URL, "/people"), params=params)
211
-
212
         try:
210
         try:
211
+            req = requests.get(urljoin(SERVER_URL, "/people"), params=params)
212
+            req.raise_for_status()
213
             data = req.json()
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
     @classmethod
227
     @classmethod
229
     def from_dict(cls, data: dict) -> "Person":
228
     def from_dict(cls, data: dict) -> "Person":
355
             LOG.exception(e)
354
             LOG.exception(e)
356
             return NetworkError.InvalidData
355
             return NetworkError.InvalidData
357
 
356
 
358
-
359
     @classmethod
357
     @classmethod
360
     def get(cls, consumption_type_id: int) -> Union[ConsumptionType, NetworkError]:
358
     def get(cls, consumption_type_id: int) -> Union[ConsumptionType, NetworkError]:
361
         """ Retrieve a ConsumptionType by id. """
359
         """ Retrieve a ConsumptionType by id. """