Skip to content

Commit 3376bb2

Browse files
author
jjjkkkjjj
committed
add reverse (#2110)
1 parent e3e411f commit 3376bb2

File tree

3 files changed

+182
-112
lines changed

3 files changed

+182
-112
lines changed

channels/routing.py

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.core.exceptions import ImproperlyConfigured
66
from django.urls.exceptions import Resolver404
77
from django.urls.resolvers import RegexPattern, RoutePattern, URLResolver, URLPattern
8+
from django.urls import reverse as django_reverse
89

910
"""
1011
All Routing instances inside this file are also valid ASGI applications - with
@@ -234,3 +235,24 @@ async def __call__(self, scope, receive, send):
234235
raise ValueError(
235236
"No application configured for channel name %r" % scope["channel"]
236237
)
238+
239+
240+
def reverse(*args, urlconf=None, **kwargs):
241+
"""reverse wrapper for django's reverse function
242+
243+
Parameters
244+
----------
245+
urlconf : str, optional
246+
The root path of the routings, by default None
247+
248+
See the django's [reverse](https://docs.djangoproject.com/en/5.0/ref/urlresolvers/#reverse)
249+
for more details of the other arguments
250+
251+
Returns
252+
-------
253+
str
254+
The reversed url
255+
"""
256+
if urlconf is None:
257+
urlconf = settings.ROOT_WEBSOCKET_URLCONF
258+
return django_reverse(*args, urlconf=urlconf, **kwargs)

tests/conftest.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import sys
23
from django.conf import settings
34

45

@@ -38,3 +39,20 @@ def samesite(request, settings):
3839
def samesite_invalid(settings):
3940
"""Set samesite flag to strict."""
4041
settings.SESSION_COOKIE_SAMESITE = "Hello"
42+
43+
44+
@pytest.fixture
45+
def root_urlconf(settings):
46+
"""Set ROOT_WEBSOCKET_URLCONF."""
47+
settings.ROOT_WEBSOCKET_URLCONF = "__src.routings"
48+
return settings.ROOT_WEBSOCKET_URLCONF
49+
50+
51+
@pytest.fixture(autouse=True)
52+
def mock_modules():
53+
"""Save original modules for each test and clear a cache"""
54+
original_modules = sys.modules.copy()
55+
yield
56+
sys.modules = original_modules
57+
from django.urls.base import _get_cached_resolver
58+
_get_cached_resolver.cache_clear()

0 commit comments

Comments
 (0)