ソースを参照

Add renaming to piket-cli

Maarten van den Berg 4 年 前
コミット
0965e3c94d
共有3 個のファイルを変更した72 個の追加3 個の削除を含む
  1. 25 1
      piket_client/cli.py
  2. 35 1
      piket_client/model.py
  3. 12 1
      piket_server/routes/people.py

+ 25 - 1
piket_client/cli.py

@@ -1,4 +1,8 @@
1
+from typing import Optional
2
+
1 3
 import click
4
+from prettytable import PrettyTable
5
+
2 6
 from piket_client.model import (
3 7
     AardbeiActivity,
4 8
     ServerStatus,
@@ -9,7 +13,6 @@ from piket_client.model import (
9 13
     Settlement,
10 14
     ConsumptionType,
11 15
 )
12
-from prettytable import PrettyTable
13 16
 
14 17
 
15 18
 @click.group()
@@ -86,6 +89,27 @@ def create_person(name: str, display_name: str) -> None:
86 89
     print_ok(f'Created person "{name}" with ID {person.person_id}.')
87 90
 
88 91
 
92
+@people.command("rename")
93
+@click.argument("person-id", type=click.INT)
94
+@click.option("--new-full-name", type=click.STRING)
95
+@click.option("--new-display-name", type=click.STRING)
96
+def rename_person(
97
+    person_id: int, new_full_name: Optional[str], new_display_name: Optional[str],
98
+) -> None:
99
+
100
+    person = Person.get(person_id)
101
+
102
+    if person is None:
103
+        raise click.UsageError(f"Cannot find Person {person_id}!")
104
+
105
+    if new_full_name is None and new_display_name is None:
106
+        raise click.UsageError("No new full name or display name specified!")
107
+
108
+    new_person = person.rename(
109
+        new_full_name=new_full_name, new_display_name=new_display_name
110
+    )
111
+
112
+
89 113
 @cli.group()
90 114
 def settlements():
91 115
     pass

+ 35 - 1
piket_client/model.py

@@ -161,6 +161,38 @@ class Person(NamedTuple):
161 161
             LOG.exception(e)
162 162
             return NetworkError.InvalidData
163 163
 
164
+    def rename(
165
+        self, new_full_name: Optional[str], new_display_name: Optional[str]
166
+    ) -> Optional[Person]:
167
+        person_payload: Dict[str, str] = {}
168
+
169
+        if new_full_name is not None:
170
+            person_payload["full_name"] = new_full_name
171
+
172
+        if new_display_name is not None:
173
+            person_payload["display_name"] = new_display_name
174
+
175
+        req = requests.patch(
176
+            urljoin(SERVER_URL, f"people/{self.person_id}"),
177
+            json={"person": person_payload},
178
+        )
179
+
180
+        try:
181
+            data = req.json()
182
+        except ValueError:
183
+            LOG.error(
184
+                "Did not get JSON on updating Person (%s): %s",
185
+                req.status_code,
186
+                req.content,
187
+            )
188
+            return None
189
+
190
+        if "error" in data or req.status_code != 200:
191
+            LOG.error("Could not update Person (%s): %s", req.status_code, data)
192
+            return None
193
+
194
+        return Person.from_dict(data["person"])
195
+
164 196
     def set_active(self, new_state=True) -> Optional[Person]:
165 197
         req = requests.patch(
166 198
             urljoin(SERVER_URL, f"people/{self.person_id}"),
@@ -622,7 +654,9 @@ class AardbeiPeopleDiff:
622 654
         return cls(**data)
623 655
 
624 656
     @classmethod
625
-    def get_diff(cls, token: str, endpoint: str) -> Union[AardbeiPeopleDiff, NetworkError]:
657
+    def get_diff(
658
+        cls, token: str, endpoint: str
659
+    ) -> Union[AardbeiPeopleDiff, NetworkError]:
626 660
         try:
627 661
             req = requests.post(
628 662
                 urljoin(SERVER_URL, "/aardbei/diff_people"),

+ 12 - 1
piket_server/routes/people.py

@@ -63,14 +63,25 @@ def update_person(person_id: int):
63 63
     person = Person.query.get_or_404(person_id)
64 64
 
65 65
     data = request.json["person"]
66
+    changed = False
66 67
 
67 68
     if "active" in data:
68 69
         person.active = data["active"]
70
+        changed = True
69 71
 
72
+    if "full_name" in data:
73
+        person.full_name = data["full_name"]
74
+        changed = True
75
+
76
+    if "display_name" in data:
77
+        person.display_name = data["display_name"]
78
+        changed = True
79
+
80
+    if changed:
70 81
         db.session.add(person)
71 82
         db.session.commit()
72 83
 
73
-        return jsonify(person=person.as_dict)
84
+    return jsonify(person=person.as_dict)
74 85
 
75 86
 
76 87
 @app.route("/people/<int:person_id>/add_consumption", methods=["POST"])