Files
prostomarkeloff b5b0bbbb01 Tortoise ORM support (#59)
* add tortoise to dependencies

* add tortoise as optional dependency in pyproject.toml

* add tortoise support (tests needed)

* Add tortoise support (also defined orm_mode in pydantic model

* tests for tortoise support

* format by black

* docs for tortoise

* delete type annotations

* delete underscore

* do it in 1 line

* add 1 line before yield

* fix in docs

* fix bug and add annotation for test

* Tweak documentation and fix Tortoise error about id update

* Improve Tortoise coverage by using get instead of filter

* Fix Pipfile.lock
2019-12-03 21:24:27 +01:00

62 lines
1.4 KiB
Python

import uuid
from typing import Optional, Type
import pydantic
from pydantic import BaseModel, EmailStr
class BaseUser(BaseModel):
"""Base User model."""
id: Optional[str] = None
email: Optional[EmailStr] = None
is_active: Optional[bool] = True
is_superuser: Optional[bool] = False
@pydantic.validator("id", pre=True, always=True)
def default_id(cls, v):
return v or str(uuid.uuid4())
def create_update_dict(self):
return self.dict(
exclude_unset=True, exclude={"id", "is_superuser", "is_active"}
)
def create_update_dict_superuser(self):
return self.dict(exclude_unset=True, exclude={"id"})
class Config:
orm_mode = True
class BaseUserCreate(BaseUser):
email: EmailStr
password: str
class BaseUserUpdate(BaseUser):
password: Optional[str]
class BaseUserDB(BaseUser):
hashed_password: str
class Models:
"""Generate models inheriting from the custom User model."""
def __init__(self, user_model: Type[BaseUser]):
class UserCreate(user_model, BaseUserCreate): # type: ignore
pass
class UserUpdate(user_model, BaseUserUpdate): # type: ignore
pass
class UserDB(user_model, BaseUserDB): # type: ignore
pass
self.User = user_model
self.UserCreate = UserCreate
self.UserUpdate = UserUpdate
self.UserDB = UserDB