diff --git a/freenit/models/metaclass.py b/freenit/models/metaclass.py deleted file mode 100644 index 7a5e2f8..0000000 --- a/freenit/models/metaclass.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import Optional - -import ormar - - -class AllOptional(ormar.models.metaclass.ModelMetaclass): - def __new__(cls, name, bases, namespaces, **kwargs): - annotations = namespaces.get("__annotations__", {}) - for base in bases: - annotations = {**annotations, **base.__annotations__} - for field in annotations: - if not field.startswith("__"): - f = annotations[field] - annotations[field] = Optional[f] - namespaces["__annotations__"] = annotations - return super().__new__(cls, name, bases, namespaces, **kwargs) diff --git a/freenit/models/ormar/role.py b/freenit/models/ormar/role.py index 7b72111..97547d2 100644 --- a/freenit/models/ormar/role.py +++ b/freenit/models/ormar/role.py @@ -1,4 +1,3 @@ -from ..metaclass import AllOptional from .base import OrmarBaseModel, OrmarRoleMixin, ormar_config @@ -6,5 +5,8 @@ class Role(OrmarBaseModel, OrmarRoleMixin): ormar_config = ormar_config.copy() -class RoleOptional(Role, metaclass=AllOptional): - ormar_config = ormar_config.copy() +class RoleOptional(Role): + pass + +for field_name in RoleOptional.model_fields: + RoleOptional.model_fields[field_name].default = None diff --git a/freenit/models/ormar/theme.py b/freenit/models/ormar/theme.py index 3845c3c..6e6a794 100644 --- a/freenit/models/ormar/theme.py +++ b/freenit/models/ormar/theme.py @@ -1,6 +1,5 @@ import ormar -from ..metaclass import AllOptional from .base import OrmarBaseModel, ormar_config @@ -25,5 +24,8 @@ class Theme(OrmarBaseModel): font_family_mono: str = ormar.Text() -class ThemeOptional(Theme, metaclass=AllOptional): +class ThemeOptional(Theme): pass + +for field_name in ThemeOptional.model_fields: + ThemeOptional.model_fields[field_name].default = None diff --git a/freenit/models/ormar/user.py b/freenit/models/ormar/user.py index b09cf30..0f76583 100644 --- a/freenit/models/ormar/user.py +++ b/freenit/models/ormar/user.py @@ -1,11 +1,11 @@ from __future__ import annotations +from copy import deepcopy import ormar import ormar.exceptions from fastapi import HTTPException from freenit.auth import verify -from freenit.models.metaclass import AllOptional from freenit.models.ormar.base import OrmarBaseModel, OrmarUserMixin, ormar_config from freenit.models.role import Role @@ -31,8 +31,11 @@ class User(BaseUser, OrmarUserMixin): roles = ormar.ManyToMany(Role, unique=True) -class UserOptional(User, metaclass=AllOptional): +class UserOptional(User): pass +for field_name in UserOptional.model_fields: + UserOptional.model_fields[field_name].default = None + UserOptionalPydantic = UserOptional.get_pydantic(exclude={"admin", "active"})