urls.py 2.02 KB
Newer Older
1 2 3 4
from django.conf.urls import include, url
from rest_framework import routers
from rest_framework.documentation import include_docs_urls

Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.permissions import DEFAULT_VIEWSET_PERMISSIONS
6
from shared import get_api_objs
7

8

9
urlpatterns = [url(r"^api-docs/", include_docs_urls(title="Outgoing API"))]
10 11
router = routers.DefaultRouter()

12 13 14 15
ALL_MODELS = map(
    lambda obj: obj.Model,
    get_api_objs(has_model=True, ignore_in_admin=False, requires_testing="smart"),
)
16

17
ALL_VIEWSETS = []
18

19

20 21 22 23
for api_obj in get_api_objs(
    has_model=None, requires_testing="smart", is_api_view=False
):
    ALL_VIEWSETS.append(api_obj.Viewset)
Florent Chehab's avatar
Florent Chehab committed
24

25 26 27 28 29 30 31 32
    # Creating the correct router entry
    str_url = api_obj.api_end_point
    if "api_attr" in api_obj:
        str_url += "/{}".format(api_obj.api_attr)
    if "api_name" in api_obj:
        router.register(str_url, api_obj.Viewset, api_obj.api_name)
    else:
        router.register(str_url, api_obj.Viewset)
33

Florent Chehab's avatar
Florent Chehab committed
34
# Add all the endpoints for the base api
35 36 37 38 39 40 41
urlpatterns.append(url(r"^api/", include(router.urls)))

for api_obj in get_api_objs(has_model=None, requires_testing="smart", is_api_view=True):
    urlpatterns.append(
        url(r"^api/{}/".format(api_obj.api_end_point), api_obj.Viewset.as_view())
    )

42

Florent Chehab's avatar
Florent Chehab committed
43 44 45 46 47
#######
# Models and Viewset checks
#######

# Check that all the models config have been set
48 49 50 51
for Model in ALL_MODELS:
    for key in Model.model_config:
        val = Model.model_config[key]
        if val is None:
52 53
            raise Exception(
                "You forgot to set the {} config variable in the model {}".format(
Florent Chehab's avatar
Florent Chehab committed
54
                    key, str(Model)
55 56
                )
            )
57

Florent Chehab's avatar
Florent Chehab committed
58
# Check that all the viewsets have at least the permissions from the default viewset permissions
59 60 61 62
for Viewset in ALL_VIEWSETS:
    for p in DEFAULT_VIEWSET_PERMISSIONS:
        v_p = Viewset.permission_classes
        if p not in v_p:
63 64 65 66 67
            raise Exception(
                "Permission_classes are not defined correctly in the viewset {}".format(
                    str(Viewset)
                )
            )