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

from rest_framework import serializers

from commonrepo.groups.models import Group as Group
from commonrepo.users.models import User as User

from commonrepo.elos.models import ELO
from commonrepo.snippets_api.models import Snippet

class UserSerializer(serializers.HyperlinkedModelSerializer):
    # ELOs information
    # elos = ForeignKey relationship from model ELO.author
    elos_published = serializers.SerializerMethodField()
    elos_forks = serializers.SerializerMethodField()
    elos_from_others = serializers.SerializerMethodField()

    # Groups
    # commonrepo_groups = ForeignKey relationship from model Group.creator

    # Misc
    # snippets = ForeignKey relationship from model snippets.owner
    
    def get_elos_published(self, obj):
        request = self.context.get("request")
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            return ELO.objects.filter(author=request.user).filter(is_public=1).count()
        else:
            return -1
    
    def get_elos_forks(self, obj):
        request = self.context.get("request")
        total_forks = 0
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            my_elo_sets = ELO.objects.filter(author=request.user)
            
            for elo in my_elo_sets:
                total_forks += ELO.objects.filter(parent_elo=elo.id).count()
            
            return total_forks
        
        else:
            return -1
    
    def get_elos_from_others(self, obj):
        request = self.context.get("request")
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            my_elo_count = ELO.objects.filter(
                    author=request.user).filter(
                    is_public=1).count()
            my_original_elo_count = ELO.objects.filter(
                    author=request.user).filter(
                    is_public=1).filter(
                    parent_elo=1).count()
            return my_elo_count - my_original_elo_count
        
        else:
            return -1

    class Meta:
        model = User
        fields = (
            # Basic user information
            'url', 'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Extend user information
            'teaching_category', 'teaching_subject_area',
            # ELO related information
            'elo_similarity_threshold', 'elos', 'elos_published', 'elos_forks', 'elos_from_others',
            # Group information
            'commonrepo_groups', 'commonrepo_groups_members',
            # Misc
            'snippets', )

class UserSerializerV2(serializers.ModelSerializer):
    # ELOs information
    # elos = ForeignKey relationship from model ELO.author
    elos_published = serializers.SerializerMethodField()
    elos_forks = serializers.SerializerMethodField()
    elos_from_others = serializers.SerializerMethodField()

    # Groups
    # commonrepo_groups = ForeignKey relationship from model Group.creator

    # Misc
    # snippets = ForeignKey relationship from model snippets.owner
    
    def get_elos_published(self, obj):
        request = self.context.get("request")
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            return ELO.objects.filter(author=obj).filter(is_public=1).count()
        else:
            return -1
    
    def get_elos_forks(self, obj):
        request = self.context.get("request")
        total_forks = 0
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            my_elo_sets = ELO.objects.filter(author=obj)
            
            for elo in my_elo_sets:
                total_forks += ELO.objects.filter(parent_elo=elo.id).count()
            
            return total_forks
        
        else:
            return -1
    
    def get_elos_from_others(self, obj):
        request = self.context.get("request")
        
        if request and hasattr(request, "user") and request.user.is_authenticated():
            my_elo_count = ELO.objects.filter(
                    author=obj).filter(
                    is_public=1).count()
            my_original_elo_count = ELO.objects.filter(
                    author=obj).filter(
                    is_public=1).filter(
                    parent_elo=1).count()
            return my_elo_count - my_original_elo_count
        
        else:
            return -1

    class Meta:
        model = User
        fields = (
            # Basic user information
            'url', 'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Social informaion
            'social_facebook', 'social_google', 'social_linkedin', 'social_twitter',
            # Extend user information
            'teaching_category', 'teaching_subject_area',
            # Social information
            'friend_with', 'followed_by',
            # ELO related information
            'elo_similarity_threshold', 'elos', 'elos_published', 'elos_forks', 'elos_from_others',
            # Group information
            'commonrepo_groups', 'commonrepo_groups_members',
            # Misc
            'snippets',
            # Preferences
            'elo_similarity_threshold',)

class UserLiteSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            # Basic user information
            'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Social informaion<commit_msg>更新 modules users_api/serializers.py, 修正 PEP8 警告<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from rest_framework import serializers

from commonrepo.groups.models import Group as Group
from commonrepo.users.models import User as User

