1 from rest_framework import serializers
5 class TextField(serializers.Field):
6 def get_attribute(self, instance):
9 def to_representation(self, value):
10 return value.materialize()
12 def to_internal_value(self, data):
16 class BookSerializer(serializers.ModelSerializer):
17 id = serializers.HyperlinkedIdentityField(view_name='documents_api_book')
27 class ChunkSerializer(serializers.ModelSerializer):
28 id = serializers.HyperlinkedIdentityField(view_name='documents_api_chunk')
29 book = serializers.HyperlinkedRelatedField(view_name='documents_api_book', read_only=True)
30 revisions = serializers.HyperlinkedIdentityField(view_name='documents_api_chunk_revision_list')
31 head = serializers.HyperlinkedRelatedField(view_name='documents_api_revision', read_only=True)
36 fields = ['id', 'book', 'revisions', 'head', 'user', 'stage']
39 class RHRF(serializers.HyperlinkedRelatedField):
40 def get_queryset(self):
41 return self.context['chunk'].change_set.all();
44 class RevisionSerializer(serializers.ModelSerializer):
45 id = serializers.HyperlinkedIdentityField(view_name='documents_api_revision')
47 view_name='documents_api_revision',
48 queryset=models.Chunk.change_model.objects.all()
51 view_name='documents_api_revision',
54 chunk = serializers.HyperlinkedRelatedField(view_name='documents_api_chunk', read_only=True, source='tree')
55 author = serializers.SerializerMethodField()
58 model = models.Chunk.change_model
59 fields = ['id', 'chunk', 'created_at', 'author', 'author_email', 'author_name', 'parent', 'merge_parent']
60 read_only_fields = ['author_email', 'author_name']
62 def get_author(self, obj):
63 return obj.author.username if obj.author is not None else None
66 class BookDetailSerializer(BookSerializer):
67 chunk = ChunkSerializer(many=True, source='chunk_set')
71 fields = BookSerializer.Meta.fields + ['chunk']
75 class ChunkDetailSerializer(ChunkSerializer):
79 class RevisionDetailSerializer(RevisionSerializer):
82 class Meta(RevisionSerializer.Meta):
83 fields = RevisionSerializer.Meta.fields + ['description', 'text']
85 def create(self, validated_data):
86 chunk = self.context['chunk']
88 validated_data['text'],
89 author=self.context['request'].user, # what if anonymous?
90 description=validated_data['description'],
91 parent=validated_data.get('parent'),