Commit 33f559b0 by Peter Fogg

Remove membership field from teams when serializing memberships.

TNL-3281
parent c82e928b
...@@ -90,6 +90,18 @@ class CourseTeamCreationSerializer(serializers.ModelSerializer): ...@@ -90,6 +90,18 @@ class CourseTeamCreationSerializer(serializers.ModelSerializer):
) )
class CourseTeamSerializerWithoutMembership(CourseTeamSerializer):
"""The same as the `CourseTeamSerializer`, but elides the membership field.
Intended to be used as a sub-serializer for serializing team
memberships, since the membership field is redundant in that case.
"""
def __init__(self, *args, **kwargs):
super(CourseTeamSerializerWithoutMembership, self).__init__(*args, **kwargs)
del self.fields['membership']
class MembershipSerializer(serializers.ModelSerializer): class MembershipSerializer(serializers.ModelSerializer):
"""Serializes CourseTeamMemberships with information about both teams and users.""" """Serializes CourseTeamMemberships with information about both teams and users."""
profile_configuration = deepcopy(settings.ACCOUNT_VISIBILITY_CONFIGURATION) profile_configuration = deepcopy(settings.ACCOUNT_VISIBILITY_CONFIGURATION)
...@@ -112,8 +124,7 @@ class MembershipSerializer(serializers.ModelSerializer): ...@@ -112,8 +124,7 @@ class MembershipSerializer(serializers.ModelSerializer):
view_name='teams_detail', view_name='teams_detail',
read_only=True, read_only=True,
), ),
expanded_serializer=CourseTeamSerializer(read_only=True), expanded_serializer=CourseTeamSerializerWithoutMembership(read_only=True),
exclude_expand_fields={'user'},
) )
class Meta(object): class Meta(object):
......
...@@ -70,10 +70,7 @@ class MembershipSerializerTestCase(SerializerTestCase): ...@@ -70,10 +70,7 @@ class MembershipSerializerTestCase(SerializerTestCase):
'has_image': False 'has_image': False
} }
}) })
self.assertEqual(data['team']['membership'][0]['user'], { self.assertNotIn('membership', data['team'])
'url': 'http://testserver/api/user/v1/accounts/' + username,
'username': username
})
class BaseTopicSerializerTestCase(SerializerTestCase): class BaseTopicSerializerTestCase(SerializerTestCase):
......
...@@ -10,21 +10,18 @@ class ExpandableField(Field): ...@@ -10,21 +10,18 @@ class ExpandableField(Field):
Kwargs: Kwargs:
collapsed_serializer (Serializer): the serializer to use for a non-expanded representation. collapsed_serializer (Serializer): the serializer to use for a non-expanded representation.
expanded_serializer (Serializer): the serializer to use for an expanded representation. expanded_serializer (Serializer): the serializer to use for an expanded representation.
exclude_expand_fields (set(str)): a set of fields which will not be expanded by sub-serializers.
""" """
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Sets up the ExpandableField with the collapsed and expanded versions of the serializer.""" """Sets up the ExpandableField with the collapsed and expanded versions of the serializer."""
assert 'collapsed_serializer' in kwargs and 'expanded_serializer' in kwargs assert 'collapsed_serializer' in kwargs and 'expanded_serializer' in kwargs
self.collapsed = kwargs.pop('collapsed_serializer') self.collapsed = kwargs.pop('collapsed_serializer')
self.expanded = kwargs.pop('expanded_serializer') self.expanded = kwargs.pop('expanded_serializer')
self.exclude_expand_fields = kwargs.pop('exclude_expand_fields', set())
super(ExpandableField, self).__init__(**kwargs) super(ExpandableField, self).__init__(**kwargs)
def field_to_native(self, obj, field_name): def field_to_native(self, obj, field_name):
"""Converts obj to a native representation, using the expanded serializer if the context requires it.""" """Converts obj to a native representation, using the expanded serializer if the context requires it."""
if 'expand' in self.context and field_name in self.context['expand']: if 'expand' in self.context and field_name in self.context['expand']:
self.expanded.initialize(self, field_name) self.expanded.initialize(self, field_name)
self.expanded.context['expand'] = list(set(self.expanded.context['expand']) - self.exclude_expand_fields)
return self.expanded.field_to_native(obj, field_name) return self.expanded.field_to_native(obj, field_name)
else: else:
self.collapsed.initialize(self, field_name) self.collapsed.initialize(self, field_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment