<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()

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

    def get_user_vote(self, document):
        user = self.context['request'].user
        # We do the filtering in python as this method is called from REST with all the necessary
        #   data already prefetched. Using self.vote_set.filter() would lead to another roundtrip
        #   to the database for each document. Thats bad.
        users_vote = None
        for vote in document.vote_set.all():
            if vote.user == user:
                users_vote = vote
                break

        if users_vote is None:
            return 0
        elif users_vote.vote_type == Vote.UPVOTE:
            return 1
        elif users_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 = (
            'course', 'date', 'description', 'downloads', 'file_type',
            'has_perm', 'hidden', 'id', 'is_processing', 'is_ready',
            'is_unconvertible', 'md5', 'name', 'pages', 'size', 'state',
            'tags', 'url', 'user', 'user_vote', 'views', 'votes',
        )

        extra_kwargs = {
            'user': {'lookup_field': 'netid'},
            'course': {'lookup_field': 'slug'},
        }

<commit_msg>[fix] Document.size seems not to be implemented<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()

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

    def get_user_vote(self, document):
        user = self.context['request'].user
        # We do the filtering in python as this method is called from REST with all the necessary
        #   data already prefetched. Using self.vote_set.filter() would lead to another roundtrip
        #   to the database for each document. Thats bad.
        users_vote = None
        for vote in document.vote_set.all():
            if vote.user == user:
                users_vote = vote
                break

        if users_vote is None:
            return 0
        elif users_vote.vote_type == Vote.UPVOTE:
            return 1
        elif users_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)

    def get_file_size(self, document):
        return document.original.size

    class Meta:
        model = Document
        fields = (
            'course', 'date', 'description', 'downloads', 'file_size',
            'file_type', 'has_perm', 'hidden', 'id', 'is_processing',
            'is_ready', 'is_unconvertible', 'md5', 'name', 'pages', 'state',
            'tags', 'url', 'user', 'user_vote', 'views', 'votes',
        )

        extra_kwargs = {
            'user': {'lookup_field': 'netid'},
            'course': {'lookup_field': 'slug'},
        }


{'fraction_non_alphanumeric': 0.10269997476659097, 'fraction_numerical': 0.0025233409033560434, 'mean_word_length': 2.5079646017699115, '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': '4405961', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1060, 'n_words': 315}