Skip to content

Commit 3975fb3

Browse files
committed
update reverse_content_api to always return an absolute URL
1 parent bcb8b60 commit 3975fb3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

indigo_content_api/reverse.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
reverse certain API URLs and by default Django would try to use the App's URLs, not the API's URLs.
44
"""
55

6+
from urllib.parse import urlparse, urlunparse
7+
68
from django.conf import settings
79
from django.urls import reverse as django_reverse
810
from rest_framework.reverse import reverse as drf_reverse
911

10-
1112
# the host alias that runs the content_api, if django_hosts is in use
1213
CONTENT_API_HOST = 'api'
1314

@@ -23,10 +24,23 @@ def reverse_content_api(*args, **kwargs):
2324
2425
While the base Indigo doesn't enable django_hosts by default, this allows Indigo users to
2526
opt-in to django_hosts and host the API on a separate hostname.
27+
28+
If django_rest_framework returns a non-absolute URL (e.g. because a request isn't given,
29+
and/or django_hosts is not in use), then use settings.INDIGO_URL to build the absolute URL.
2630
"""
2731
if using_django_hosts():
2832
kwargs.setdefault('host', CONTENT_API_HOST)
29-
return drf_reverse(*args, **kwargs)
33+
url = drf_reverse(*args, **kwargs)
34+
35+
parsed_url = urlparse(url)
36+
if not parsed_url.scheme:
37+
parsed_base_url = urlparse(settings.INDIGO_URL)
38+
# combine e.g. http://localhost:8000 with e.g. /api/v3/akn/…
39+
url = urlunparse((
40+
parsed_base_url.scheme, parsed_base_url.netloc,
41+
parsed_url.path, parsed_url.params, parsed_url.query, parsed_url.fragment))
42+
43+
return url
3044

3145

3246
def intercept_drf_reverse():

0 commit comments

Comments
 (0)