From 54ec9b8cbc2630d32fff817cf2107d2411cfb426 Mon Sep 17 00:00:00 2001 From: Cody Parker Date: Thu, 25 Feb 2016 14:08:13 -0600 Subject: [PATCH 1/2] added in serialization of properties --- dddp/api.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dddp/api.py b/dddp/api.py index 0e77f47..36ff800 100644 --- a/dddp/api.py +++ b/dddp/api.py @@ -494,6 +494,13 @@ class Collection(APIMixin): fields['%s_ids' % field.name] = get_meteor_ids( field.rel.to, fields.pop(field.name), ).values() + + # run serialization for all properties + for o in obj.__class__.mro(): + for attr in o.__dict__: + if isinstance(o.__dict__[attr], property): + val = o.__dict__[attr].__get__(obj) + fields[attr] = val return data def obj_change_as_msg(self, obj, msg, meteor_ids=None): From 291b3b882f6f0d0a6d65292d285ea17526f32ecd Mon Sep 17 00:00:00 2001 From: Cody Parker Date: Thu, 25 Feb 2016 15:33:21 -0600 Subject: [PATCH 2/2] changed decorator to custom ddp_property --- dddp/api.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/dddp/api.py b/dddp/api.py index 36ff800..50d3af7 100644 --- a/dddp/api.py +++ b/dddp/api.py @@ -94,6 +94,22 @@ class Array(aggregates.Aggregate): return value +def ddp_property(method): + """ + Simple decorator that adds a "serialize_ddp" attribute to tell the + serializer to include it in the payload returning to the client + + Args: + method: the decorated method + + Returns: + Callable: Decorated method with new attribute + """ + method.serialize_ddp = True + + return method + + def api_endpoint(path_or_func=None, decorate=True): """ Decorator to mark a method as an API endpoint for later registration. @@ -495,11 +511,11 @@ class Collection(APIMixin): field.rel.to, fields.pop(field.name), ).values() - # run serialization for all properties + # run serialization for all methods decoratored with ddp_property for o in obj.__class__.mro(): for attr in o.__dict__: - if isinstance(o.__dict__[attr], property): - val = o.__dict__[attr].__get__(obj) + if hasattr(o.__dict__[attr], 'serialize_ddp'): + val = o.__dict__[attr].__call__(obj) fields[attr] = val return data