Skip to content

Commit 45883d0

Browse files
committed
Initial work for supporting ASGI. adamghill#19
1 parent 52b974d commit 45883d0

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

django_unicorn/templates/unicorn/scripts.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
<script src="{% static 'js/unicorn.min.js' %}"></script>
55

66
<script>
7+
{% if IS_ASGI %}
8+
var url = "{% url 'django_unicorn:message_async' %}";
9+
{% else %}
710
var url = "{% url 'django_unicorn:message' %}";
11+
{% endif %}
812
Unicorn.init(url);
913
</script>
1014
{% else %}
@@ -14,7 +18,11 @@
1418
// Set Unicorn to the global so it can be used by components
1519
window.Unicorn = Unicorn;
1620

21+
{% if IS_ASGI %}
22+
var url = "{% url 'django_unicorn:message_async' %}";
23+
{% else %}
1724
var url = "{% url 'django_unicorn:message' %}";
25+
{% endif %}
1826
Unicorn.init(url);
1927
</script>
2028
{% endif %}

django_unicorn/templatetags/unicorn.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
@register.inclusion_tag("unicorn/scripts.html")
1818
def unicorn_scripts():
19-
return {"MINIFIED": get_setting("MINIFIED", not settings.DEBUG)}
19+
return {
20+
"MINIFIED": get_setting("MINIFIED", not settings.DEBUG),
21+
"IS_ASGI": hasattr(settings, "ASGI_APPLICATION")
22+
and bool(settings.ASGI_APPLICATION)
23+
and (
24+
not hasattr(settings, "WSGI_APPLICATION")
25+
or not bool(settings.WSGI_APPLICATION)
26+
),
27+
}
2028

2129

2230
@register.inclusion_tag("unicorn/errors.html", takes_context=True)

django_unicorn/urls.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
urlpatterns = (
1010
re_path("message/(?P<component_name>[\w/\.-]+)", views.message, name="message"),
11-
path(
12-
"message", views.message, name="message"
13-
), # Only here to build the correct url in scripts.html
11+
re_path(
12+
"message-async/(?P<component_name>[\w/\.-]+)",
13+
views.message_async,
14+
name="message_async",
15+
),
16+
# Only here to build the correct url in scripts.html
17+
path("message", views.message, name="message"),
18+
path("message-async", views.message_async, name="message_async"),
1419
)

django_unicorn/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,9 @@ def message(request: HttpRequest, component_name: str = None) -> JsonResponse:
422422
)
423423

424424
return JsonResponse(res)
425+
426+
427+
async def message_async(
428+
request: HttpRequest, component_name: str = None
429+
) -> JsonResponse:
430+
return message(request, component_name)

example/project/asgi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
from django.core.asgi import get_asgi_application
4+
5+
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
7+
8+
application = get_asgi_application()

example/project/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
]
5353

5454
WSGI_APPLICATION = "project.wsgi.application"
55+
ASGI_APPLICATION = (
56+
"project.asgi.application" # need to comment out WSGI_APPLICATION to enable ASGI
57+
)
5558

5659

5760
DATABASES = {

0 commit comments

Comments
 (0)