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_app.permissions import DEFAULT_VIEWSET_PERMISSIONS from shared import get_api_config from . import views from dotmap import DotMap import importlib urlpatterns = [url(r"^api-docs/", include_docs_urls(title="Outgoing API"))] router = routers.DefaultRouter() ALL_MODELS = [] ALL_VIEWSETS = [] # Automatically load models and viewset based on API config file api_config = get_api_config() for entry in api_config: model_obj = DotMap(entry) if (not model_obj.requires_testing) or ( settings.TESTING and model_obj.requires_testing ): module = importlib.import_module( "backend_app.models.{}".format(model_obj.import_location) ) Viewset = getattr(module, model_obj.viewset) ALL_VIEWSETS.append(Viewset) 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) else: router.register(str_url, Viewset) # Add all the endpoints for the base api urlpatterns += [url(r"^api/", include(router.urls))] # Add some custom APIs urlpatterns.append(path("api/serverModerationStatus/", views.app_moderation_status)) ####### # Models and Viewset checks ####### # Check that all the models config have been set 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) ) ) # Check that all the viewsets have at least the permissions from the default viewset permissions 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) ) )