from commonrepo.elos.models import ELO
from commonrepo.snippets_api.models import Snippet


class UserSerializer(serializers.HyperlinkedModelSerializer):
    # ELOs information
    # elos = ForeignKey relationship from model ELO.author
    elos_published = serializers.SerializerMethodField()
    elos_forks = serializers.SerializerMethodField()
    elos_from_others = serializers.SerializerMethodField()

    # Groups
    # commonrepo_groups = ForeignKey relationship from model Group.creator

    # Misc
    # snippets = ForeignKey relationship from model snippets.owner

    def get_elos_published(self, obj):
        request = self.context.get("request")

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            return ELO.objects.filter(
                author=request.user).filter(
                is_public=1).count()
        else:
            return -1

    def get_elos_forks(self, obj):
        request = self.context.get("request")
        total_forks = 0

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            my_elo_sets = ELO.objects.filter(author=request.user)

            for elo in my_elo_sets:
                total_forks += ELO.objects.filter(parent_elo=elo.id).count()

            return total_forks

        else:
            return -1

    def get_elos_from_others(self, obj):
        request = self.context.get("request")

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            my_elo_count = ELO.objects.filter(
                author=request.user).filter(
                is_public=1).count()
            my_original_elo_count = ELO.objects.filter(
                author=request.user).filter(
                is_public=1).filter(
                parent_elo=1).count()
            return my_elo_count - my_original_elo_count

        else:
            return -1

    class Meta:
        model = User
        fields = (
            # Basic user information
            'url', 'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Extend user information
            'teaching_category', 'teaching_subject_area',
            # ELO related information
            'elo_similarity_threshold', 'elos', 'elos_published', 'elos_forks', 'elos_from_others',
            # Group information
            'commonrepo_groups', 'commonrepo_groups_members',
            # Misc
            'snippets', )


class UserSerializerV2(serializers.ModelSerializer):
    # ELOs information
    # elos = ForeignKey relationship from model ELO.author
    elos_published = serializers.SerializerMethodField()
    elos_forks = serializers.SerializerMethodField()
    elos_from_others = serializers.SerializerMethodField()

    # Groups
    # commonrepo_groups = ForeignKey relationship from model Group.creator

    # Misc
    # snippets = ForeignKey relationship from model snippets.owner

    def get_elos_published(self, obj):
        request = self.context.get("request")

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            return ELO.objects.filter(author=obj).filter(is_public=1).count()
        else:
            return -1

    def get_elos_forks(self, obj):
        request = self.context.get("request")
        total_forks = 0

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            my_elo_sets = ELO.objects.filter(author=obj)

            for elo in my_elo_sets:
                total_forks += ELO.objects.filter(parent_elo=elo.id).count()

            return total_forks

        else:
            return -1

    def get_elos_from_others(self, obj):
        request = self.context.get("request")

        if request and hasattr(request,
                               "user") and request.user.is_authenticated():
            my_elo_count = ELO.objects.filter(
                author=obj).filter(
                is_public=1).count()
            my_original_elo_count = ELO.objects.filter(
                author=obj).filter(
                is_public=1).filter(
                parent_elo=1).count()
            return my_elo_count - my_original_elo_count

        else:
            return -1

    class Meta:
        model = User
        fields = (
            # Basic user information
            'url', 'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Social informaion
            'social_facebook', 'social_google', 'social_linkedin', 'social_twitter',
            # Extend user information
            'teaching_category', 'teaching_subject_area',
            # Social information
            'friend_with', 'followed_by',
            # ELO related information
            'elo_similarity_threshold', 'elos', 'elos_published', 'elos_forks', 'elos_from_others',
            # Group information
            'commonrepo_groups', 'commonrepo_groups_members',
            # Misc
            'snippets',
            # Preferences
            'elo_similarity_threshold',)


class UserLiteSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = (
            # Basic user information
            'id', 'username', 'organization', 'education', 'url', 'phone', 'address', 'language', 'area', 'about',
            # Social informaion
{'fraction_non_alphanumeric': 0.09509518092951466, 'fraction_numerical': 0.003172697650488767, 'mean_word_length': 2.145361380798274, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345946', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3121, 'n_words': 694}