示例#1
0
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')
示例#2
0
class CommentSerializer(serializers.ModelSerializer):
  contents = JsonField()
  owner    = UserSerializer()
  
  class Meta:
    model = Comment
    fields = ('pk', 'owner', 'contents', 'version', 'date_created', 'highlights', 'short_url', 'status')
示例#3
0
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')
示例#4
0
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')
示例#5
0
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')
示例#6
0
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')
示例#7
0
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')
示例#8
0
class LiteDocumentSerializer(serializers.ModelSerializer):
    metadata = JsonField(source='contents')

    class Meta:
        model = Document
        fields = ('id', 'copyrights', 'metadata', 'url', 'attachment')