mirror of
https://github.com/django/daphne.git
synced 2025-10-28 22:37:31 +03:00
29 lines
824 B
Python
29 lines
824 B
Python
import functools
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
from channels import Channel
|
|
|
|
|
|
def view_producer(channel_name):
|
|
"""
|
|
Returns a new view function that actually writes the request to a channel
|
|
and abandons the response (with an exception the Worker will catch)
|
|
"""
|
|
def producing_view(request):
|
|
Channel(channel_name).send(request.channel_encode())
|
|
raise HttpResponse.ResponseLater()
|
|
return producing_view
|
|
|
|
|
|
def view_consumer(func):
|
|
"""
|
|
Decorates a normal Django view to be a channel consumer.
|
|
Does not run any middleware
|
|
"""
|
|
@functools.wraps(func)
|
|
def consumer(message):
|
|
request = HttpRequest.channel_decode(message.content)
|
|
response = func(request)
|
|
message.reply_channel.send(response.channel_encode())
|
|
return func
|