Skip to content

Commit d21e032

Browse files
authored
Merge pull request #12 from freemindcore/feat/django-model-config
Feat/django model config
2 parents fa0aeae + f7ecd82 commit d21e032

File tree

7 files changed

+47
-36
lines changed

7 files changed

+47
-36
lines changed

.bumpversion.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[bumpversion]
2-
current_version = 0.1.34
2+
current_version = 0.1.36
33
commit = True
44
tag = True
55
parse = (?P<major>\d+)\.(?P<feat>\d+)\.(?P<patch>\d+)
6-
serialize =
6+
serialize =
77
{major}.{feat}.{patch}
88

99
[bumpversion:file:easy/__init__.py]

easy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Django Easy API - Easy and Fast Django REST framework based on Django-ninja-extra"""
22

3-
__version__ = "0.1.34"
3+
__version__ = "0.1.36"
44

55
from easy.main import EasyAPI
66

easy/controller/auto_api.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
from easy.controller.base import CrudAPIController
99
from easy.controller.meta_conf import (
1010
GENERATE_CRUD_ATTR,
11-
GENERATE_CRUD_ATTR_DEFAULT,
11+
MODEL_EXCLUDE_ATTR,
1212
MODEL_FIELDS_ATTR,
13-
MODEL_FIELDS_ATTR_DEFAULT,
1413
MODEL_JOIN_ATTR,
15-
MODEL_JOIN_ATTR_DEFAULT,
1614
MODEL_RECURSIVE_ATTR,
17-
MODEL_RECURSIVE_ATTR_DEFAULT,
1815
SENSITIVE_FIELDS_ATTR,
19-
SENSITIVE_FIELDS_ATTR_DEFAULT,
16+
ModelOptions,
2017
)
2118
from easy.permissions import AdminSitePermission, BaseApiPermission
2219

@@ -31,16 +28,22 @@ def create_api_controller(
3128
) -> Union[Type[ControllerBase], Type]:
3229
"""Create APIController class dynamically, with specified permission class"""
3330
model_name = model.__name__ # type:ignore
31+
32+
model_opts: ModelOptions = ModelOptions.get_model_options(
33+
getattr(model, "ApiMeta", None)
34+
)
35+
3436
Meta = type(
3537
"Meta",
3638
(object,),
3739
{
3840
"model": model,
39-
GENERATE_CRUD_ATTR: GENERATE_CRUD_ATTR_DEFAULT,
40-
MODEL_FIELDS_ATTR: MODEL_FIELDS_ATTR_DEFAULT,
41-
MODEL_RECURSIVE_ATTR: MODEL_RECURSIVE_ATTR_DEFAULT,
42-
MODEL_JOIN_ATTR: MODEL_JOIN_ATTR_DEFAULT,
43-
SENSITIVE_FIELDS_ATTR: SENSITIVE_FIELDS_ATTR_DEFAULT,
41+
GENERATE_CRUD_ATTR: model_opts.generate_crud,
42+
MODEL_EXCLUDE_ATTR: model_opts.model_exclude,
43+
MODEL_FIELDS_ATTR: model_opts.model_fields,
44+
MODEL_RECURSIVE_ATTR: model_opts.model_recursive,
45+
MODEL_JOIN_ATTR: model_opts.model_join,
46+
SENSITIVE_FIELDS_ATTR: model_opts.model_fields,
4447
},
4548
)
4649

easy/controller/meta_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, options: Dict = None):
4949
)
5050

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

5555
@classmethod

tests/easy_app/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Category(TestBaseModel):
1010
title = models.CharField(max_length=100)
1111
status = models.PositiveSmallIntegerField(default=1, null=True)
1212

13+
class ApiMeta:
14+
generate_crud = False
15+
1316

1417
class Client(TestBaseModel):
1518
key = models.CharField(max_length=20, unique=True)

tests/test_async_auto_crud_apis.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ async def test_crud_default_create_some_fields(
228228
assert response.json()["data"]["key"] == "Type"
229229
with pytest.raises(KeyError):
230230
print(response.json()["data"]["password"])
231+
with pytest.raises(KeyError):
231232
print(response.json()["data"]["category"])
232233

233234
async def test_crud_default_patch(self, transactional_db, easy_api_client):

tests/test_auto_api_creation.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from ninja_extra.operation import AsyncOperation
23

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

3536
async def test_auto_apis(transactional_db, user, easy_api_client):
3637
for controller_class in controllers:
37-
if not str(controller_class).endswith("ClientAdminAPIController"):
38-
continue
39-
client = easy_api_client(controller_class, api_user=user, has_perm=True)
40-
response = await client.get("/", data={}, json={}, user=user)
41-
assert response.status_code == 403
38+
if str(controller_class).endswith("ClientAdminAPIController"):
39+
client = easy_api_client(controller_class, api_user=user, has_perm=True)
40+
response = await client.get("/", data={}, json={}, user=user)
41+
assert response.status_code == 403
4242

43-
client = easy_api_client(
44-
controller_class, api_user=user, has_perm=True, is_staff=True
45-
)
46-
response = await client.get("/", data={}, json={}, user=user)
47-
assert response.status_code == 200
48-
assert response.json()["data"] == []
43+
client = easy_api_client(
44+
controller_class, api_user=user, has_perm=True, is_staff=True
45+
)
46+
response = await client.get("/", data={}, json={}, user=user)
47+
assert response.status_code == 200
48+
assert response.json()["data"] == []
4949

50-
client = easy_api_client(
51-
controller_class, api_user=user, has_perm=True, is_staff=True
52-
)
53-
response = await client.delete("/20000")
54-
assert response.status_code == 403
50+
client = easy_api_client(
51+
controller_class, api_user=user, has_perm=True, is_staff=True
52+
)
53+
response = await client.delete("/20000")
54+
assert response.status_code == 403
5555

56-
client = easy_api_client(
57-
controller_class, api_user=user, has_perm=True, is_staff=True
58-
)
59-
response = await client.delete("/20000", data={}, json={}, user=user)
60-
assert response.status_code == 200
61-
assert response.json()["code"] == 404
56+
client = easy_api_client(
57+
controller_class, api_user=user, has_perm=True, is_staff=True
58+
)
59+
response = await client.delete("/20000", data={}, json={}, user=user)
60+
assert response.status_code == 200
61+
assert response.json()["code"] == 404
62+
elif str(controller_class).endswith("CategoryAdminAPIController"):
63+
client = easy_api_client(controller_class, api_user=user, has_perm=True)
64+
with pytest.raises(Exception):
65+
await client.get("/", data={}, json={}, user=user)
6266

6367

6468
async def test_auto_generation_settings(settings):

0 commit comments

Comments
 (0)