From cab9961eceafd8462665f2fb7e0a7c2db7c8b2ea Mon Sep 17 00:00:00 2001 From: cklein12 <48038030+cklein12@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:22:13 -0400 Subject: [PATCH] Create bchuioew --- bchuioew | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 bchuioew diff --git a/bchuioew b/bchuioew new file mode 100644 index 0000000000..c663ddc39d --- /dev/null +++ b/bchuioew @@ -0,0 +1,41 @@ +from hashlib import md5 +from typing import NamedTuple, Optional + +from aiopg import Connection + + +class User(NamedTuple): + id: int + first_name: str + middle_name: Optional[str] + last_name: str + username: str + pwd_hash: str + is_admin: bool + + @classmethod + def from_raw(cls, raw: tuple): + return cls(*raw) if raw else None + + @staticmethod + async def get(conn: Connection, id_: int): + async with conn.cursor() as cur: + await cur.execute( + 'SELECT id, first_name, middle_name, last_name, ' + 'username, pwd_hash, is_admin FROM users WHERE id = %s', + (id_,), + ) + return User.from_raw(await cur.fetchone()) + + @staticmethod + async def get_by_username(conn: Connection, username: str): + async with conn.cursor() as cur: + await cur.execute( + 'SELECT id, first_name, middle_name, last_name, ' + 'username, pwd_hash, is_admin FROM users WHERE username = %s', + (username,), + ) + return User.from_raw(await cur.fetchone()) + + def check_password(self, password: str): + return self.pwd_hash == md5(password.encode('utf-8')).hexdigest()