Skip to content

Commit

Permalink
Work around optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Apr 3, 2024
1 parent 63d8fd5 commit 632d442
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
16 changes: 0 additions & 16 deletions freenit/models/metaclass.py

This file was deleted.

8 changes: 5 additions & 3 deletions freenit/models/ormar/role.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from ..metaclass import AllOptional
from .base import OrmarBaseModel, OrmarRoleMixin, ormar_config


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
6 changes: 4 additions & 2 deletions freenit/models/ormar/theme.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ormar

from ..metaclass import AllOptional
from .base import OrmarBaseModel, ormar_config


Expand All @@ -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
7 changes: 5 additions & 2 deletions freenit/models/ormar/user.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"})

0 comments on commit 632d442

Please sign in to comment.