urls.py 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
from django.conf import settings
from django.conf.urls import include, url
from django.urls import path
from rest_framework import routers
from rest_framework.documentation import include_docs_urls

from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from shared import get_api_config

from . import views
from dotmap import DotMap

13
14
import importlib

15
16
17
18
19
20
21
22
23
24
25
26
27
28
urlpatterns = [
    url(r'^api-docs/', include_docs_urls(title='Outgoing API')),
]
router = routers.DefaultRouter()

ALL_MODELS = []
ALL_VIEWSETS = []

# Automatically loading models based on API config file
api_config = get_api_config()

for model in api_config:
    model = DotMap(model)
    if not model.requires_testing:
29
30
31
        module = importlib.import_module("backend.models.{}".format(model.import_location))
        Viewset = getattr(module, model.viewset)
        ALL_VIEWSETS.append(Viewset)
32
        if model.model is not None and not model.ignore_in_admin:
33
34
35
36
37
38
39
40
41
42
            ALL_MODELS.append(getattr(module, model.model))

        # print(viewset)
        str_url = model.api_end_point
        if 'api_attr' in model:
            str_url += '/{}'.format(model.api_attr)
        if 'api_name' in model:
            router.register(str_url, Viewset, model.api_name)
        else:
            router.register(str_url, Viewset)
43
44
45
46
47
48
49


if settings.TESTING:

    for model in api_config:
        model = DotMap(model)
        if model.requires_testing:
50
51
52
            module = importlib.import_module("backend.models.{}".format(model.import_location))
            Viewset = getattr(module, model.viewset)
            ALL_VIEWSETS.append(Viewset)
53
            if model.model is not None:
54
                ALL_MODELS.append(getattr(module, model.model))
55
56
57
58
59

            str_url = model.api_end_point
            if 'api_attr' in model:
                str_url += '/{}'.format(model.api_attr)
            if 'api_name' in model:
60
                router.register(str_url, Viewset, model.api_name)
61
            else:
62
                router.register(str_url, Viewset, model.viewset)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78


urlpatterns += [url(r'^api/', include(router.urls))]
urlpatterns.append(path('api/serverModerationStatus/', views.app_moderation_status))

for Model in ALL_MODELS:
    for key in Model.model_config:
        val = Model.model_config[key]
        if val is None:
            raise Exception("You forgot to set the {} config variable in the model {}".format(key, str(model)))

for Viewset in ALL_VIEWSETS:
    for p in DEFAULT_VIEWSET_PERMISSIONS:
        v_p = Viewset.permission_classes
        if p not in v_p:
            raise Exception("Permission_classes are not defined correctly in the viewset {}".format(str(viewset)))