Skip to content

Commit

Permalink
add reverse (#2110)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjkkkjjj committed Aug 5, 2024
1 parent e3e411f commit 3376bb2
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 112 deletions.
22 changes: 22 additions & 0 deletions channels/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.urls.exceptions import Resolver404
from django.urls.resolvers import RegexPattern, RoutePattern, URLResolver, URLPattern
from django.urls import reverse as django_reverse

"""
All Routing instances inside this file are also valid ASGI applications - with
Expand Down Expand Up @@ -234,3 +235,24 @@ async def __call__(self, scope, receive, send):
raise ValueError(
"No application configured for channel name %r" % scope["channel"]
)


def reverse(*args, urlconf=None, **kwargs):
"""reverse wrapper for django's reverse function
Parameters
----------
urlconf : str, optional
The root path of the routings, by default None
See the django's [reverse](https://docs.djangoproject.com/en/5.0/ref/urlresolvers/#reverse)
for more details of the other arguments
Returns
-------
str
The reversed url
"""
if urlconf is None:
urlconf = settings.ROOT_WEBSOCKET_URLCONF
return django_reverse(*args, urlconf=urlconf, **kwargs)
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import sys
from django.conf import settings


Expand Down Expand Up @@ -38,3 +39,20 @@ def samesite(request, settings):
def samesite_invalid(settings):
"""Set samesite flag to strict."""
settings.SESSION_COOKIE_SAMESITE = "Hello"


@pytest.fixture
def root_urlconf(settings):
"""Set ROOT_WEBSOCKET_URLCONF."""
settings.ROOT_WEBSOCKET_URLCONF = "__src.routings"
return settings.ROOT_WEBSOCKET_URLCONF


@pytest.fixture(autouse=True)
def mock_modules():
"""Save original modules for each test and clear a cache"""
original_modules = sys.modules.copy()
yield
sys.modules = original_modules
from django.urls.base import _get_cached_resolver
_get_cached_resolver.cache_clear()
Loading

0 comments on commit 3376bb2

Please sign in to comment.