Skip to content

Commit

Permalink
enabling assoiciated serializers to get the parent's args
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Orlov committed Sep 7, 2017
1 parent 163848c commit b428660
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion serializers/__init__.py
@@ -1,2 +1,2 @@
__title__ = 'serializers'
__version__ = '0.2.2'
__version__ = '0.2.3'
8 changes: 4 additions & 4 deletions serializers/association.py
Expand Up @@ -8,8 +8,8 @@ def __init__( self, parent, name, **options ):

#private

def _dict_for( self, item):
return self.serializer( item ).to_dict()
def _dict_for( self, item, args ):
return self.serializer( item, args ).to_dict()

class HasOneAssociation(Association):

Expand All @@ -20,7 +20,7 @@ def value_for( self, item, args ):
if (isinstance( assoc_item, list )):
raise Exception("has_one associations must be applied to a single object, not a list")
# serialize the object and get its to_dict
return self._dict_for( assoc_item )
return self._dict_for( assoc_item, args )

class HasManyAssociation(Association):

Expand All @@ -32,4 +32,4 @@ def value_for( self, items, args ):
raise Exception("has_many associations must be applied to a list")

# serialize the objects and get their to_dict
return [ self._dict_for( item ) for item in assoc_items ]
return [ self._dict_for( item, args ) for item in assoc_items ]
21 changes: 16 additions & 5 deletions tests/__init__.py
Expand Up @@ -23,6 +23,7 @@ def __init__( self ):
self.something = [1, 2, "string"]
self.wonderful = { 'key' : 'value' }
self.here = "poops"
self.base = BaseModel()

def func( self ):
return "lol"
Expand Down Expand Up @@ -69,30 +70,40 @@ def custom_base( cls, item, args = {} ):
HyperComplexSerializer.has_one( 'custom_base', serializer = BaseSerializer ) \
.has_many( 'others', key = 'magic', serializer = OtherSerializer )

class PassthroughArgsMethodizedSerializer( Serializer ):

@classmethod
def pass_through( cls, object, args = {} ):
return args

PassthroughArgsMethodizedSerializer.attribute( 'pass_through' )

class MethodizedSerializer(Serializer):

@classmethod
def custom_field( self, object, args = {} ):
def custom_field( cls, object, args = {} ):
return object.here

@classmethod
def custom_field_with_args( self, object, args = {} ):
def custom_field_with_args( cls, object, args = {} ):
ret = "object.here:" + object.here
if args:
ret += ", args['lol']:" + args.get('lol')

return ret

@classmethod
def keyed_custom_field_with_args( self, object, args = {} ):
def keyed_custom_field_with_args( cls, object, args = {} ):
return "lol"

@classmethod
def func( self, object, args = {} ):
def func( cls, object, args = {} ):
return "overridden func: %s" %( object.func() )

MethodizedSerializer.attributes( 'custom_field',
'custom_field_with_args',
'func' ) \
.attribute( 'keyed_custom_field_with_args', key = 'kek' )
.attribute( 'keyed_custom_field_with_args', key = 'kek' ) \
.has_one( 'base', serializer = PassthroughArgsMethodizedSerializer )


5 changes: 5 additions & 0 deletions tests/test_serializer.py
Expand Up @@ -14,9 +14,14 @@ def test_serializer_can_take_optional_args( self ):
result_dict = MethodizedSerializer( OtherModel(), { 'lol': "thing" } ).to_dict()
assert_equals( "object.here:poops, args['lol']:thing", result_dict['custom_field_with_args'] )

def test_serializer_passes_through_optional_args( self ):
result_dict = MethodizedSerializer( OtherModel(), { 'lol': "thing" } ).to_dict()
assert_equals( "object.here:poops, args['lol']:thing", result_dict['custom_field_with_args'] )

def test_serializer_methods_can_be_keyed( self ):
result_dict = MethodizedSerializer( OtherModel(), { 'lol': "thing" } ).to_dict()
assert_equals( "lol", result_dict['kek'] )
assert_equals( { 'pass_through' : { 'lol': "thing" } }, result_dict['base'] )

def test_serializer_methods_shadow_object_methods( self ):
result_dict = MethodizedSerializer( OtherModel(), { 'lol': "thing" } ).to_dict()
Expand Down

0 comments on commit b428660

Please sign in to comment.