Commit 9715fac0 authored by Florent Chehab's avatar Florent Chehab

Fixed backend batch import, should fix testing too

parent 69453f94
...@@ -3,24 +3,22 @@ from dotmap import DotMap ...@@ -3,24 +3,22 @@ from dotmap import DotMap
from reversion_compare.admin import CompareVersionAdmin from reversion_compare.admin import CompareVersionAdmin
from shared import get_api_config from shared import get_api_config
import importlib
api_config = get_api_config() api_config = get_api_config()
CLASSIC_MODELS_str = "" CLASSIC_MODELS = []
VERSIONNED_MODELS_str = "" VERSIONNED_MODELS = []
for model in api_config: for model in api_config:
if "model" in model and model['model']: if "model" in model and model['model']:
model = DotMap(model) model = DotMap(model)
if (not model.requires_testing) and (not model.ignore_in_admin): if (not model.requires_testing) and (not model.ignore_in_admin):
exec("from backend.models.{} import {}".format(model.import_location, model.model)) module = importlib.import_module("backend.models.{}".format (model.import_location))
if model.versionned: if model.versionned:
VERSIONNED_MODELS_str += model.model + ',' VERSIONNED_MODELS.append(getattr(module, model.model))
else: else:
CLASSIC_MODELS_str += model.model + ',' CLASSIC_MODELS.append(getattr(module, model.model))
exec("CLASSIC_MODELS = [{}]".format(CLASSIC_MODELS_str))
exec("VERSIONNED_MODELS = [{}]".format(VERSIONNED_MODELS_str))
for model in CLASSIC_MODELS: for model in CLASSIC_MODELS:
......
...@@ -2,6 +2,7 @@ from django.conf import settings ...@@ -2,6 +2,7 @@ from django.conf import settings
from dotmap import DotMap from dotmap import DotMap
from shared import get_api_config from shared import get_api_config
import importlib
api_config = get_api_config() api_config = get_api_config()
...@@ -11,16 +12,16 @@ for model in api_config: ...@@ -11,16 +12,16 @@ for model in api_config:
model = DotMap(model) model = DotMap(model)
if not model.requires_testing: if not model.requires_testing:
if model.viewset != 'UserDataViewSet': if model.viewset != 'UserDataViewSet':
exec("from backend.models.{} import {}".format(model.import_location, model.viewset)) module = importlib.import_module("backend.models.{}".format (model.import_location))
exec("ALL_VIEWSETS['{}'] = {}".format(model.viewset, model.viewset)) ALL_VIEWSETS[model.viewset] = getattr(module, model.viewset)
if settings.TESTING: if settings.TESTING:
for model in api_config: for model in api_config:
model = DotMap(model) model = DotMap(model)
if model.requires_testing: if model.requires_testing:
if model.viewset != 'UserDataViewSet': if model.viewset != 'UserDataViewSet':
exec("from backend.models.{} import {}".format(model.import_location, model.viewset)) module = importlib.import_module("backend.models.{}".format (model.import_location))
exec("ALL_VIEWSETS['{}'] = {}".format(model.viewset, model.viewset)) ALL_VIEWSETS[model.viewset] = getattr(module, model.viewset)
class Request(object): class Request(object):
......
...@@ -10,6 +10,8 @@ from shared import get_api_config ...@@ -10,6 +10,8 @@ from shared import get_api_config
from . import views from . import views
from dotmap import DotMap from dotmap import DotMap
import importlib
urlpatterns = [ urlpatterns = [
url(r'^api-docs/', include_docs_urls(title='Outgoing API')), url(r'^api-docs/', include_docs_urls(title='Outgoing API')),
] ]
...@@ -24,11 +26,20 @@ api_config = get_api_config() ...@@ -24,11 +26,20 @@ api_config = get_api_config()
for model in api_config: for model in api_config:
model = DotMap(model) model = DotMap(model)
if not model.requires_testing: if not model.requires_testing:
exec("from backend.models.{} import {}".format(model.import_location, model.viewset)) module = importlib.import_module("backend.models.{}".format(model.import_location))
exec("ALL_VIEWSETS.append({})".format(model.viewset)) Viewset = getattr(module, model.viewset)
ALL_VIEWSETS.append(Viewset)
if model.model is not None and not model.ignore_in_admin: if model.model is not None and not model.ignore_in_admin:
exec("from backend.models.{} import {}".format(model.import_location, model.model)) ALL_MODELS.append(getattr(module, model.model))
exec("ALL_MODELS.append({})".format(model.model))
# print(viewset)
str_url = model.api_end_point
if 'api_attr' in model:
str_url += '/{}'.format(model.api_attr)
if 'api_name' in model:
router.register(str_url, Viewset, model.api_name)
else:
router.register(str_url, Viewset)
if settings.TESTING: if settings.TESTING:
...@@ -36,33 +47,19 @@ if settings.TESTING: ...@@ -36,33 +47,19 @@ if settings.TESTING:
for model in api_config: for model in api_config:
model = DotMap(model) model = DotMap(model)
if model.requires_testing: if model.requires_testing:
exec("from backend.models.{} import {}".format(model.import_location, model.viewset)) module = importlib.import_module("backend.models.{}".format(model.import_location))
exec("ALL_VIEWSET.append({})".format(model.viewset)) Viewset = getattr(module, model.viewset)
ALL_VIEWSETS.append(Viewset)
if model.model is not None: if model.model is not None:
exec("from backend.models.{} import {}".format(model.import_location, model.model)) ALL_MODELS.append(getattr(module, model.model))
exec("ALL_MODELS.append({})".format(model.model))
str_url = model.api_end_point str_url = model.api_end_point
if 'api_attr' in model: if 'api_attr' in model:
str_url += '/{}'.format(model.api_attr) str_url += '/{}'.format(model.api_attr)
if 'api_name' in model: if 'api_name' in model:
cmd = "r'{}', {}, '{}'".format(str_url, model.viewset, model.api_name) router.register(str_url, Viewset, model.api_name)
else: else:
cmd = "r'{}', {}".format(str_url, model.viewset) router.register(str_url, Viewset, model.viewset)
exec("router.register({})".format(cmd))
for model in api_config:
model = DotMap(model)
if not model.requires_testing:
str_url = model.api_end_point
if 'api_attr' in model:
str_url += '/{}'.format(model.api_attr)
if 'api_name' in model:
cmd = "r'{}', {}, '{}'".format(str_url, model.viewset, model.api_name)
else:
cmd = "r'{}', {}".format(str_url, model.viewset)
exec("router.register({})".format(cmd))
urlpatterns += [url(r'^api/', include(router.urls))] urlpatterns += [url(r'^api/', include(router.urls))]
......
...@@ -6,8 +6,8 @@ djangorestframework==3.8.2 # Django REST Framework ...@@ -6,8 +6,8 @@ djangorestframework==3.8.2 # Django REST Framework
coreapi==2.3.3 # Automatic API doc generation coreapi==2.3.3 # Automatic API doc generation
django-reversion==2.0.13 django-reversion==2.0.13
django-reversion-compare==0.8.4 django-reversion-compare==0.8.4
pytest-django==3.4.2 pytest-django==3.4.7
pytest-cov==2.5.1 pytest-cov==2.6.1
pytest-xdist==1.23.0 pytest-xdist==1.23.0
flake8 flake8
django-debug-toolbar==1.9.1 django-debug-toolbar==1.9.1
......
from .get_api_config import get_api_config # noqa: F401 from .get_api_config import get_api_config # noqa: F401
from .obj_moderation_permission import DEFAULT_OBJ_MODERATION_LV, OBJ_MODERATION_PERMISSIONS from .obj_moderation_permission import DEFAULT_OBJ_MODERATION_LV, OBJ_MODERATION_PERMISSIONS # noqa: F401
\ No newline at end of file
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