Repository: watchdogpolska/small_eod

Issue:
Niekompletny wykaz endpointów API w /api
Na `/api` (np. https://dev.small-eod.siecobywatelska.pl/api/ ) nie mamy kompletnego wykazu endpointów API. Kompletny jest dostępny przez ReDoc np. na https://dev.small-eod.siecobywatelska.pl/api/redoc/ .

Powinniśmy to naprawić, bo wprowadza ryzyko mylnego wrażenia co do zakresu API.


Discussion:
wybaczcie nowicjuszowi w django, ale czy to jest aż tak proste, czy jednak to dirty hack? :) 
Mogę się tym zająć

Resolving patch:
diff --git a/backend-project/config/urls.py b/backend-project/config/urls.py
--- a/backend-project/config/urls.py
+++ b/backend-project/config/urls.py
@@ -13,6 +13,9 @@
     1. Import the include() function: from django.urls import include, path
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
+
+import re
+
 from django.conf import settings
 from django.conf.urls.static import static
 from django.contrib import admin
@@ -29,13 +32,56 @@
 
 from .swagger import info
 
-router = routers.DefaultRouter()
+
+class BetterDefaultRouter(routers.DefaultRouter):
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.include_urls = []
+        self.api_root_dict = {}
+
+    def get_urls(self):
+        urls = super().get_urls()
+        urls.extend(self.include_urls)
+        return urls
+
+    def include(self, module):
+        urlpatterns = getattr(include(module)[0], "urlpatterns")
+        viewnames = set()
+        for urlpattern in urlpatterns:
+            self.include_urls.append(urlpattern)
+            if hasattr(urlpattern, "url_patterns"):
+                viewnames.update([pattern.name for pattern in urlpattern.url_patterns])
+            elif hasattr(urlpattern, "name"):
+                viewnames.add(urlpattern.name)
+        self.api_root_dict.update(
+            {re.sub(r"-list$", "", viewname): viewname for viewname in viewnames}
+        )
+
+    def get_api_root_view(self, api_urls=None):
+        api_root_dict = {}
+        list_name = self.routes[0].name
+
+        for prefix, viewset, basename in self.registry:
+            api_root_dict[prefix] = list_name.format(basename=basename)
+        api_root_dict.update(self.api_root_dict)
+
+        return self.APIRootView.as_view(api_root_dict=api_root_dict)
+
+
+router = BetterDefaultRouter()
+
 router.register(r"channels", ChannelViewSet)
 router.register(r"events", EventViewSet)
 router.register(r"institutions", InstitutionViewSet)
 router.register(r"notes", NoteViewSet)
 router.register(r"tags", TagViewSet)
 router.register(r"users", UserViewSet)
+router.include("small_eod.cases.urls")
+router.include("small_eod.features.urls")
+router.include("small_eod.collections.urls")
+router.include("small_eod.letters.urls")
+router.include("small_eod.administrative_units.urls")
+router.include("small_eod.autocomplete.urls")
 
 schema_view = get_schema_view(
     info,
@@ -46,12 +92,6 @@
 
 urlpatterns = [
     path("admin/", admin.site.urls),
-    path("api/", include("small_eod.collections.urls")),
-    path("api/", include("small_eod.cases.urls")),
-    path("api/", include("small_eod.letters.urls")),
-    path("api/", include("small_eod.features.urls")),
-    path("api/", include("small_eod.administrative_units.urls")),
-    path("api/", include("small_eod.autocomplete.urls")),
     path("api/docs/", schema_view.with_ui("swagger"), name="api_docs"),
     path("api/redoc/", schema_view.with_ui("redoc"), name="api_redocs"),
     re_path(
@@ -62,7 +102,6 @@
     path("api/", include(router.urls)),
 ]
 
-
 if settings.DEBUG:
     import debug_toolbar
 


Test patch: