src/iconolab/serializers.py
author ymh <ymh.work@gmail.com>
Wed, 27 Jun 2018 15:50:14 +0200
changeset 556 3fd4e04004f6
parent 427 2a9ce7ec3a29
permissions -rw-r--r--
Move importcollection and importmetacategories commands to the generic project. Add unique constraint to Metacategories
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
418
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     1
from iconolab.models import AnnotationRevision, IconolabComment, AnnotationStats
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
from rest_framework import serializers
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
418
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     4
class AnnotationStatsSerializer(serializers.ModelSerializer):
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     5
    class Meta:
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     6
        model = AnnotationStats
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     7
        fields = ('submitted_revisions_count', 'awaiting_revisions_count', 'accepted_revisions_count', 'contributors_count', 'views_count', 'comments_count', 'tag_count')
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
     8
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
class AnnotationRevisionSerializer(serializers.ModelSerializer):
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
    tags = serializers.SerializerMethodField('get_normalized_tags')
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
    annotation_guid = serializers.SerializerMethodField()
418
a04c55054afe Introduce display of all annotations at the same time.
Alexandre Segura <mex.zktk@gmail.com>
parents: 396
diff changeset
    12
    stats = AnnotationStatsSerializer(source='annotation.stats')
427
2a9ce7ec3a29 Display date & author of last revision.
Alexandre Segura <mex.zktk@gmail.com>
parents: 418
diff changeset
    13
    author = serializers.SerializerMethodField()
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
    def get_normalized_tags(self, obj):
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
        tags = []
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
        for tagging_info in obj.tagginginfo_set.all():
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
            tags.append({
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
                "tag_label": tagging_info.tag.label,
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
                "tag_link": tagging_info.tag.link,
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
                "accuracy": tagging_info.accuracy,
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
                "relevancy": tagging_info.relevancy,
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
            })
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
        return tags
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
    def get_annotation_guid(self, obj):
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
        return obj.annotation.annotation_guid
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
427
2a9ce7ec3a29 Display date & author of last revision.
Alexandre Segura <mex.zktk@gmail.com>
parents: 418
diff changeset
    29
    def get_author(self, obj):
2a9ce7ec3a29 Display date & author of last revision.
Alexandre Segura <mex.zktk@gmail.com>
parents: 418
diff changeset
    30
        return obj.author.username
2a9ce7ec3a29 Display date & author of last revision.
Alexandre Segura <mex.zktk@gmail.com>
parents: 418
diff changeset
    31
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
    class Meta:
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
        model = AnnotationRevision
427
2a9ce7ec3a29 Display date & author of last revision.
Alexandre Segura <mex.zktk@gmail.com>
parents: 418
diff changeset
    34
        fields = ('annotation_guid', 'title', 'description', 'fragment', 'tags', 'stats', 'created', 'author')
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
class IconolabCommentSerializer(serializers.ModelSerializer):
396
0a4743126d74 Implement replying to comments.
Alexandre Segura <mex.zktk@gmail.com>
parents: 337
diff changeset
    37
    allow_thread = serializers.BooleanField()
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
    class Meta:
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
        model = IconolabComment
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
        fields = '__all__'