Merge pull request #261 from AlexejStukov/patch-8

move encoding from serialize to trigger_outbound
This commit is contained in:
Andrew Godwin 2016-07-21 11:30:48 -04:00 committed by GitHub
commit 435fd89be8
2 changed files with 19 additions and 9 deletions

View File

@ -93,6 +93,13 @@ class Binding(object):
"""
cls.trigger_outbound(instance, "delete")
@classmethod
def encode(cls, stream, payload):
"""
Encodes stream + payload for outbound sending.
"""
raise NotImplementedError()
@classmethod
def trigger_outbound(cls, instance, action):
"""
@ -101,9 +108,13 @@ class Binding(object):
self = cls()
self.instance = instance
# Check to see if we're covered
for group_name in self.group_names(instance, action):
group = Group(group_name)
group.send(self.serialize(instance, action))
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):
group = Group(group_name)
group.send(message)
def group_names(self, instance, action):
"""
@ -115,9 +126,7 @@ class Binding(object):
def serialize(self, instance, action):
"""
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 -
e.g., to send JSON as a WebSocket text frame, you must return
{"text": json.dumps(instance_serialized_as_dict)}
wire (e.g. {"pk": 12, "value": 42, "string": "some string"})
"""
raise NotImplementedError()

View File

@ -32,6 +32,9 @@ class WebsocketBinding(Binding):
stream = None
# Outbound
@classmethod
def encode(cls, stream, payload):
return WebsocketDemultiplexer.encode(stream, payload)
def serialize(self, instance, action):
payload = {
@ -40,9 +43,7 @@ class WebsocketBinding(Binding):
"data": self.serialize_data(instance),
"model": self.model_label,
}
# Encode for the stream
assert self.stream is not None
return WebsocketDemultiplexer.encode(self.stream, payload)
return payload
def serialize_data(self, instance):
"""