Skip to content

Commit

Permalink
Merge pull request #12 from freemindcore/feat/django-model-config
Browse files Browse the repository at this point in the history
Feat/django model config
  • Loading branch information
freemindcore authored Oct 16, 2022
2 parents fa0aeae + f7ecd82 commit d21e032
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[bumpversion]
current_version = 0.1.34
current_version = 0.1.36
commit = True
tag = True
parse = (?P<major>\d+)\.(?P<feat>\d+)\.(?P<patch>\d+)
serialize =
serialize =
{major}.{feat}.{patch}

[bumpversion:file:easy/__init__.py]
2 changes: 1 addition & 1 deletion easy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Django Easy API - Easy and Fast Django REST framework based on Django-ninja-extra"""

__version__ = "0.1.34"
__version__ = "0.1.36"

from easy.main import EasyAPI

Expand Down
23 changes: 13 additions & 10 deletions easy/controller/auto_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
from easy.controller.base import CrudAPIController
from easy.controller.meta_conf import (
GENERATE_CRUD_ATTR,
GENERATE_CRUD_ATTR_DEFAULT,
MODEL_EXCLUDE_ATTR,
MODEL_FIELDS_ATTR,
MODEL_FIELDS_ATTR_DEFAULT,
MODEL_JOIN_ATTR,
MODEL_JOIN_ATTR_DEFAULT,
MODEL_RECURSIVE_ATTR,
MODEL_RECURSIVE_ATTR_DEFAULT,
SENSITIVE_FIELDS_ATTR,
SENSITIVE_FIELDS_ATTR_DEFAULT,
ModelOptions,
)
from easy.permissions import AdminSitePermission, BaseApiPermission

Expand All @@ -31,16 +28,22 @@ def create_api_controller(
) -> Union[Type[ControllerBase], Type]:
"""Create APIController class dynamically, with specified permission class"""
model_name = model.__name__ # type:ignore

model_opts: ModelOptions = ModelOptions.get_model_options(
getattr(model, "ApiMeta", None)
)

Meta = type(
"Meta",
(object,),
{
"model": model,
GENERATE_CRUD_ATTR: GENERATE_CRUD_ATTR_DEFAULT,
MODEL_FIELDS_ATTR: MODEL_FIELDS_ATTR_DEFAULT,
MODEL_RECURSIVE_ATTR: MODEL_RECURSIVE_ATTR_DEFAULT,
MODEL_JOIN_ATTR: MODEL_JOIN_ATTR_DEFAULT,
SENSITIVE_FIELDS_ATTR: SENSITIVE_FIELDS_ATTR_DEFAULT,
GENERATE_CRUD_ATTR: model_opts.generate_crud,
MODEL_EXCLUDE_ATTR: model_opts.model_exclude,
MODEL_FIELDS_ATTR: model_opts.model_fields,
MODEL_RECURSIVE_ATTR: model_opts.model_recursive,
MODEL_JOIN_ATTR: model_opts.model_join,
SENSITIVE_FIELDS_ATTR: model_opts.model_fields,
},
)

Expand Down
2 changes: 1 addition & 1 deletion easy/controller/meta_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, options: Dict = None):
)

@classmethod
def get_model_options(cls, meta: Dict) -> Any:
def get_model_options(cls, meta: Optional[Any]) -> "ModelOptions":
return ModelOptions(meta)

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions tests/easy_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Category(TestBaseModel):
title = models.CharField(max_length=100)
status = models.PositiveSmallIntegerField(default=1, null=True)

class ApiMeta:
generate_crud = False


class Client(TestBaseModel):
key = models.CharField(max_length=20, unique=True)
Expand Down
1 change: 1 addition & 0 deletions tests/test_async_auto_crud_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ async def test_crud_default_create_some_fields(
assert response.json()["data"]["key"] == "Type"
with pytest.raises(KeyError):
print(response.json()["data"]["password"])
with pytest.raises(KeyError):
print(response.json()["data"]["category"])

async def test_crud_default_patch(self, transactional_db, easy_api_client):
Expand Down
48 changes: 26 additions & 22 deletions tests/test_auto_api_creation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from ninja_extra.operation import AsyncOperation

from easy import EasyAPI
Expand Down Expand Up @@ -34,31 +35,34 @@ def test_auto_generate_admin_api():

async def test_auto_apis(transactional_db, user, easy_api_client):
for controller_class in controllers:
if not str(controller_class).endswith("ClientAdminAPIController"):
continue
client = easy_api_client(controller_class, api_user=user, has_perm=True)
response = await client.get("/", data={}, json={}, user=user)
assert response.status_code == 403
if str(controller_class).endswith("ClientAdminAPIController"):
client = easy_api_client(controller_class, api_user=user, has_perm=True)
response = await client.get("/", data={}, json={}, user=user)
assert response.status_code == 403

client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.get("/", data={}, json={}, user=user)
assert response.status_code == 200
assert response.json()["data"] == []
client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.get("/", data={}, json={}, user=user)
assert response.status_code == 200
assert response.json()["data"] == []

client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.delete("/20000")
assert response.status_code == 403
client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.delete("/20000")
assert response.status_code == 403

client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.delete("/20000", data={}, json={}, user=user)
assert response.status_code == 200
assert response.json()["code"] == 404
client = easy_api_client(
controller_class, api_user=user, has_perm=True, is_staff=True
)
response = await client.delete("/20000", data={}, json={}, user=user)
assert response.status_code == 200
assert response.json()["code"] == 404
elif str(controller_class).endswith("CategoryAdminAPIController"):
client = easy_api_client(controller_class, api_user=user, has_perm=True)
with pytest.raises(Exception):
await client.get("/", data={}, json={}, user=user)


async def test_auto_generation_settings(settings):
Expand Down

0 comments on commit d21e032

Please sign in to comment.