|
| 1 | +from rest_framework import serializers |
| 2 | +from .. import models |
| 3 | + |
| 4 | + |
| 5 | +class TextField(serializers.Field): |
| 6 | + def get_attribute(self, instance): |
| 7 | + return instance |
| 8 | + |
| 9 | + def to_representation(self, value): |
| 10 | + return value.materialize() |
| 11 | + |
| 12 | + def to_internal_value(self, data): |
| 13 | + return data |
| 14 | + |
| 15 | + |
| 16 | +class BookSerializer(serializers.ModelSerializer): |
| 17 | + id = serializers.HyperlinkedIdentityField(view_name='documents_api_book') |
| 18 | + |
| 19 | + class Meta: |
| 20 | + model = models.Book |
| 21 | + fields = [ |
| 22 | + 'id', |
| 23 | + 'title' |
| 24 | + ] |
| 25 | + |
| 26 | + |
| 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) |
| 32 | + ## RelatedField |
| 33 | + |
| 34 | + class Meta: |
| 35 | + model = models.Chunk |
| 36 | + fields = ['id', 'book', 'revisions', 'head', 'user', 'stage'] |
| 37 | + |
| 38 | + |
| 39 | +class RHRF(serializers.HyperlinkedRelatedField): |
| 40 | + def get_queryset(self): |
| 41 | + return self.context['chunk'].change_set.all(); |
| 42 | + |
| 43 | + |
| 44 | +class RevisionSerializer(serializers.ModelSerializer): |
| 45 | + id = serializers.HyperlinkedIdentityField(view_name='documents_api_revision') |
| 46 | + parent = RHRF( |
| 47 | + view_name='documents_api_revision', |
| 48 | + queryset=models.Chunk.change_model.objects.all() |
| 49 | + ) |
| 50 | + merge_parent = RHRF( |
| 51 | + view_name='documents_api_revision', |
| 52 | + read_only=True |
| 53 | + ) |
| 54 | + chunk = serializers.HyperlinkedRelatedField(view_name='documents_api_chunk', read_only=True, source='tree') |
| 55 | + author = serializers.SerializerMethodField() |
| 56 | + |
| 57 | + class Meta: |
| 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'] |
| 61 | + |
| 62 | + def get_author(self, obj): |
| 63 | + return obj.author.username if obj.author is not None else None |
| 64 | + |
| 65 | + |
| 66 | +class BookDetailSerializer(BookSerializer): |
| 67 | + chunk = ChunkSerializer(many=True, source='chunk_set') |
| 68 | + |
| 69 | + class Meta: |
| 70 | + model = models.Book |
| 71 | + fields = BookSerializer.Meta.fields + ['chunk'] |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +class ChunkDetailSerializer(ChunkSerializer): |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +class RevisionDetailSerializer(RevisionSerializer): |
| 80 | + text = TextField() |
| 81 | + |
| 82 | + class Meta(RevisionSerializer.Meta): |
| 83 | + fields = RevisionSerializer.Meta.fields + ['description', 'text'] |
| 84 | + |
| 85 | + def create(self, validated_data): |
| 86 | + chunk = self.context['chunk'] |
| 87 | + return chunk.commit( |
| 88 | + validated_data['text'], |
| 89 | + author=self.context['request'].user, # what if anonymous? |
| 90 | + description=validated_data['description'], |
| 91 | + parent=validated_data.get('parent'), |
| 92 | + ) |
0 commit comments