mirror of
https://github.com/django/daphne.git
synced 2025-04-20 08:42:18 +03:00
Easy application of decorators to Bindings (#281)
* Added get_handler Added a get_handler method that applies decorators to the consumer-classmethod * added imports for decorators * Added get_handler to WebsocketBinding * Fixed missing import * channel_session_user defaults to True * removed user-transfer from http would only work in a connect-method * removed unused import
This commit is contained in:
parent
2d97ab2cc7
commit
0bc1cee103
|
@ -6,6 +6,7 @@ from django.apps import apps
|
|||
from django.db.models.signals import post_save, post_delete
|
||||
|
||||
from ..channel import Group
|
||||
from ..auth import channel_session, channel_session_user
|
||||
|
||||
|
||||
class BindingMetaclass(type):
|
||||
|
@ -62,6 +63,10 @@ class Binding(object):
|
|||
|
||||
fields = None
|
||||
|
||||
# Decorators
|
||||
channel_session_user = True
|
||||
channel_session = False
|
||||
|
||||
@classmethod
|
||||
def register(cls):
|
||||
"""
|
||||
|
@ -158,7 +163,23 @@ class Binding(object):
|
|||
# Run incoming action
|
||||
self.run_action(self.action, self.pk, self.data)
|
||||
|
||||
consumer = trigger_inbound
|
||||
@classmethod
|
||||
def get_handler(cls):
|
||||
"""
|
||||
Adds decorators to trigger_inbound.
|
||||
"""
|
||||
handler = cls.trigger_inbound
|
||||
if cls.channel_session_user:
|
||||
return channel_session_user(handler)
|
||||
elif cls.channel_session:
|
||||
return channel_session(handler)
|
||||
else:
|
||||
return handler
|
||||
|
||||
@classmethod
|
||||
def consumer(cls, message, **kwargs):
|
||||
handler = cls.get_handler()
|
||||
handler(message, **kwargs)
|
||||
|
||||
def deserialize(self, message):
|
||||
"""
|
||||
|
|
|
@ -5,6 +5,7 @@ from django.core.serializers.json import DjangoJSONEncoder
|
|||
|
||||
from .base import Binding
|
||||
from ..generic.websockets import WebsocketDemultiplexer
|
||||
from ..sessions import enforce_ordering
|
||||
|
||||
|
||||
class WebsocketBinding(Binding):
|
||||
|
@ -32,6 +33,10 @@ class WebsocketBinding(Binding):
|
|||
|
||||
stream = None
|
||||
|
||||
# Decorators
|
||||
strict_ordering = False
|
||||
slight_ordering = False
|
||||
|
||||
# Outbound
|
||||
@classmethod
|
||||
def encode(cls, stream, payload):
|
||||
|
@ -58,6 +63,20 @@ class WebsocketBinding(Binding):
|
|||
return json.loads(data)[0]['fields']
|
||||
|
||||
# Inbound
|
||||
@classmethod
|
||||
def get_handler(cls):
|
||||
"""
|
||||
Adds decorators to trigger_inbound.
|
||||
"""
|
||||
# Get super-handler
|
||||
handler = super(WebsocketBinding, cls).get_handler()
|
||||
# Ordering decorators
|
||||
if cls.strict_ordering:
|
||||
return enforce_ordering(handler, slight=False)
|
||||
elif cls.slight_ordering:
|
||||
return enforce_ordering(handler, slight=True)
|
||||
else:
|
||||
return handler
|
||||
|
||||
def deserialize(self, message):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user