Source code for django_aws_api_gateway_websockets.mixins
import sys
from django.urls import resolve
from django_aws_api_gateway_websockets.models import ApiGatewayAdditionalRoute
[docs]
class AddWebSocketRouteToContextMixin:
"""Adds the APIGateway Route to the Content"""
websocket_route_key = (
"default" # Ensure you override this if you are not using the default route
)
channel_name = ""
[docs]
def get_context_data(self, **kwargs) -> dict:
"""Adds the Websocket route connection to the context"""
context = super().get_context_data(**kwargs)
context["api_gateway_route"] = (
ApiGatewayAdditionalRoute.objects.filter(route_key=self.websocket_route_key)
.select_related("api_gateway")
.first()
)
context["channel_name"] = self.channel_name
return context
[docs]
class AppChannelWebSocketMixin(AddWebSocketRouteToContextMixin):
"""Adds the websocket setup where the route key and channel name are the name of the app containing the view
that extends this class
"""
request = None
app_channel_override = None # If set overrides the default channel_name
[docs]
def get_context_data(self, **kwargs) -> dict:
app_name = sys.modules[
resolve(self.request.path_info).func.__module__
].__package__
self.channel_name = app_name
self.websocket_route_key = app_name
if self.app_channel_override:
self.channel_name = self.app_channel_override
return super().get_context_data(**kwargs)