class ActionSerializer(serializers.ModelSerializer): """ Generic serializer """ class IncredibleField(serializers.RelatedField): def to_representation(self, value): if isinstance(value, User): serializer = UserSerializer(value) elif isinstance(value, Document): serializer = LiteDocumentSerializer(value) elif isinstance(value, Story): serializer = IncrediblyLiteStorySerializer(value) elif isinstance(value, Profile): serializer = ProfileSerializer(value) elif isinstance(value, Comment): serializer = CommentSerializer(value) else: raise Exception('Unexpected type of action object') return serializer.data actor = IncredibleField(read_only=True) target = IncredibleField(read_only=True) target_content_type = ContentTypeField(source='target', read_only=True) info = JsonField(source='data') class Meta: model = Action fields = ('id', 'verb', 'description', 'timestamp', 'timesince', 'actor', 'target', 'target_content_type', 'info') #, 'actor', 'target')
class CommentSerializer(serializers.ModelSerializer): contents = JsonField() owner = UserSerializer() class Meta: model = Comment fields = ('pk', 'owner', 'contents', 'version', 'date_created', 'highlights', 'short_url', 'status')
class LiteMentionSerializer(serializers.ModelSerializer): slug = serializers.ReadOnlyField() data = JsonField() covers = LiteDocumentSerializer(many=True) tags = TagSerializer(many=True) authors = AuthorSerializer(many=True) class Meta: model = Mention fields = ('id', 'slug', 'data', 'tags', 'authors', 'covers')
class LiteStorySerializer(serializers.HyperlinkedModelSerializer): authors = AuthorSerializer(many=True) owner = AuthorSerializer() tags = TagSerializer(many=True) covers = LiteDocumentSerializer(many=True) metadata = JsonField() class Meta: model = Story fields = ('id', 'url', 'slug', 'short_url', 'date', 'date_created', 'status', 'covers', 'authors', 'tags', 'owner', 'metadata')
class StorySerializer(serializers.HyperlinkedModelSerializer): authors = AuthorSerializer(many=True) owner = AuthorSerializer() tags = TagSerializer(many=True) documents = CaptionSerializer(source='caption_set', many=True) covers = LiteDocumentSerializer(many=True) metadata = JsonField() class Meta: model = Story fields = ('id', 'url', 'slug', 'short_url', 'title', 'abstract', 'documents', 'tags', 'covers', 'metadata', 'contents', 'date', 'date_created', 'status', 'authors', 'owner')
class MatchingStorySerializer(serializers.HyperlinkedModelSerializer): authors = AuthorSerializer(many=True) owner = AuthorSerializer() tags = TagSerializer(many=True) covers = LiteDocumentSerializer(many=True) matches = HitField() metadata = JsonField() def is_named_bar(self, foo): return foo.name == "bar" class Meta: model = Story fields = ('id', 'url', 'slug', 'short_url', 'title', 'abstract', 'date', 'date_created', 'status', 'covers', 'metadata', 'authors', 'tags', 'owner', 'matches')
class CaptionSerializer(serializers.HyperlinkedModelSerializer): document_id = serializers.ReadOnlyField(source='document.id') type = serializers.ReadOnlyField(source='document.type') title = serializers.ReadOnlyField(source='document.title') slug = serializers.ReadOnlyField(source='document.slug') src = OptionalFileField(source='document.attachment') short_url = serializers.ReadOnlyField(source='document.short_url') copyrights = serializers.ReadOnlyField(source='document.copyrights') caption = serializers.ReadOnlyField(source='contents') metadata = JsonField(source='document.contents') snapshot = serializers.ReadOnlyField(source='document.snapshot') class Meta: model = Caption fields = ('id', 'document_id', 'title', 'slug', 'type', 'copyrights', 'caption', 'short_url', 'src', 'snapshot', 'metadata')
class LiteDocumentSerializer(serializers.ModelSerializer): metadata = JsonField(source='contents') class Meta: model = Document fields = ('id', 'copyrights', 'metadata', 'url', 'attachment')