From a6f1738b73b7715f21a25f03ec9a9b193559718e Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sat, 16 Mar 2024 06:11:47 -0400 Subject: [PATCH] fix user updating --- relay/database/connection.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/relay/database/connection.py b/relay/database/connection.py index 67df706..4e575f6 100644 --- a/relay/database/connection.py +++ b/relay/database/connection.py @@ -192,26 +192,29 @@ class Connection(SqlConnection): def put_user(self, username: str, password: str | None, handle: str | None = None) -> Row: if self.get_user(username): - data: dict[str, str | datetime | None] = { - 'username': username - } + data: dict[str, str] = {} if password: - data['password'] = password + data['hash'] = self.hasher.hash(password) if handle: - data['handler'] = handle + data['handle'] = handle - else: - if password is None: - raise ValueError('Password cannot be empty') + stmt = Update("users", data) + stmt.set_where("username", username) - data = { - 'username': username, - 'hash': self.hasher.hash(password), - 'handle': handle, - 'created': datetime.now(tz = timezone.utc) - } + with self.query(stmt) as cur: + return cur.one() + + if password is None: + raise ValueError('Password cannot be empty') + + data = { + 'username': username, + 'hash': self.hasher.hash(password), + 'handle': handle, + 'created': datetime.now(tz = timezone.utc) + } with self.run('put-user', data) as cur: return cur.one() # type: ignore