admin.py 1.13 KB
Newer Older
1 2 3 4 5
from django.contrib import admin
from dotmap import DotMap
from reversion_compare.admin import CompareVersionAdmin

from shared import get_api_config
6
import importlib
7 8 9

api_config = get_api_config()

10 11
CLASSIC_MODELS = []
VERSIONNED_MODELS = []
12 13

for model in api_config:
14
    if "model" in model and model["model"]:
15 16
        model = DotMap(model)
        if (not model.requires_testing) and (not model.ignore_in_admin):
17 18 19
            module = importlib.import_module(
                "backend_app.models.{}".format(model.import_location)
            )
20
            if model.versionned:
21
                VERSIONNED_MODELS.append(getattr(module, model.model))
22
            else:
23
                CLASSIC_MODELS.append(getattr(module, model.model))
24 25 26 27 28 29 30 31 32 33 34 35


for model in CLASSIC_MODELS:
    admin.site.register(model)
    try:
        model.get_serializer()
        raise Exception("CLASSIC MODEL SHOULDN'T have this method")
    except AttributeError:
        pass

for model in VERSIONNED_MODELS:
    admin.site.register(model, CompareVersionAdmin)
36 37
    if model.get_serializer().Meta.model != model:
        raise Exception("Get_serializer configuration incorrect in", str(model))