<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework import serializers

from documents.models import Document, Page, Vote
from tags.serializers import TagSerializer
from users.serializers import SmallUserSerializer


class DocumentSerializer(serializers.HyperlinkedModelSerializer):
    tags = TagSerializer(many=True)
    user = SmallUserSerializer()

    has_perm = serializers.SerializerMethodField()
    user_vote = serializers.SerializerMethodField()

    def get_user_vote(self, document):
        user = self.context['request'].user
        try:
            vote = Vote.objects.get(document=document, user=user)
        except Vote.DoesNotExist:
            return 0

        if vote.vote_type == vote.UPVOTE:
            return 1
        elif vote.vote_type == vote.DOWNVOTE:
            return -1
        else:
            raise NotImplemented("Vote not of known type.")

    def get_has_perm(self, document):
        user = self.context['request'].user
        return user.write_perm(obj=document)

    class Meta:
        model = Document
        fields = (
            'id', 'name', 'url', 'course', 'description',
            'user', 'pages', 'date', 'views', 'user_vote', 'votes',
            'downloads', 'state', 'md5', 'tags', 'has_perm',<commit_msg>[fix] Really avoid n+1 queries on get_user_vote<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework import serializers

from documents.models import Document, Page, Vote
from tags.serializers import TagSerializer
from users.serializers import SmallUserSerializer


class DocumentSerializer(serializers.HyperlinkedModelSerializer):
    tags = TagSerializer(many=True)
    user = SmallUserSerializer()

    has_perm = serializers.SerializerMethodField()
    user_vote = serializers.SerializerMethodField()

    def get_user_vote(self, document):
        user = self.context['request'].user
        try:
            vote = document.vote_set.get(user=user)
        except Vote.DoesNotExist:
            return 0

        if vote.vote_type == vote.UPVOTE:
            return 1
        elif vote.vote_type == vote.DOWNVOTE:
            return -1
        else:
            raise NotImplemented("Vote not of known type.")

    def get_has_perm(self, document):
        user = self.context['request'].user
        return user.write_perm(obj=document)

    class Meta:
        model = Document
        fields = (
            'id', 'name', 'url', 'course', 'description',
            'user', 'pages', 'date', 'views', 'user_vote', 'votes',
            'downloads', 'state', 'md5', 'tags', 'has_perm',
{'fraction_non_alphanumeric': 0.10754716981132076, 'fraction_numerical': 0.004150943396226415, 'mean_word_length': 2.819884726224784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4477874', 'n_tokens_mistral': 752, 'n_tokens_neox': 723, 'n_words': 190}