mirror of
https://github.com/django/daphne.git
synced 2025-07-11 08:22:17 +03:00
Merge pull request #261 from AlexejStukov/patch-8
move encoding from serialize to trigger_outbound
This commit is contained in:
commit
435fd89be8
|
@ -93,6 +93,13 @@ class Binding(object):
|
||||||
"""
|
"""
|
||||||
cls.trigger_outbound(instance, "delete")
|
cls.trigger_outbound(instance, "delete")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def encode(cls, stream, payload):
|
||||||
|
"""
|
||||||
|
Encodes stream + payload for outbound sending.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def trigger_outbound(cls, instance, action):
|
def trigger_outbound(cls, instance, action):
|
||||||
"""
|
"""
|
||||||
|
@ -101,9 +108,13 @@ class Binding(object):
|
||||||
self = cls()
|
self = cls()
|
||||||
self.instance = instance
|
self.instance = instance
|
||||||
# Check to see if we're covered
|
# Check to see if we're covered
|
||||||
|
payload = self.serialize(instance, action)
|
||||||
|
if payload != {}:
|
||||||
|
assert self.stream is not None
|
||||||
|
message = cls.encode(self.stream, payload)
|
||||||
for group_name in self.group_names(instance, action):
|
for group_name in self.group_names(instance, action):
|
||||||
group = Group(group_name)
|
group = Group(group_name)
|
||||||
group.send(self.serialize(instance, action))
|
group.send(message)
|
||||||
|
|
||||||
def group_names(self, instance, action):
|
def group_names(self, instance, action):
|
||||||
"""
|
"""
|
||||||
|
@ -115,9 +126,7 @@ class Binding(object):
|
||||||
def serialize(self, instance, action):
|
def serialize(self, instance, action):
|
||||||
"""
|
"""
|
||||||
Should return a serialized version of the instance to send over the
|
Should return a serialized version of the instance to send over the
|
||||||
wire (return value must be a dict suitable for sending over a channel -
|
wire (e.g. {"pk": 12, "value": 42, "string": "some string"})
|
||||||
e.g., to send JSON as a WebSocket text frame, you must return
|
|
||||||
{"text": json.dumps(instance_serialized_as_dict)}
|
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,9 @@ class WebsocketBinding(Binding):
|
||||||
stream = None
|
stream = None
|
||||||
|
|
||||||
# Outbound
|
# Outbound
|
||||||
|
@classmethod
|
||||||
|
def encode(cls, stream, payload):
|
||||||
|
return WebsocketDemultiplexer.encode(stream, payload)
|
||||||
|
|
||||||
def serialize(self, instance, action):
|
def serialize(self, instance, action):
|
||||||
payload = {
|
payload = {
|
||||||
|
@ -40,9 +43,7 @@ class WebsocketBinding(Binding):
|
||||||
"data": self.serialize_data(instance),
|
"data": self.serialize_data(instance),
|
||||||
"model": self.model_label,
|
"model": self.model_label,
|
||||||
}
|
}
|
||||||
# Encode for the stream
|
return payload
|
||||||
assert self.stream is not None
|
|
||||||
return WebsocketDemultiplexer.encode(self.stream, payload)
|
|
||||||
|
|
||||||
def serialize_data(self, instance):
|
def serialize_data(self, instance):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user