class EventSerializer(ModelSerializer): project = ProjectSerializer(read_only=True) actor = UserSerializer() patch = PatchSerializer(read_only=True) series = SeriesSerializer(read_only=True) cover = CoverLetterSerializer(read_only=True) previous_state = SlugRelatedField(slug_field='slug', read_only=True) current_state = SlugRelatedField(slug_field='slug', read_only=True) previous_delegate = UserSerializer() current_delegate = UserSerializer() created_check = SerializerMethodField() created_check = CheckSerializer() previous_relation = PatchRelationSerializer(read_only=True) current_relation = PatchRelationSerializer(read_only=True) _category_map = { Event.CATEGORY_COVER_CREATED: ['cover'], Event.CATEGORY_PATCH_CREATED: ['patch'], Event.CATEGORY_PATCH_COMPLETED: ['patch', 'series'], Event.CATEGORY_PATCH_STATE_CHANGED: ['patch', 'previous_state', 'current_state'], Event.CATEGORY_PATCH_DELEGATED: ['patch', 'previous_delegate', 'current_delegate'], Event.CATEGORY_PATCH_RELATION_CHANGED: ['patch', 'previous_relation', 'current_relation'], Event.CATEGORY_CHECK_CREATED: ['patch', 'created_check'], Event.CATEGORY_SERIES_CREATED: ['series'], Event.CATEGORY_SERIES_COMPLETED: ['series'], } def to_representation(self, instance): data = super(EventSerializer, self).to_representation(instance) payload = OrderedDict() kept_fields = self._category_map[instance.category] + [ 'id', 'category', 'project', 'date', 'actor'] for field in [x for x in data]: if field not in kept_fields: del data[field] elif field in self._category_map[instance.category]: field_name = 'check' if field == 'created_check' else field payload[field_name] = data.pop(field) data['payload'] = payload return data class Meta: model = Event fields = ('id', 'category', 'project', 'date', 'actor', 'patch', 'series', 'cover', 'previous_state', 'current_state', 'previous_delegate', 'current_delegate', 'created_check', 'previous_relation', 'current_relation',) read_only_fields = fields versioned_fields = { '1.2': ('actor', ), }
class EventSerializer(ModelSerializer): project = ProjectSerializer(read_only=True) actor = UserSerializer() patch = PatchSerializer(read_only=True) series = SeriesSerializer(read_only=True) cover = CoverSerializer(read_only=True) previous_state = SlugRelatedField(slug_field='slug', read_only=True) current_state = SlugRelatedField(slug_field='slug', read_only=True) previous_delegate = UserSerializer() current_delegate = UserSerializer() created_check = SerializerMethodField() created_check = CheckSerializer() previous_relation = SerializerMethodField() current_relation = SerializerMethodField() cover_comment = CoverCommentSerializer() patch_comment = PatchCommentSerializer() # Mapping of event type to fields to include in the response _category_map = { Event.CATEGORY_COVER_CREATED: ['cover'], Event.CATEGORY_PATCH_CREATED: ['patch'], Event.CATEGORY_PATCH_COMPLETED: ['patch', 'series'], Event.CATEGORY_PATCH_STATE_CHANGED: [ 'patch', 'previous_state', 'current_state', ], Event.CATEGORY_PATCH_DELEGATED: [ 'patch', 'previous_delegate', 'current_delegate', ], Event.CATEGORY_PATCH_RELATION_CHANGED: [ 'patch', 'previous_relation', 'current_relation', ], Event.CATEGORY_CHECK_CREATED: ['patch', 'created_check'], Event.CATEGORY_SERIES_CREATED: ['series'], Event.CATEGORY_SERIES_COMPLETED: ['series'], Event.CATEGORY_COVER_COMMENT_CREATED: ['cover', 'cover_comment'], Event.CATEGORY_PATCH_COMMENT_CREATED: ['patch', 'patch_comment'], } # Mapping of database column names to REST API representations _field_name_map = { 'created_check': 'check', 'cover_comment': 'comment', 'patch_comment': 'comment', } def get_previous_relation(self, instance): return None def get_current_relation(self, instance): return None def to_representation(self, instance): data = super(EventSerializer, self).to_representation(instance) payload = OrderedDict() kept_fields = self._category_map[instance.category] + [ 'id', 'category', 'project', 'date', 'actor', ] for field in [x for x in data]: if field not in kept_fields: del data[field] elif field in self._category_map[instance.category]: # remap fields if necessary field_name = self._field_name_map.get(field, field) payload[field_name] = data.pop(field) data['payload'] = payload return data class Meta: model = Event fields = ( 'id', 'category', 'project', 'date', 'actor', 'patch', 'series', 'cover', 'previous_state', 'current_state', 'previous_delegate', 'current_delegate', 'created_check', 'previous_relation', 'current_relation', 'cover_comment', 'patch_comment', ) read_only_fields = fields versioned_fields = { '1.2': ('actor', ), }