<commit_before>from rest_framework import serializers
from rest_framework.reverse import reverse

from parties.api.next.serializers import MinimalPartySerializer
from popolo.api.next.serializers import (
    BallotOnCandidacySerializer,
    PersonOnBallotSerializer,
    CandidacyOnBallotSerializer,
    CANDIDACY_ON_PERSON_FIELDS,
)
from popolo.models import Membership
from uk_results.models import ResultSet, CandidateResult


class CandidateResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            "person",
            "party",
            "party_list_position",
            "num_ballots",
            "elected",
        )
        ref_name = None  # Tells swagger that this is always embedded

    elected = serializers.ReadOnlyField(source="is_winner")
    person = serializers.SerializerMethodField()
    party_list_position = serializers.ReadOnlyField(
        source="membership__party_list_position"
    )
    party = serializers.SerializerMethodField()

    def get_person(self, obj):
        return PersonOnBallotSerializer(
            obj.membership.person, context=self.context
        ).data

    def get_party(self, obj):
        return MinimalPartySerializer(
            obj.membership.party, context=self.context
        ).data


class ResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            "url",
            "num_turnout_reported",
            "num_spoilt_ballots",
            "source",
            "ballot",
            "candidate_results",
        )

    url = serializers.SerializerMethodField()

    def get_url(self, obj):
        return reverse(
            "resultset-detail",
            kwargs={"ballot_paper_id": obj.ballot.ballot_paper_id},
            request=self.context["request"],
        )

    ballot = BallotOnCandidacySerializer()
    candidate_results = CandidateResultSerializer(many=True)


class ElectedSerializer(CandidacyOnBallotSerializer):
    class Meta:
        model = Membership
        fields = CANDIDACY_ON_PERSON_FIELDS + ["ballot"]<commit_msg>Add person to elected endpoint<commit_after>from rest_framework import serializers
from rest_framework.reverse import reverse

from parties.api.next.serializers import MinimalPartySerializer
from popolo.api.next.serializers import (
    BallotOnCandidacySerializer,
    PersonOnBallotSerializer,
    CandidacyOnBallotSerializer,
    CANDIDACY_ON_PERSON_FIELDS,
)
from popolo.models import Membership
from uk_results.models import ResultSet, CandidateResult


class CandidateResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            "person",
            "party",
            "party_list_position",
            "num_ballots",
            "elected",
        )
        ref_name = None  # Tells swagger that this is always embedded

    elected = serializers.ReadOnlyField(source="is_winner")
    person = serializers.SerializerMethodField()
    party_list_position = serializers.ReadOnlyField(
        source="membership__party_list_position"
    )
    party = serializers.SerializerMethodField()

    def get_person(self, obj):
        return PersonOnBallotSerializer(
            obj.membership.person, context=self.context
        ).data

    def get_party(self, obj):
        return MinimalPartySerializer(
            obj.membership.party, context=self.context
        ).data


class ResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            "url",
            "num_turnout_reported",
            "num_spoilt_ballots",
            "source",
            "ballot",
            "candidate_results",
        )

    url = serializers.SerializerMethodField()

    def get_url(self, obj):
        return reverse(
            "resultset-detail",
            kwargs={"ballot_paper_id": obj.ballot.ballot_paper_id},
            request=self.context["request"],
        )

    ballot = BallotOnCandidacySerializer()
    candidate_results = CandidateResultSerializer(many=True)


class ElectedSerializer(CandidacyOnBallotSerializer):
    class Meta:
        model = Membership
        fields = CANDIDACY_ON_PERSON_FIELDS + ["ballot", "person"]

    def get_person(self, obj):
        return PersonOnBallotSerializer(obj.person, context=self.context).data
{'fraction_non_alphanumeric': 0.07845804988662132, 'fraction_numerical': 0.0, 'mean_word_length': 2.7895189003436425, 'pattern_counts': {'":': 2, '<': 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': '923356', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1198, 'n_words': 232}