From 45883d096a28b76a4f6511cb79c48728072c0828 Mon Sep 17 00:00:00 2001 From: Adam Hill Date: Sat, 23 Jan 2021 10:12:08 -0500 Subject: [PATCH] Initial work for supporting ASGI. https://github.com/adamghill/django-unicorn/issues/19 --- django_unicorn/templates/unicorn/scripts.html | 8 ++++++++ django_unicorn/templatetags/unicorn.py | 10 +++++++++- django_unicorn/urls.py | 11 ++++++++--- django_unicorn/views.py | 6 ++++++ example/project/asgi.py | 8 ++++++++ example/project/settings.py | 3 +++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 example/project/asgi.py diff --git a/django_unicorn/templates/unicorn/scripts.html b/django_unicorn/templates/unicorn/scripts.html index 2a5a5587..f4ec1569 100644 --- a/django_unicorn/templates/unicorn/scripts.html +++ b/django_unicorn/templates/unicorn/scripts.html @@ -4,7 +4,11 @@ {% else %} @@ -14,7 +18,11 @@ // Set Unicorn to the global so it can be used by components window.Unicorn = Unicorn; + {% if IS_ASGI %} + var url = "{% url 'django_unicorn:message_async' %}"; + {% else %} var url = "{% url 'django_unicorn:message' %}"; + {% endif %} Unicorn.init(url); {% endif %} diff --git a/django_unicorn/templatetags/unicorn.py b/django_unicorn/templatetags/unicorn.py index 9b9ac5be..2a01e07f 100644 --- a/django_unicorn/templatetags/unicorn.py +++ b/django_unicorn/templatetags/unicorn.py @@ -16,7 +16,15 @@ @register.inclusion_tag("unicorn/scripts.html") def unicorn_scripts(): - return {"MINIFIED": get_setting("MINIFIED", not settings.DEBUG)} + return { + "MINIFIED": get_setting("MINIFIED", not settings.DEBUG), + "IS_ASGI": hasattr(settings, "ASGI_APPLICATION") + and bool(settings.ASGI_APPLICATION) + and ( + not hasattr(settings, "WSGI_APPLICATION") + or not bool(settings.WSGI_APPLICATION) + ), + } @register.inclusion_tag("unicorn/errors.html", takes_context=True) diff --git a/django_unicorn/urls.py b/django_unicorn/urls.py index c11cf25f..4e26bd97 100644 --- a/django_unicorn/urls.py +++ b/django_unicorn/urls.py @@ -8,7 +8,12 @@ urlpatterns = ( re_path("message/(?P[\w/\.-]+)", views.message, name="message"), - path( - "message", views.message, name="message" - ), # Only here to build the correct url in scripts.html + re_path( + "message-async/(?P[\w/\.-]+)", + views.message_async, + name="message_async", + ), + # Only here to build the correct url in scripts.html + path("message", views.message, name="message"), + path("message-async", views.message_async, name="message_async"), ) diff --git a/django_unicorn/views.py b/django_unicorn/views.py index cfba2265..97b69a72 100644 --- a/django_unicorn/views.py +++ b/django_unicorn/views.py @@ -422,3 +422,9 @@ def message(request: HttpRequest, component_name: str = None) -> JsonResponse: ) return JsonResponse(res) + + +async def message_async( + request: HttpRequest, component_name: str = None +) -> JsonResponse: + return message(request, component_name) diff --git a/example/project/asgi.py b/example/project/asgi.py new file mode 100644 index 00000000..5de670bc --- /dev/null +++ b/example/project/asgi.py @@ -0,0 +1,8 @@ +import os + +from django.core.asgi import get_asgi_application + + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + +application = get_asgi_application() diff --git a/example/project/settings.py b/example/project/settings.py index 51cd40de..6a7cdf04 100644 --- a/example/project/settings.py +++ b/example/project/settings.py @@ -52,6 +52,9 @@ ] WSGI_APPLICATION = "project.wsgi.application" +ASGI_APPLICATION = ( + "project.asgi.application" # need to comment out WSGI_APPLICATION to enable ASGI +) DATABASES = {