Skip to content

Commit 3a3df58

Browse files
committed
style(base): extract create method to BaseModel
1 parent dc7d28f commit 3a3df58

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

authority/authorize.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ async def authorize(user: User, device: str = "pc", auto_delete=True) -> str:
2424
return token
2525

2626
@staticmethod
27-
async def revoke(token: str):
27+
async def revoke(token: str) -> bool:
2828
try:
2929
payload = Token.decode(token)
3030
except: # noqa
3131
pass
3232
else:
3333
await redis.execute("DEL", f"{payload.id}-{token}")
34+
return True
3435

3536
@staticmethod
3637
async def revoke_all(user: User):

authority/credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AuthUser(BaseModel):
1818

1919
@property
2020
def is_authenticated(self) -> bool:
21-
return True
21+
return self.user_id is not None
2222

2323
@property
2424
def display_id(self) -> int:

database.sqlite3

0 Bytes
Binary file not shown.

orm/base.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
from typing import TypeVar
2+
13
from sqlalchemy import create_engine
24
from sqlalchemy.ext.declarative import declarative_base
35
from sqlalchemy.orm import sessionmaker
46

57
from settings import SQLITE_URI
68

9+
engine = create_engine(f'sqlite:///{SQLITE_URI}', convert_unicode=True, echo=False)
10+
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
11+
session = Session()
12+
13+
T = TypeVar("T")
14+
715

816
class Base(declarative_base()):
917
__abstract__ = True
@@ -12,7 +20,13 @@ def dict(self):
1220
column_names = self.__table__.columns.keys()
1321
return {c: getattr(self, c) for c in column_names}
1422

23+
@classmethod
24+
def create(cls, **kwargs):
25+
instance = cls(**kwargs) # noqa
26+
return instance.save()
1527

16-
engine = create_engine(f'sqlite:///{SQLITE_URI}', convert_unicode=True, echo=False)
17-
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
18-
session = Session()
28+
def save(self) -> T:
29+
"""Save the record."""
30+
session.add(self)
31+
session.commit()
32+
return self

resolvers/login.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
async def register(*_, create: dict = None) -> User:
1616
create_user = CreateUser(**create)
1717
create_user.password = Password.encode(create_user.password)
18-
user = User(**create_user.dict())
19-
session.add(user)
20-
session.commit()
21-
return user
18+
return User.create(**create_user.dict())
2219

2320

2421
@query.field("login")
@@ -36,8 +33,7 @@ async def login(_, info: GraphQLResolveInfo, id: int, password: str) -> str:
3633
@login_required
3734
async def logout(_, info: GraphQLResolveInfo, id: int) -> bool:
3835
token = info.context["request"].headers[JWT_AUTH_HEADER]
39-
await Authorize.revoke(token)
40-
return True
36+
return await Authorize.revoke(token)
4137

4238

4339
@query.field("user")

0 commit comments

Comments
 (0)