Skip to content

Commit

Permalink
Remove uneeded drf_reverse overwrite
Browse files Browse the repository at this point in the history
* `drf_reverse()` was introduced here 1a75b18
* There is a comment about monkey patching. I can't find the monkey patch it is referencing.
* AWX `drf_reverse()` is a copy paste of this https://github.com/encode/django-rest-framework/blob/master/rest_framework/reverse.py#L32
  * The only difference is DRF's version calls `preserve_builtin_query_params()`
    * `preserve_builtin_query_params()` only does something if `api_settings.URL_FORMAT_OVERRIDE` is defined.
      * We don't use `REST_FRAMEWORK.URL_FORMAT_OVERRIDE`
  • Loading branch information
chrismeyersfsu committed Apr 8, 2024
1 parent f1d9966 commit 468949b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
25 changes: 5 additions & 20 deletions awx/api/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# All Rights Reserved.

from django.conf import settings
from django.urls import NoReverseMatch

from rest_framework.reverse import _reverse
from rest_framework.reverse import reverse as drf_reverse
from rest_framework.versioning import URLPathVersioning as BaseVersioning


Expand All @@ -15,25 +14,9 @@ def is_optional_api_urlpattern_prefix_request(request):
return False


def drf_reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
"""
Copy and monkey-patch `rest_framework.reverse.reverse` to prevent adding unwarranted
query string parameters.
"""
scheme = getattr(request, 'versioning_scheme', None)
if scheme is not None:
try:
url = scheme.reverse(viewname, args, kwargs, request, format, **extra)
except NoReverseMatch:
# In case the versioning scheme reversal fails, fallback to the
# default implementation
url = _reverse(viewname, args, kwargs, request, format, **extra)
else:
url = _reverse(viewname, args, kwargs, request, format, **extra)

def transform_optional_api_urlpattern_prefix_url(request, url):
if is_optional_api_urlpattern_prefix_request(request):
url = url.replace('/api', f"/api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}")

return url


Expand All @@ -46,7 +29,9 @@ def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra
kwargs = {}
if 'version' not in kwargs:
kwargs['version'] = settings.REST_FRAMEWORK['DEFAULT_VERSION']
return drf_reverse(viewname, args, kwargs, request, format, **extra)
url = drf_reverse(viewname, args, kwargs, request, format, **extra)

return transform_optional_api_urlpattern_prefix_url(request, url)


class URLPathVersioning(BaseVersioning):
Expand Down
2 changes: 1 addition & 1 deletion awx/main/tests/functional/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework.test import APIRequestFactory

from awx.api.generics import LoggedLoginView
from awx.api.versioning import drf_reverse
from rest_framework.reverse import reverse as drf_reverse


@pytest.mark.django_db
Expand Down
4 changes: 3 additions & 1 deletion awx/main/tests/functional/api/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from django.test.utils import override_settings
from django.utils.encoding import smart_str, smart_bytes

from rest_framework.reverse import reverse as drf_reverse

from awx.main.utils.encryption import decrypt_value, get_encryption_key
from awx.api.versioning import reverse, drf_reverse
from awx.api.versioning import reverse
from awx.main.models.oauth import OAuth2Application as Application, OAuth2AccessToken as AccessToken
from awx.main.tests.functional import immediate_on_commit
from awx.sso.models import UserEnterpriseAuth
Expand Down

0 comments on commit 468949b

Please sign in to comment.