Skip to content

Commit

Permalink
consolidate urls.pys
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Dec 1, 2023
1 parent 78dd6a8 commit a2f50db
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 110 deletions.
9 changes: 0 additions & 9 deletions addon_service/authorized_storage_account/urls.py

This file was deleted.

32 changes: 0 additions & 32 deletions addon_service/common/jsonapi.py

This file was deleted.

9 changes: 0 additions & 9 deletions addon_service/configured_storage_addon/urls.py

This file was deleted.

9 changes: 0 additions & 9 deletions addon_service/external_storage_service/urls.py

This file was deleted.

9 changes: 0 additions & 9 deletions addon_service/internal_resource/urls.py

This file was deleted.

9 changes: 0 additions & 9 deletions addon_service/internal_user/urls.py

This file was deleted.

65 changes: 65 additions & 0 deletions addon_service/urls.py
Original file line number Diff line number Diff line change
@@ -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}/<pk>/<related_field>/",
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}/<pk>/relationships/<related_field>/",
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,
),
]
34 changes: 1 addition & 33 deletions app/urls.py
Original file line number Diff line number Diff line change
@@ -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")),
]

0 comments on commit a2f50db

Please sign in to comment.