♻️ Add type annotations
This commit is contained in:
parent
7ca64e8aef
commit
2870d55926
2 changed files with 13 additions and 14 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
|
from functools import wraps
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from ohmyapi.db import Model, field
|
from ohmyapi.router import HTTPException
|
||||||
|
from ohmyapi.db import Model, field, pre_save, pre_delete
|
||||||
from passlib.context import CryptContext
|
from passlib.context import CryptContext
|
||||||
from tortoise.contrib.pydantic import pydantic_queryset_creator
|
from tortoise.contrib.pydantic import pydantic_queryset_creator
|
||||||
|
|
||||||
|
|
@ -7,24 +9,22 @@ pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
||||||
|
|
||||||
|
|
||||||
class Group(Model):
|
class Group(Model):
|
||||||
id = field.data.UUIDField(pk=True)
|
id: str = field.data.UUIDField(pk=True)
|
||||||
name = field.CharField(max_length=42, index=True)
|
name: str = field.CharField(max_length=42, index=True)
|
||||||
|
|
||||||
|
|
||||||
class User(Model):
|
class User(Model):
|
||||||
id = field.data.UUIDField(pk=True)
|
id: str = field.data.UUIDField(pk=True)
|
||||||
email = field.CharField(max_length=255, unique=True, index=True)
|
email: str = field.CharField(max_length=255, unique=True, index=True)
|
||||||
username = field.CharField(max_length=150, unique=True)
|
username: str = field.CharField(max_length=150, unique=True)
|
||||||
password_hash = field.CharField(max_length=128)
|
password_hash: str = field.CharField(max_length=128)
|
||||||
is_admin = field.BooleanField(default=False)
|
is_admin: bool = field.BooleanField(default=False)
|
||||||
is_staff = field.BooleanField(default=False)
|
is_staff: bool = field.BooleanField(default=False)
|
||||||
groups: Optional[List[Group]] = field.ManyToManyField("ohmyapi_auth.Group", related_name="users")
|
groups: field.ManyToManyRelation[Group] = field.ManyToManyField("ohmyapi_auth.Group", related_name="users", through='user_groups')
|
||||||
|
|
||||||
|
|
||||||
class Schema:
|
class Schema:
|
||||||
exclude = 'password_hash',
|
exclude = 'password_hash',
|
||||||
|
|
||||||
|
|
||||||
def set_password(self, raw_password: str) -> None:
|
def set_password(self, raw_password: str) -> None:
|
||||||
"""Hash and store the password."""
|
"""Hash and store the password."""
|
||||||
self.password_hash = pwd_context.hash(raw_password)
|
self.password_hash = pwd_context.hash(raw_password)
|
||||||
|
|
@ -40,4 +40,3 @@ class User(Model):
|
||||||
if user and user.verify_password(password):
|
if user and user.verify_password(password):
|
||||||
return user
|
return user
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,5 +160,5 @@ async def introspect(token: Dict = Depends(get_token)):
|
||||||
@router.get("/me")
|
@router.get("/me")
|
||||||
async def me(user: User = Depends(get_current_user)):
|
async def me(user: User = Depends(get_current_user)):
|
||||||
"""Return the currently authenticated user."""
|
"""Return the currently authenticated user."""
|
||||||
return user
|
return User.Schema.one.from_orm(user)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue