admin.py 1.62 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()

Florent Chehab's avatar
Florent Chehab committed
10
# models that are versionned in the app
11
VERSIONNED_MODELS = []
Florent Chehab's avatar
Florent Chehab committed
12 13 14
# Other models, ie not versionned
CLASSIC_MODELS = []

15

Florent Chehab's avatar
Florent Chehab committed
16 17 18 19 20 21
# Go through the API configuraion
for entry in api_config:
    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
22
            module = importlib.import_module(
Florent Chehab's avatar
Florent Chehab committed
23
                "backend_app.models.{}".format(model_obj.import_location)
24
            )
Florent Chehab's avatar
Florent Chehab committed
25 26 27
            # Add it to the correct list of models
            if model_obj.versionned:
                VERSIONNED_MODELS.append(getattr(module, model_obj.model))
28
            else:
Florent Chehab's avatar
Florent Chehab committed
29
                CLASSIC_MODELS.append(getattr(module, model_obj.model))
30

Florent Chehab's avatar
Florent Chehab committed
31 32 33
#######
# Some dynamic checks
#######
34

Florent Chehab's avatar
Florent Chehab committed
35 36 37
for Model in CLASSIC_MODELS:
    # Register the model in the admin in a standard way
    admin.site.register(Model)
38
    try:
Florent Chehab's avatar
Florent Chehab committed
39 40 41
        # 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")
42 43 44
    except AttributeError:
        pass

Florent Chehab's avatar
Florent Chehab committed
45 46 47 48 49 50
for Model in VERSIONNED_MODELS:
    # Register the model in the admin with versioning
    admin.site.register(Model, CompareVersionAdmin)
    # Check that it has a get_serializer method
    if Model.get_serializer().Meta.model != Model:
        raise Exception("Get_serializer configuration incorrect in", str(Model))