From a4c8602ea1821ce8198a92705d760ec3abfa96e3 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 22 Jul 2016 21:40:51 -0400 Subject: [PATCH] Move fields check to register so it happens on server start --- channels/binding/base.py | 10 ++++++++++ channels/binding/websockets.py | 13 ++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/channels/binding/base.py b/channels/binding/base.py index bd8f9c0..dd35b03 100644 --- a/channels/binding/base.py +++ b/channels/binding/base.py @@ -53,8 +53,15 @@ class Binding(object): and tie that in as a consumer. """ + # Model to serialize + model = None + # Only model fields that are listed in fields should be send by default + # if you want to really send all fields, use fields = ['__all__'] + + fields = None + @classmethod def register(cls): """ @@ -66,6 +73,9 @@ class Binding(object): return else: raise ValueError("You must set the model attribute on Binding %r!" % cls) + # If fields is not defined, raise an error + if cls.fields is None: + raise ValueError("You must set the fields attribute on Binding %r!" % cls) # Optionally resolve model strings if isinstance(cls.model, six.string_types): cls.model = apps.get_model(cls.model) diff --git a/channels/binding/websockets.py b/channels/binding/websockets.py index fcd97ef..c99eb00 100644 --- a/channels/binding/websockets.py +++ b/channels/binding/websockets.py @@ -31,11 +31,6 @@ class WebsocketBinding(Binding): stream = None - # only model fields that are listed in fields should be send by default - # if you want to really send all fields, use fields = ['__all__'] - - fields = [] - # Outbound @classmethod def encode(cls, stream, payload): @@ -55,10 +50,10 @@ class WebsocketBinding(Binding): Serializes model data into JSON-compatible types. """ if self.fields == ['__all__']: - self.fields = None - elif not self.fields: - raise ValueError("You must set the fields attribute on Binding %r!" % self.__class__) - data = serializers.serialize('json', [instance], fields=self.fields) + fields = None + else: + fields = self.fields + data = serializers.serialize('json', [instance], fields=fields) return json.loads(data)[0]['fields'] # Inbound