Skip to content

Commit

Permalink
refactor: model options
Browse files Browse the repository at this point in the history
  • Loading branch information
freemindcore committed Oct 12, 2022
1 parent a9e0506 commit a2863c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
17 changes: 12 additions & 5 deletions easy/controller/admin_auto_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from ninja_extra.permissions import BasePermission

from easy.controller.base import CrudAPIController
from easy.controller.meta_conf import (
GENERATE_CRUD_ATTR_DEFAULT,
MODEL_FIELDS_ATTR_DEFAULT,
MODEL_JOIN_ATTR_DEFAULT,
MODEL_RECURSIVE_ATTR_DEFAULT,
SENSITIVE_FIELDS_ATTR_DEFAULT,
)
from easy.permissions import AdminSitePermission, BaseApiPermission

logger = logging.getLogger(__name__)
Expand All @@ -24,11 +31,11 @@ def create_api_controller(
(object,),
{
"model": model,
"generate_crud": True,
"model_fields": "__all__",
"model_recursive": False,
"model_join": True,
"sensitive_fields": ["password", "token"],
"generate_crud": GENERATE_CRUD_ATTR_DEFAULT,
"model_fields": MODEL_FIELDS_ATTR_DEFAULT,
"model_recursive": MODEL_RECURSIVE_ATTR_DEFAULT,
"model_join": MODEL_JOIN_ATTR_DEFAULT,
"sensitive_fields": SENSITIVE_FIELDS_ATTR_DEFAULT,
},
)

Expand Down
10 changes: 5 additions & 5 deletions easy/controller/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ninja_extra import ControllerBase, http_delete, http_get, http_patch, http_put
from ninja_extra.pagination import paginate

from easy.controller.meta_conf import ModelOptions
from easy.controller.meta_conf import MODEL_FIELDS_ATTR_DEFAULT, ModelOptions
from easy.domain.orm import CrudModel
from easy.response import BaseApiResponse
from easy.services import BaseService
Expand Down Expand Up @@ -113,13 +113,13 @@ class Config:
if model_opts.model_exclude:
model_exclude = model_opts.model_exclude
else:
if model_opts.model_fields == "__all__":
model_fields = "__all__"
if model_opts.model_fields == MODEL_FIELDS_ATTR_DEFAULT:
model_fields = MODEL_FIELDS_ATTR_DEFAULT
else:
model_fields = (
model_opts.model_fields
model_opts.model_fields # type: ignore
if model_opts.model_fields
else "__all__"
else MODEL_FIELDS_ATTR_DEFAULT
)

async def add_obj( # type: ignore
Expand Down
14 changes: 7 additions & 7 deletions easy/controller/meta_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ def __init__(self, options: object = None):
"""
self.model: Optional[Type[models.Model]] = getattr(options, "model", None)
self.generate_crud: Optional[Union[bool]] = getattr(
options, GENERATE_CRUD_ATTR, True
options, GENERATE_CRUD_ATTR, GENERATE_CRUD_ATTR_DEFAULT
)
self.model_exclude: Optional[Union[str]] = getattr(
options, MODEL_EXCLUDE_ATTR, None
self.model_exclude: Union[Union[str], List[Any]] = getattr(
options, MODEL_EXCLUDE_ATTR, MODEL_EXCLUDE_ATTR_DEFAULT
)
self.model_fields: Optional[Union[str]] = getattr(
options, MODEL_FIELDS_ATTR, "__all__"
self.model_fields: Union[Union[str], List[Any]] = getattr(
options, MODEL_FIELDS_ATTR, MODEL_FIELDS_ATTR_DEFAULT
)
self.model_join: Optional[Union[bool]] = getattr(
options, MODEL_JOIN_ATTR, False
options, MODEL_JOIN_ATTR, MODEL_JOIN_ATTR_DEFAULT
)
self.model_recursive: Optional[Union[bool]] = getattr(
options, MODEL_RECURSIVE_ATTR, False
options, MODEL_RECURSIVE_ATTR, MODEL_RECURSIVE_ATTR_DEFAULT
)
self.sensitive_fields: Optional[Union[str, List[str]]] = getattr(
options, SENSITIVE_FIELDS_ATTR, list(SENSITIVE_FIELDS_ATTR_DEFAULT)
Expand Down

0 comments on commit a2863c7

Please sign in to comment.