3
3
reverse certain API URLs and by default Django would try to use the App's URLs, not the API's URLs.
4
4
"""
5
5
6
+ from urllib .parse import urlparse , urlunparse
7
+
6
8
from django .conf import settings
7
9
from django .urls import reverse as django_reverse
8
10
from rest_framework .reverse import reverse as drf_reverse
9
11
10
-
11
12
# the host alias that runs the content_api, if django_hosts is in use
12
13
CONTENT_API_HOST = 'api'
13
14
@@ -23,10 +24,23 @@ def reverse_content_api(*args, **kwargs):
23
24
24
25
While the base Indigo doesn't enable django_hosts by default, this allows Indigo users to
25
26
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.
26
30
"""
27
31
if using_django_hosts ():
28
32
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
30
44
31
45
32
46
def intercept_drf_reverse ():
0 commit comments