Browse Source

Add renaming to piket-cli

Maarten van den Berg 4 years ago
parent
commit
0965e3c94d
3 changed files with 72 additions and 3 deletions
  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
+from typing import Optional
2
+
1
 import click
3
 import click
4
+from prettytable import PrettyTable
5
+
2
 from piket_client.model import (
6
 from piket_client.model import (
3
     AardbeiActivity,
7
     AardbeiActivity,
4
     ServerStatus,
8
     ServerStatus,
9
     Settlement,
13
     Settlement,
10
     ConsumptionType,
14
     ConsumptionType,
11
 )
15
 )
12
-from prettytable import PrettyTable
13
 
16
 
14
 
17
 
15
 @click.group()
18
 @click.group()
86
     print_ok(f'Created person "{name}" with ID {person.person_id}.')
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
 @cli.group()
113
 @cli.group()
90
 def settlements():
114
 def settlements():
91
     pass
115
     pass

+ 35 - 1
piket_client/model.py

161
             LOG.exception(e)
161
             LOG.exception(e)
162
             return NetworkError.InvalidData
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
     def set_active(self, new_state=True) -> Optional[Person]:
196
     def set_active(self, new_state=True) -> Optional[Person]:
165
         req = requests.patch(
197
         req = requests.patch(
166
             urljoin(SERVER_URL, f"people/{self.person_id}"),
198
             urljoin(SERVER_URL, f"people/{self.person_id}"),
622
         return cls(**data)
654
         return cls(**data)
623
 
655
 
624
     @classmethod
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
         try:
660
         try:
627
             req = requests.post(
661
             req = requests.post(
628
                 urljoin(SERVER_URL, "/aardbei/diff_people"),
662
                 urljoin(SERVER_URL, "/aardbei/diff_people"),

+ 12 - 1
piket_server/routes/people.py

63
     person = Person.query.get_or_404(person_id)
63
     person = Person.query.get_or_404(person_id)
64
 
64
 
65
     data = request.json["person"]
65
     data = request.json["person"]
66
+    changed = False
66
 
67
 
67
     if "active" in data:
68
     if "active" in data:
68
         person.active = data["active"]
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
         db.session.add(person)
81
         db.session.add(person)
71
         db.session.commit()
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
 @app.route("/people/<int:person_id>/add_consumption", methods=["POST"])
87
 @app.route("/people/<int:person_id>/add_consumption", methods=["POST"])