urls.py 2.42 KB
Newer Older
1
2
3
4
5
6
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

Florent Chehab's avatar
Florent Chehab committed
7
from backend_app.permissions import DEFAULT_VIEWSET_PERMISSIONS
8
9
10
11
12
from shared import get_api_config

from . import views
from dotmap import DotMap

13
14
import importlib

15
urlpatterns = [url(r"^api-docs/", include_docs_urls(title="Outgoing API"))]
16
17
18
19
20
router = routers.DefaultRouter()

ALL_MODELS = []
ALL_VIEWSETS = []

Florent Chehab's avatar
Florent Chehab committed
21
# Automatically load models and viewset based on API config file
22
23
api_config = get_api_config()

Florent Chehab's avatar
Florent Chehab committed
24
25
26
27
28
for entry in api_config:
    model_obj = DotMap(entry)
    if (not model_obj.requires_testing) or (
        settings.TESTING and model_obj.requires_testing
    ):
29
        module = importlib.import_module(
Florent Chehab's avatar
Florent Chehab committed
30
            "backend_app.models.{}".format(model_obj.import_location)
31
        )
Florent Chehab's avatar
Florent Chehab committed
32
        Viewset = getattr(module, model_obj.viewset)
33
34
        ALL_VIEWSETS.append(Viewset)

Florent Chehab's avatar
Florent Chehab committed
35
36
37
38
39
40
41
42
43
        if model_obj.model is not None and not model_obj.ignore_in_admin:
            ALL_MODELS.append(getattr(module, model_obj.model))

        # Creating the correct router entry
        str_url = model_obj.api_end_point
        if "api_attr" in model_obj:
            str_url += "/{}".format(model_obj.api_attr)
        if "api_name" in model_obj:
            router.register(str_url, Viewset, model_obj.api_name)
44
45
        else:
            router.register(str_url, Viewset)
46

Florent Chehab's avatar
Florent Chehab committed
47
# Add all the endpoints for the base api
48
urlpatterns += [url(r"^api/", include(router.urls))]
Florent Chehab's avatar
Florent Chehab committed
49
50

# Add some custom APIs
51
urlpatterns.append(path("api/serverModerationStatus/", views.app_moderation_status))
52

Florent Chehab's avatar
Florent Chehab committed
53
54
55
56
57
#######
# Models and Viewset checks
#######

# Check that all the models config have been set
58
59
60
61
for Model in ALL_MODELS:
    for key in Model.model_config:
        val = Model.model_config[key]
        if val is None:
62
63
            raise Exception(
                "You forgot to set the {} config variable in the model {}".format(
Florent Chehab's avatar
Florent Chehab committed
64
                    key, str(Model)
65
66
                )
            )
67

Florent Chehab's avatar
Florent Chehab committed
68
# Check that all the viewsets have at least the permissions from the default viewset permissions
69
70
71
72
for Viewset in ALL_VIEWSETS:
    for p in DEFAULT_VIEWSET_PERMISSIONS:
        v_p = Viewset.permission_classes
        if p not in v_p:
73
74
75
76
77
            raise Exception(
                "Permission_classes are not defined correctly in the viewset {}".format(
                    str(Viewset)
                )
            )