♻️ 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 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 tortoise.contrib.pydantic import pydantic_queryset_creator
|
||||
|
||||
|
|
@ -7,24 +9,22 @@ pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
|||
|
||||
|
||||
class Group(Model):
|
||||
id = field.data.UUIDField(pk=True)
|
||||
name = field.CharField(max_length=42, index=True)
|
||||
id: str = field.data.UUIDField(pk=True)
|
||||
name: str = field.CharField(max_length=42, index=True)
|
||||
|
||||
|
||||
class User(Model):
|
||||
id = field.data.UUIDField(pk=True)
|
||||
email = field.CharField(max_length=255, unique=True, index=True)
|
||||
username = field.CharField(max_length=150, unique=True)
|
||||
password_hash = field.CharField(max_length=128)
|
||||
is_admin = field.BooleanField(default=False)
|
||||
is_staff = field.BooleanField(default=False)
|
||||
groups: Optional[List[Group]] = field.ManyToManyField("ohmyapi_auth.Group", related_name="users")
|
||||
|
||||
id: str = field.data.UUIDField(pk=True)
|
||||
email: str = field.CharField(max_length=255, unique=True, index=True)
|
||||
username: str = field.CharField(max_length=150, unique=True)
|
||||
password_hash: str = field.CharField(max_length=128)
|
||||
is_admin: bool = field.BooleanField(default=False)
|
||||
is_staff: bool = field.BooleanField(default=False)
|
||||
groups: field.ManyToManyRelation[Group] = field.ManyToManyField("ohmyapi_auth.Group", related_name="users", through='user_groups')
|
||||
|
||||
class Schema:
|
||||
exclude = 'password_hash',
|
||||
|
||||
|
||||
def set_password(self, raw_password: str) -> None:
|
||||
"""Hash and store the password."""
|
||||
self.password_hash = pwd_context.hash(raw_password)
|
||||
|
|
@ -40,4 +40,3 @@ class User(Model):
|
|||
if user and user.verify_password(password):
|
||||
return user
|
||||
return None
|
||||
|
||||
|
|
|
|||
|
|
@ -160,5 +160,5 @@ async def introspect(token: Dict = Depends(get_token)):
|
|||
@router.get("/me")
|
||||
async def me(user: User = Depends(get_current_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