viewsets.py 1.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
import importlib
from typing import List, Optional, Union

from django.conf import settings

from dotmap import DotMap

from .utils import load_viewsets_config


def get_viewsets_info(
    ignore_in_admin: Optional[bool] = None,
    requires_testing: Union[None, bool, "smart"] = None,
    is_api_view: Optional[bool] = False,
) -> 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 matches.

    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.
    """
    out = list()

    for obj in load_viewsets_config():

        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 == "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

        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)
        )

        Viewset = getattr(module, obj.viewset)
        obj.Viewset = Viewset

        out.append(obj)

    return out