Kaynağa Gözat

Add "people create"

Maarten van den Berg 5 yıl önce
ebeveyn
işleme
d398582d16
3 değiştirilmiş dosya ile 42 ekleme ve 17 silme
  1. 14 0
      piket_client/cli.py
  2. 23 16
      piket_client/model.py
  3. 5 1
      piket_server/routes/people.py

+ 14 - 0
piket_client/cli.py

@@ -70,6 +70,20 @@ def list_people(active: bool) -> None:
70 70
     print(table)
71 71
 
72 72
 
73
+@people.command("create")
74
+@click.option("--display-name", type=click.STRING)
75
+@click.argument("name", type=click.STRING)
76
+def create_person(name: str, display_name: str) -> None:
77
+    """Create a person."""
78
+    person = Person(full_name=name, display_name=display_name).create()
79
+
80
+    if isinstance(person, NetworkError):
81
+        print_error(f"Could not create Person: {person.value}")
82
+        return
83
+
84
+    print_ok(f'Created person "{name}" with ID {person.person_id}.')
85
+
86
+
73 87
 @cli.group()
74 88
 def settlements():
75 89
     pass

+ 23 - 16
piket_client/model.py

@@ -130,29 +130,36 @@ class Person(NamedTuple):
130 130
             )
131 131
             return None
132 132
 
133
-    def create(self) -> Optional[Person]:
133
+    def create(self) -> Union[Person, NetworkError]:
134 134
         """ Create a new Person from the current attributes. As tuples are
135 135
         immutable, a new Person with the correct id is returned. """
136
-        req = requests.post(
137
-            urljoin(SERVER_URL, "people"),
138
-            json={"person": {"name": self.name, "active": True}},
139
-        )
140 136
 
141 137
         try:
142
-            data = req.json()
143
-        except ValueError:
144
-            LOG.error(
145
-                "Did not get JSON on adding Person (%s): %s",
146
-                req.status_code,
147
-                req.content,
138
+            req = requests.post(
139
+                urljoin(SERVER_URL, "people"),
140
+                json={
141
+                    "person": {
142
+                        "full_name": self.full_name,
143
+                        "display_name": self.display_name,
144
+                        "active": True,
145
+                    }
146
+                },
148 147
             )
149
-            return None
148
+            req.raise_for_status()
149
+            data = req.json()
150
+            return Person.from_dict(data["person"])
150 151
 
151
-        if "error" in data or req.status_code != 201:
152
-            LOG.error("Could not create Person (%s): %s", req.status_code, data)
153
-            return None
152
+        except requests.ConnectionError as e:
153
+            LOG.exception(e)
154
+            return NetworkError.ConnectionFailure
154 155
 
155
-        return Person.from_dict(data["person"])
156
+        except requests.HTTPError as e:
157
+            LOG.exception(e)
158
+            return NetworkError.HttpFailure
159
+
160
+        except ValueError as e:
161
+            LOG.exception(e)
162
+            return NetworkError.InvalidData
156 163
 
157 164
     def set_active(self, new_state=True) -> Optional[Person]:
158 165
         req = requests.patch(

+ 5 - 1
piket_server/routes/people.py

@@ -43,7 +43,11 @@ def add_person():
43 43
         return jsonify({"error": "Could not parse JSON."}), 400
44 44
 
45 45
     data = json.get("person") or {}
46
-    person = Person(name=data.get("name"), active=data.get("active", False))
46
+    person = Person(
47
+        full_name=data.get("full_name"),
48
+        active=data.get("active", False),
49
+        display_name=data.get("display_name", None),
50
+    )
47 51
 
48 52
     try:
49 53
         db.session.add(person)