-
Notifications
You must be signed in to change notification settings - Fork 518
/
user.py
41 lines (34 loc) · 1.21 KB
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()