get_api_config.py 2.63 KB
Newer Older
1
2
import yaml
from os.path import join, realpath, dirname
3
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
4
5
6
from dotmap import DotMap
from django.conf import settings
import importlib
7
8
9
10


def get_api_config():
    current_dir = dirname(realpath(__file__))
11
    with open(join(current_dir, "api_config.yml"), "r") as f:
12
13
14
15
        api_config = yaml.load(f)

    # clean api_config (add default arguments)
    DEFAULT_SETTINGS = {
16
        "is_api_view": False,
17
18
        "ignore_in_admin": False,
        "requires_testing": False,
Florent Chehab's avatar
Florent Chehab committed
19
20
        "moderation_level": 2,
        "versionned": False,
21
        "read_only": False,
22
        "viewset_permission": "default",
23
        "model": None,
24
25
26
    }

    for obj in api_config:
27
        tmp = "enforce_moderation_user_level"
28
29
        if tmp in obj.keys():
            obj[tmp] = OBJ_MODERATION_PERMISSIONS[obj[tmp]]
30
31
32
33
34
        for key in DEFAULT_SETTINGS:
            if key not in obj:
                obj[key] = DEFAULT_SETTINGS[key]

    return api_config
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96


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