Commit 304fa96f authored by Florent Chehab's avatar Florent Chehab

Dynamic imports cleaned in admin.py

Creation of a get_api_objs function that is easier to understand
More to come
parent 85d181ad
Pipeline #36170 passed with stages
in 4 minutes and 27 seconds
from django.contrib import admin
from dotmap import DotMap
from reversion_compare.admin import CompareVersionAdmin
from shared import get_api_config
import importlib
from reversion_compare.admin import CompareVersionAdmin
from shared import get_api_config, get_api_objs
api_config = get_api_config()
# models that are versionned in the app
VERSIONNED_MODELS = []
# Other models, ie not versionned
CLASSIC_MODELS = []
VERSIONNED_MODELS = map(
lambda obj: obj.Model,
get_api_objs(
has_model=True, ignore_in_admin=False, versionned=True, requires_testing=False
),
)
CLASSIC_MODELS = map(
lambda obj: obj.Model,
get_api_objs(
has_model=True, ignore_in_admin=False, versionned=False, requires_testing=False
),
)
# Go through the API configuraion
for entry in api_config:
if "is_api_view" in entry and entry["is_api_view"]:
continue
if "model" in entry and entry["model"]:
model_obj = DotMap(entry)
if (not model_obj.requires_testing) and (not model_obj.ignore_in_admin):
# Import the model
module = importlib.import_module(
"backend_app.models.{}".format(model_obj.import_location)
)
# Add it to the correct list of models
if model_obj.versionned:
VERSIONNED_MODELS.append(getattr(module, model_obj.model))
else:
CLASSIC_MODELS.append(getattr(module, model_obj.model))
#######
# Some dynamic checks
# Register the models and perform some dynamic checks
#######
for Model in CLASSIC_MODELS:
......@@ -40,7 +29,10 @@ for Model in CLASSIC_MODELS:
try:
# Check that it doesn't have the get_serializer method
Model.get_serializer()
raise Exception("A 'CLASSIC MODEL' SHOULDN'T have the get_serializer method")
raise Exception(
"A 'CLASSIC MODEL' SHOULDN'T have the "
"get_serializer method, {}".format(Model)
)
except AttributeError:
pass
......
from .get_api_config import get_api_config
from .get_api_config import get_api_config, get_api_objs
from .obj_moderation_permission import (
DEFAULT_OBJ_MODERATION_LV,
OBJ_MODERATION_PERMISSIONS,
)
__all__ = ["get_api_config", "DEFAULT_OBJ_MODERATION_LV", "OBJ_MODERATION_PERMISSIONS"]
__all__ = [
"get_api_config",
"DEFAULT_OBJ_MODERATION_LV",
"OBJ_MODERATION_PERMISSIONS",
"get_api_objs",
]
import yaml
from os.path import join, realpath, dirname
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
from dotmap import DotMap
from django.conf import settings
import importlib
def get_api_config():
......@@ -10,12 +13,14 @@ def get_api_config():
# clean api_config (add default arguments)
DEFAULT_SETTINGS = {
"is_api_view": False,
"ignore_in_admin": False,
"requires_testing": False,
"moderation_level": 2,
"versionned": False,
"read_only": False,
"viewset_permission": "default",
"model": None,
}
for obj in api_config:
......@@ -27,3 +32,65 @@ def get_api_config():
obj[key] = DEFAULT_SETTINGS[key]
return api_config
def get_api_objs(
has_model,
ignore_in_admin=None,
versionned=None,
requires_testing=None,
is_api_view=False,
) -> dict:
out = list()
for entry in get_api_config():
obj = DotMap(entry)
if has_model is not None:
if has_model and obj.model is None:
continue
if not has_model and obj.model is not None:
continue
if versionned is not None:
if versionned and not obj.versionned:
continue
if not versionned and obj.versionned:
continue
if ignore_in_admin is not None:
if ignore_in_admin and not obj.ignore_in_admin:
continue
if not ignore_in_admin and obj.ignore_in_admin:
continue
if requires_testing is not None:
if requires_testing and not settings.TESTING:
continue
if requires_testing and not obj.requires_testing:
continue
if not requires_testing and obj.requires_testing:
continue
if is_api_view is not None:
if is_api_view and not obj.is_api_view:
continue
if not is_api_view and obj.is_api_view:
continue
module = importlib.import_module(
"backend_app.models.{}".format(obj.import_location)
)
if obj.model is not None:
Model = getattr(module, obj.model)
obj.Model = Model
if obj.viewset is not None:
Viewset = getattr(module, obj.viewset)
obj.Viewset = Viewset
out.append(obj)
return out
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment