From a2f50db482aa3e0884350cdeebaf02d8347c3248 Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Fri, 1 Dec 2023 08:36:26 -0500 Subject: [PATCH] consolidate urls.pys --- .../authorized_storage_account/urls.py | 9 --- addon_service/common/jsonapi.py | 32 --------- .../configured_storage_addon/urls.py | 9 --- .../external_storage_service/urls.py | 9 --- addon_service/internal_resource/urls.py | 9 --- addon_service/internal_user/urls.py | 9 --- addon_service/urls.py | 65 +++++++++++++++++++ app/urls.py | 34 +--------- 8 files changed, 66 insertions(+), 110 deletions(-) delete mode 100644 addon_service/authorized_storage_account/urls.py delete mode 100644 addon_service/common/jsonapi.py delete mode 100644 addon_service/configured_storage_addon/urls.py delete mode 100644 addon_service/external_storage_service/urls.py delete mode 100644 addon_service/internal_resource/urls.py delete mode 100644 addon_service/internal_user/urls.py create mode 100644 addon_service/urls.py diff --git a/addon_service/authorized_storage_account/urls.py b/addon_service/authorized_storage_account/urls.py deleted file mode 100644 index 398875dd..00000000 --- a/addon_service/authorized_storage_account/urls.py +++ /dev/null @@ -1,9 +0,0 @@ -from addon_service.common.jsonapi import urls_for_viewset - -from . import views - - -urlpatterns = urls_for_viewset( - views.AuthorizedStorageAccountViewSet, - views.AuthorizedStorageAccountRelationshipView, -) diff --git a/addon_service/common/jsonapi.py b/addon_service/common/jsonapi.py deleted file mode 100644 index e7df573b..00000000 --- a/addon_service/common/jsonapi.py +++ /dev/null @@ -1,32 +0,0 @@ -from django.urls import path -from rest_framework.routers import SimpleRouter -from rest_framework_json_api.utils import get_resource_type_from_serializer - - -def urls_for_viewset(viewset, relationship_view=None, additional_urlpatterns=()): - _resource_name = get_resource_type_from_serializer(viewset.serializer_class) - _router = SimpleRouter() - _router.register( - prefix=_resource_name, - viewset=viewset, - basename=_resource_name, - ) - _urlpatterns = [*_router.urls] - _urlpatterns.append( - path( - f"{_resource_name}///", - viewset.as_view({"get": "retrieve_related"}), - name=f"{_resource_name}-related", - ), - ) - if relationship_view is not None: - _urlpatterns.append( - path( - f"{_resource_name}//relationships//", - relationship_view.as_view(), - name=f"{_resource_name}-relationships", - ), - ) - - # _urlpatterns.extend(additional_urlpatterns) - return _urlpatterns diff --git a/addon_service/configured_storage_addon/urls.py b/addon_service/configured_storage_addon/urls.py deleted file mode 100644 index 14da03ed..00000000 --- a/addon_service/configured_storage_addon/urls.py +++ /dev/null @@ -1,9 +0,0 @@ -from addon_service.common.jsonapi import urls_for_viewset - -from . import views - - -urlpatterns = urls_for_viewset( - views.ConfiguredStorageAddonViewSet, - views.ConfiguredStorageAddonRelationshipView, -) diff --git a/addon_service/external_storage_service/urls.py b/addon_service/external_storage_service/urls.py deleted file mode 100644 index 155b27fd..00000000 --- a/addon_service/external_storage_service/urls.py +++ /dev/null @@ -1,9 +0,0 @@ -from addon_service.common.jsonapi import urls_for_viewset - -from . import views - - -urlpatterns = urls_for_viewset( - views.ExternalStorageServiceViewSet, - views.ExternalStorageServiceRelationshipView, -) diff --git a/addon_service/internal_resource/urls.py b/addon_service/internal_resource/urls.py deleted file mode 100644 index 84b76ff6..00000000 --- a/addon_service/internal_resource/urls.py +++ /dev/null @@ -1,9 +0,0 @@ -from addon_service.common.jsonapi import urls_for_viewset - -from . import views - - -urlpatterns = urls_for_viewset( - views.InternalResourceViewSet, - views.InternalResourceRelationshipView, -) diff --git a/addon_service/internal_user/urls.py b/addon_service/internal_user/urls.py deleted file mode 100644 index 6235296a..00000000 --- a/addon_service/internal_user/urls.py +++ /dev/null @@ -1,9 +0,0 @@ -from addon_service.common.jsonapi import urls_for_viewset - -from . import views - - -urlpatterns = urls_for_viewset( - views.InternalUserViewSet, - views.InternalUserRelationshipView, -) diff --git a/addon_service/urls.py b/addon_service/urls.py new file mode 100644 index 00000000..68157367 --- /dev/null +++ b/addon_service/urls.py @@ -0,0 +1,65 @@ +from django.urls import path +from rest_framework.routers import SimpleRouter +from rest_framework_json_api.utils import get_resource_type_from_serializer + +from addon_service import views + + +def _urls_for_viewset(viewset, *, relationship_view=None): + """returns urlpatterns for a viewset that corresponds to a resource type + + includes patterns for jsonapi-style relationships + """ + _resource_name = get_resource_type_from_serializer(viewset.serializer_class) + _router = SimpleRouter() + _router.register( + prefix=_resource_name, + viewset=viewset, + basename=_resource_name, + ) + _urlpatterns = [*_router.urls] + # add route for all relationship "related" links + # https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#related-urls + _urlpatterns.append( + path( + f"{_resource_name}///", + viewset.as_view({"get": "retrieve_related"}), + name=f"{_resource_name}-related", + ), + ) + if relationship_view is not None: + # add route for all relationship "self" links + # https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#relationshipview + _urlpatterns.append( + path( + f"{_resource_name}//relationships//", + relationship_view.as_view(), + name=f"{_resource_name}-relationships", + ), + ) + return _urlpatterns + + +# NOTE: assumes each viewset corresponds to a distinct resource_name +urlpatterns = [ + *_urls_for_viewset( + views.AuthorizedStorageAccountViewSet, + relationship_view=views.AuthorizedStorageAccountRelationshipView, + ), + *_urls_for_viewset( + views.ConfiguredStorageAddonViewSet, + relationship_view=views.ConfiguredStorageAddonRelationshipView, + ), + *_urls_for_viewset( + views.ExternalStorageServiceViewSet, + relationship_view=views.ExternalStorageServiceRelationshipView, + ), + *_urls_for_viewset( + views.InternalResourceViewSet, + relationship_view=views.InternalResourceRelationshipView, + ), + *_urls_for_viewset( + views.InternalUserViewSet, + relationship_view=views.InternalUserRelationshipView, + ), +] diff --git a/app/urls.py b/app/urls.py index d0ac0481..d8f02233 100644 --- a/app/urls.py +++ b/app/urls.py @@ -1,41 +1,9 @@ -"""gravyvalet URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/3.1/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" from django.urls import ( include, path, ) -from addon_service.authorized_storage_account import urls as asa_urls -from addon_service.configured_storage_addon import urls as csa_urls -from addon_service.external_storage_service import urls as ess_urls -from addon_service.internal_resource import urls as ir_urls -from addon_service.internal_user import urls as iu_urls - urlpatterns = [ - path( - "v1/", - include( - [ - *asa_urls.urlpatterns, - *csa_urls.urlpatterns, - *ess_urls.urlpatterns, - *ir_urls.urlpatterns, - *iu_urls.urlpatterns, - ] - ), - ), + path("v1/", include("addon_service.urls")), ]