From 2fbadb52df4c0ba0c6a633f0ef3afc48b9a64a0d Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 12 Jul 2019 12:41:02 +0200 Subject: [PATCH 1/2] test: Add Django channels to Django example app --- tests/integrations/django/myapp/asgi.py | 17 +++++++++++++++++ tests/integrations/django/myapp/manage.py | 10 ++++++++++ tests/integrations/django/myapp/routing.py | 6 ++++++ tests/integrations/django/myapp/settings.py | 5 ++++- tests/integrations/django/myapp/urls.py | 7 +++++++ tests/integrations/django/myapp/views.py | 3 +++ tests/integrations/django/myapp/wsgi.py | 2 +- 7 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/integrations/django/myapp/asgi.py create mode 100644 tests/integrations/django/myapp/manage.py create mode 100644 tests/integrations/django/myapp/routing.py diff --git a/tests/integrations/django/myapp/asgi.py b/tests/integrations/django/myapp/asgi.py new file mode 100644 index 0000000000..8cbfe1099b --- /dev/null +++ b/tests/integrations/django/myapp/asgi.py @@ -0,0 +1,17 @@ +""" +ASGI entrypoint. Configures Django and then runs the application +defined in the ASGI_APPLICATION setting. +""" + +import os +import django +from channels.routing import get_default_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") + +django.setup() + +from sentry_asgi import SentryMiddleware + +application = get_default_application() +application = SentryMiddleware(application) diff --git a/tests/integrations/django/myapp/manage.py b/tests/integrations/django/myapp/manage.py new file mode 100644 index 0000000000..c56bdb2dca --- /dev/null +++ b/tests/integrations/django/myapp/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") + + from django.core.management import execute_from_command_line + +execute_from_command_line(sys.argv) diff --git a/tests/integrations/django/myapp/routing.py b/tests/integrations/django/myapp/routing.py new file mode 100644 index 0000000000..64c674b66e --- /dev/null +++ b/tests/integrations/django/myapp/routing.py @@ -0,0 +1,6 @@ +from channels.http import AsgiHandler +from channels.routing import ProtocolTypeRouter + +application = ProtocolTypeRouter({ + "http": AsgiHandler, +}) diff --git a/tests/integrations/django/myapp/settings.py b/tests/integrations/django/myapp/settings.py index 4182f669a3..5bf2fed2f4 100644 --- a/tests/integrations/django/myapp/settings.py +++ b/tests/integrations/django/myapp/settings.py @@ -95,7 +95,7 @@ def process_response(self, request, response): } ] -WSGI_APPLICATION = "tests.django.myapp.wsgi.application" +WSGI_APPLICATION = "tests.integrations.django.myapp.wsgi.application" # Database @@ -150,3 +150,6 @@ def process_response(self, request, response): # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = "/static/" + +# django-channels specific +ASGI_APPLICATION = 'tests.integrations.django.myapp.routing.application' diff --git a/tests/integrations/django/myapp/urls.py b/tests/integrations/django/myapp/urls.py index 934fa65fae..61a9fa161f 100644 --- a/tests/integrations/django/myapp/urls.py +++ b/tests/integrations/django/myapp/urls.py @@ -49,6 +49,13 @@ name="rest_framework_read_body_and_exc", ) ) + urlpatterns.append( + path( + "rest-hello", + views.rest_hello, + name="rest_hello" + ) + ) except AttributeError: pass diff --git a/tests/integrations/django/myapp/views.py b/tests/integrations/django/myapp/views.py index 58626811d0..cf22810bb3 100644 --- a/tests/integrations/django/myapp/views.py +++ b/tests/integrations/django/myapp/views.py @@ -16,6 +16,9 @@ def rest_framework_read_body_and_exc(request): request.data 1 / 0 + @api_view(["GET"]) + def rest_hello(request): + return HttpResponse("ok") except ImportError: pass diff --git a/tests/integrations/django/myapp/wsgi.py b/tests/integrations/django/myapp/wsgi.py index 298524a800..f6c94a8ff3 100644 --- a/tests/integrations/django/myapp/wsgi.py +++ b/tests/integrations/django/myapp/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") application = get_wsgi_application() From c378a7e79d53e088fe98c6f3abdfaa2efbd90b44 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 12 Jul 2019 14:55:12 +0200 Subject: [PATCH 2/2] fix: Formatting --- tests/integrations/django/myapp/asgi.py | 4 +++- tests/integrations/django/myapp/manage.py | 4 +++- tests/integrations/django/myapp/routing.py | 4 +--- tests/integrations/django/myapp/settings.py | 2 +- tests/integrations/django/myapp/urls.py | 8 +------- tests/integrations/django/myapp/views.py | 1 + tests/integrations/django/myapp/wsgi.py | 4 +++- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/integrations/django/myapp/asgi.py b/tests/integrations/django/myapp/asgi.py index 8cbfe1099b..536753c911 100644 --- a/tests/integrations/django/myapp/asgi.py +++ b/tests/integrations/django/myapp/asgi.py @@ -7,7 +7,9 @@ import django from channels.routing import get_default_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") +os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings" +) django.setup() diff --git a/tests/integrations/django/myapp/manage.py b/tests/integrations/django/myapp/manage.py index c56bdb2dca..d65c90e4ee 100644 --- a/tests/integrations/django/myapp/manage.py +++ b/tests/integrations/django/myapp/manage.py @@ -3,7 +3,9 @@ import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings" + ) from django.core.management import execute_from_command_line diff --git a/tests/integrations/django/myapp/routing.py b/tests/integrations/django/myapp/routing.py index 64c674b66e..796d3d7d56 100644 --- a/tests/integrations/django/myapp/routing.py +++ b/tests/integrations/django/myapp/routing.py @@ -1,6 +1,4 @@ from channels.http import AsgiHandler from channels.routing import ProtocolTypeRouter -application = ProtocolTypeRouter({ - "http": AsgiHandler, -}) +application = ProtocolTypeRouter({"http": AsgiHandler}) diff --git a/tests/integrations/django/myapp/settings.py b/tests/integrations/django/myapp/settings.py index 5bf2fed2f4..d0c47a001d 100644 --- a/tests/integrations/django/myapp/settings.py +++ b/tests/integrations/django/myapp/settings.py @@ -152,4 +152,4 @@ def process_response(self, request, response): STATIC_URL = "/static/" # django-channels specific -ASGI_APPLICATION = 'tests.integrations.django.myapp.routing.application' +ASGI_APPLICATION = "tests.integrations.django.myapp.routing.application" diff --git a/tests/integrations/django/myapp/urls.py b/tests/integrations/django/myapp/urls.py index 61a9fa161f..11cc157101 100644 --- a/tests/integrations/django/myapp/urls.py +++ b/tests/integrations/django/myapp/urls.py @@ -49,13 +49,7 @@ name="rest_framework_read_body_and_exc", ) ) - urlpatterns.append( - path( - "rest-hello", - views.rest_hello, - name="rest_hello" - ) - ) + urlpatterns.append(path("rest-hello", views.rest_hello, name="rest_hello")) except AttributeError: pass diff --git a/tests/integrations/django/myapp/views.py b/tests/integrations/django/myapp/views.py index cf22810bb3..078906d023 100644 --- a/tests/integrations/django/myapp/views.py +++ b/tests/integrations/django/myapp/views.py @@ -20,6 +20,7 @@ def rest_framework_read_body_and_exc(request): def rest_hello(request): return HttpResponse("ok") + except ImportError: pass diff --git a/tests/integrations/django/myapp/wsgi.py b/tests/integrations/django/myapp/wsgi.py index f6c94a8ff3..8c01991e9f 100644 --- a/tests/integrations/django/myapp/wsgi.py +++ b/tests/integrations/django/myapp/wsgi.py @@ -11,6 +11,8 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings") +os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", "tests.integrations.django.myapp.settings" +) application = get_wsgi_application()