Browse Source

Add "people create"

Maarten van den Berg 5 years ago
parent
commit
d398582d16
3 changed files with 42 additions and 17 deletions
  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
     print(table)
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
 @cli.group()
87
 @cli.group()
74
 def settlements():
88
 def settlements():
75
     pass
89
     pass

+ 23 - 16
piket_client/model.py

130
             )
130
             )
131
             return None
131
             return None
132
 
132
 
133
-    def create(self) -> Optional[Person]:
133
+    def create(self) -> Union[Person, NetworkError]:
134
         """ Create a new Person from the current attributes. As tuples are
134
         """ Create a new Person from the current attributes. As tuples are
135
         immutable, a new Person with the correct id is returned. """
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
         try:
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
     def set_active(self, new_state=True) -> Optional[Person]:
164
     def set_active(self, new_state=True) -> Optional[Person]:
158
         req = requests.patch(
165
         req = requests.patch(

+ 5 - 1
piket_server/routes/people.py

43
         return jsonify({"error": "Could not parse JSON."}), 400
43
         return jsonify({"error": "Could not parse JSON."}), 400
44
 
44
 
45
     data = json.get("person") or {}
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
     try:
52
     try:
49
         db.session.add(person)
53
         db.session.add(person)