-
Notifications
You must be signed in to change notification settings - Fork 143
Added find_api_root() to standardize URL building-blocks. #7556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added `pulpcore.app.find_url.find_api_root()` to standardize Pulp url-creation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from django.conf import settings | ||
|
|
||
| DOMAIN_SLUG = "<slug:pulp_domain>" | ||
| REWRITE_SLUG = "/<path:api_root>/" | ||
|
|
||
|
|
||
| # We isolate this utility-function to avoid pulling in random "Django App must be configured" | ||
| # code segments from the rest of the .util methods/imports | ||
| def find_api_root(version="v3", set_domain=True, domain=None, lstrip=False, rewrite_header=True): | ||
| """ | ||
| Returns the tuple (api-root, <root>/api/<version>) | ||
|
|
||
| Args: | ||
| version (str): API-version desired | ||
| set_domain (bool): Should the domain-slug be included if DOMAIN_ENABLED? | ||
| domain (str): Domain-name to replace DOMAIN_SLUG | ||
| lstrip (bool): Should the full version have it's leading-/ stripped | ||
| rewrite_header (bool): Should API_ROOT_REWRITE_HEADER be honored or not | ||
|
|
||
| Examples: | ||
| find_api_root() : ("/pulp/", "/pulp/api/v3/") | ||
| find_api_root(), DOMAIN_ENABLED: ("/pulp/", "/pulp/<slug:pulp_domain>/api/v3/") | ||
| find_api_root(domain="default"), DOMAIN_ENABLED : ("/pulp/", "/pulp/default/api/v3/") | ||
| find_api_root(lstrip=True) : ("pulp/", "pulp/api/v3/") | ||
| find_api_root(), API_ROOT_REWRITE_HEADER: ("/<path:api_root>/", "/<path:api_root>/api/v3/") | ||
| find_api_root(version="v4", domain="foo", lstrip=True), API_ROOT_REWRITE_HEADER : | ||
| ("<path:api_root>/", "<path:api_root>/default/api/v4/") | ||
| Returns: | ||
| (str, str) : (API_ROOT (possibly re-written), API_ROOT/api/<version>/ | ||
| (with <domain> if enabled)) | ||
| """ | ||
| # Some current path-building wants to ignore REWRITE - make that possible | ||
| if rewrite_header and settings.API_ROOT_REWRITE_HEADER: | ||
| root = REWRITE_SLUG | ||
| else: | ||
| root = settings.API_ROOT | ||
|
|
||
| # Some current path-building wants to ignore DOMAIN - make that possible | ||
| if set_domain and settings.DOMAIN_ENABLED: | ||
| if domain: | ||
| path = f"{root}{domain}/api/{version}/" | ||
| else: | ||
| path = f"{root}{DOMAIN_SLUG}/api/{version}/" | ||
| else: | ||
| path = f"{root}api/{version}/" | ||
| if lstrip: | ||
| return root.lstrip("/"), path.lstrip("/") | ||
| else: | ||
| return root, path |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| from django.template.defaultfilters import stringfilter, urlize as orig_urlize, register | ||
| from django.conf import settings | ||
| from django.template.defaultfilters import stringfilter, urlize as orig_urlize, register | ||
| from django.utils.safestring import SafeData, mark_safe | ||
|
|
||
|
|
||
|
|
@@ -11,13 +11,14 @@ def urlize(text, autoescape=True): | |
|
|
||
| This filter overwrites the django default implementation to also handle pulp api hrefs. | ||
| """ | ||
| current_root = settings.API_ROOT + "api/" | ||
| safe_input = isinstance(text, SafeData) | ||
| text = text.replace(settings.V3_API_ROOT, "http://SENTINEL.org" + settings.V3_API_ROOT) | ||
| text = text.replace(current_root, "http://SENTINEL.org" + current_root) | ||
| if safe_input: | ||
| text = mark_safe(text) | ||
| text = orig_urlize(text, autoescape=autoescape) | ||
| safe_input = isinstance(text, SafeData) | ||
| text = text.replace("http://SENTINEL.org" + settings.V3_API_ROOT, settings.V3_API_ROOT) | ||
| text = text.replace("http://SENTINEL.org" + current_root, current_root) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like the whole dance going on here (which was already the case btw, so not specific to your PR) is under-documented. The purpose of slotting in |
||
| if safe_input: | ||
| text = mark_safe(text) | ||
| return text | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| from rest_framework.routers import APIRootView | ||
|
|
||
| from pulpcore.app.apps import pulp_plugin_configs | ||
| from pulpcore.plugin.find_url import find_api_root | ||
| from pulpcore.app.views import ( | ||
| LivezView, | ||
| OrphansView, | ||
|
|
@@ -30,14 +31,9 @@ | |
| ReclaimSpaceViewSet, | ||
| ) | ||
|
|
||
| if settings.DOMAIN_ENABLED: | ||
| API_ROOT = settings.V3_DOMAIN_API_ROOT_NO_FRONT_SLASH | ||
| else: | ||
| API_ROOT = settings.V3_API_ROOT_NO_FRONT_SLASH | ||
| if settings.API_ROOT_REWRITE_HEADER: | ||
| V3_API_ROOT = settings.V3_API_ROOT.replace("/<path:api_root>/", settings.API_ROOT) | ||
| else: | ||
| V3_API_ROOT = settings.V3_API_ROOT | ||
| _, PATH_DOMAIN_REWRITE_NFS = find_api_root(lstrip=True, set_domain=True, rewrite_header=True) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NFS seems like a potentially misleading abbreviation for no-front-slash |
||
| _, PATH_NODOMAIN_NOREWRITE_NFS = find_api_root(lstrip=True, set_domain=False, rewrite_header=False) | ||
| _, PATH_NODOMAIN_REWRITE_NFS = find_api_root(lstrip=True, set_domain=False, rewrite_header=True) | ||
|
|
||
|
|
||
| class ViewSetNode: | ||
|
|
@@ -203,7 +199,7 @@ class PulpDefaultRouter(routers.DefaultRouter): | |
| SpectacularRedocView.as_view( | ||
| authentication_classes=[], | ||
| permission_classes=[], | ||
| url=f"{V3_API_ROOT}docs/api.json?include_html=1&pk_path=1", | ||
| url=f"/{PATH_NODOMAIN_NOREWRITE_NFS}docs/api.json?include_html=1&pk_path=1", | ||
| ) | ||
| ), | ||
| name="schema-redoc", | ||
|
|
@@ -214,17 +210,18 @@ class PulpDefaultRouter(routers.DefaultRouter): | |
| SpectacularSwaggerView.as_view( | ||
| authentication_classes=[], | ||
| permission_classes=[], | ||
| url=f"{V3_API_ROOT}docs/api.json?include_html=1&pk_path=1", | ||
| url=f"/{PATH_NODOMAIN_NOREWRITE_NFS}docs/api.json?include_html=1&pk_path=1", | ||
| ) | ||
| ), | ||
| name="schema-swagger", | ||
| ), | ||
| ] | ||
|
|
||
| urlpatterns = [ | ||
| path(API_ROOT, include(special_views)), | ||
| path(PATH_DOMAIN_REWRITE_NFS, include(special_views)), | ||
| path("auth/", include("rest_framework.urls")), | ||
| path(settings.V3_API_ROOT_NO_FRONT_SLASH, include(docs_and_status)), | ||
| # docs/status aren't "inside" a domain | ||
| path(PATH_NODOMAIN_REWRITE_NFS, include(docs_and_status)), | ||
| ] | ||
|
|
||
| if settings.DOMAIN_ENABLED: | ||
|
|
@@ -239,7 +236,7 @@ class NoSchema(p.callback.cls): | |
| view = NoSchema.as_view(**p.callback.initkwargs) | ||
| name = p.name + "-domains" if p.name else None | ||
| docs_and_status_no_schema.append(path(str(p.pattern), view, name=name)) | ||
| urlpatterns.insert(-1, path(API_ROOT, include(docs_and_status_no_schema))) | ||
| urlpatterns.insert(-1, path(PATH_DOMAIN_REWRITE_NFS, include(docs_and_status_no_schema))) | ||
|
|
||
| if "social_django" in settings.INSTALLED_APPS: | ||
| urlpatterns.append( | ||
|
|
@@ -251,7 +248,7 @@ class NoSchema(p.callback.cls): | |
|
|
||
| all_routers = [root_router] + vs_tree.register_with(root_router) | ||
| for router in all_routers: | ||
| urlpatterns.append(path(API_ROOT, include(router.urls))) | ||
| urlpatterns.append(path(PATH_DOMAIN_REWRITE_NFS, include(router.urls))) | ||
|
|
||
| # If plugins define a urls.py, include them into the root namespace. | ||
| for plugin_pattern in plugin_patterns: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from pulpcore.app.find_url import find_api_root | ||
|
|
||
| __all__ = ["find_api_root"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a place where using the function isn't possible?