get_api_config.py 3.85 KB
Newer Older
1 2 3 4 5 6
import importlib
from os.path import dirname, join, realpath
from typing import List, Optional, Union, Dict

from django.conf import settings

7
import yaml
8
from dotmap import DotMap
9 10

from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
11 12


13 14 15 16 17 18
def load_api_config() -> List[Dict]:
    """
    Returns the list of api objects without filtering add default attributes
    added to all objects if they are missing.
    """

19
    current_dir = dirname(realpath(__file__))
20
    with open(join(current_dir, "api_config.yml"), "r") as f:
21 22 23 24
        api_config = yaml.load(f)

    # clean api_config (add default arguments)
    DEFAULT_SETTINGS = {
25
        "is_api_view": False,
26 27
        "ignore_in_admin": False,
        "requires_testing": False,
Florent Chehab's avatar
Florent Chehab committed
28 29
        "moderation_level": 2,
        "versionned": False,
30
        "read_only": False,
31
        "viewset_permission": "default",
32
        "model": None,
33 34 35
    }

    for obj in api_config:
36
        tmp = "enforce_moderation_user_level"
37 38
        if tmp in obj.keys():
            obj[tmp] = OBJ_MODERATION_PERMISSIONS[obj[tmp]]
39 40 41 42 43
        for key in DEFAULT_SETTINGS:
            if key not in obj:
                obj[key] = DEFAULT_SETTINGS[key]

    return api_config
44 45 46


def get_api_objs(
47 48 49 50 51 52
    has_model: Optional[bool],
    ignore_in_admin: Optional[bool] = None,
    versionned: Optional[bool] = None,
    requires_testing: Union[None, bool, "smart"] = None,
    is_api_view: Optional[bool] = False,
    ignore_models: List[str] = list(),
Florent Chehab's avatar
Florent Chehab committed
53
    make_imports: bool = True,
54 55 56 57 58 59 60 61 62
) -> List[DotMap]:
    """
    Returns a list of DotMap objects corresponding the api config file
    with filtering applied to some attributes.
    If the parameter is `None` then no filtering is applied to this attribute.
    If the parameter is `True` or `False` the object is returned only if it matched.

    There is one exception for the parameter `requires_testing` if it is set to `smart` then
    the object is returned only if doesn't require testing or if testing is activated.
Florent Chehab's avatar
Florent Chehab committed
63 64

    make_imports: do we perform the model and viewsets imports ?
65
    """
66 67 68

    out = list()

69
    for entry in load_api_config():
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        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:
91 92 93 94 95 96 97 98 99 100
            if requires_testing == "smart":
                if obj.requires_testing and not settings.TESTING:
                    continue
            else:
                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
101 102 103 104 105 106 107

        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

Florent Chehab's avatar
Florent Chehab committed
108 109 110 111
        if make_imports:
            module = importlib.import_module(
                "backend_app.models.{}".format(obj.import_location)
            )
112

Florent Chehab's avatar
Florent Chehab committed
113 114 115 116 117
            if obj.model is not None:
                if obj.model in ignore_models:
                    continue
                Model = getattr(module, obj.model)
                obj.Model = Model
118

Florent Chehab's avatar
Florent Chehab committed
119 120 121
            if obj.viewset is not None:
                Viewset = getattr(module, obj.viewset)
                obj.Viewset = Viewset
122 123 124 125

        out.append(obj)

    return out