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

Florent Chehab's avatar
Florent Chehab committed
33
34
35
#######
# Some dynamic checks
#######
36

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

Florent Chehab's avatar
Florent Chehab committed
47
48
49
50
51
52
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))