<commit_before>import json

from django.core.urlresolvers import reverse

from rest_framework import serializers

from candidates import models as candidates_models
from images.models import Image
from elections import models as election_models
from popolo import models as popolo_models


class OtherNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.OtherName
        fields = ('name', 'note')


class IdentifierSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Identifier
        fields = ('identifier', 'scheme')


class ContactDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.ContactDetail
        fields = ('contact_type', 'label', 'note', 'value')


class LinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Link
        fields = ('note', 'url')


class SourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Source
        fields = ('note', 'url')


class AreaTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = election_models.AreaType
        fields = ('name', 'source')


class AreaSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = popolo_models.Area
        fields = (
            'id',
            'url',
            'name',
            'identifier',
            'classification',
            'other_identifiers',
            'parent',
            'type',
        )

    other_identifiers = IdentifierSerializer(many=True, read_only=True)
    type = AreaTypeSerializer(source='extra.type')


class ElectionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = election_models.Election
        fields = (
            'id',
            'url',
            'name',
            'winner_membership_role',
            'candidate_membership_role',
            'election_date',
            'current',
            'use_for_candidate_suggestions',
            'area_types',
            'area_generation',
            'organization',
            'party_lists_in_use',
            'ocd_division',
            'description'
        )

    url = serializers.HyperlinkedIdentityField(
        view_name='election-detail',
        lookup_field='slug',
        lookup_url_kwarg='slug',
    )

    id = serializers.ReadOnlyField(source='slug')

    area_types = AreaTypeSerializer(many=True, read_only=True)


class FlatMembershipSerialzier(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = popolo_models.Membership
        fields = (
            'label',
            'role',
            'person',
            'organization',
            'on_behalf_of',
            'post',
            'start_date',
            'end_date',
            'election',
        )

    election = ElectionSerializer(source='extra.election')


class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Image
        fields = (
            'source',
            'is_primary',
            'md5sum',
            'copyright',
            'uploading_user',
            'user_notes',
            'user_copyright',
            'notes',
            'image_url',
        )

    md5sum = serializers.ReadOnlyField(source='extra.md5sum')
    copyright = serializers.ReadOnlyField(source='extra.copyright')
    uploading_user = serializers.ReadOnlyField(source='extra.uploading_user.username')
    user_notes = serializers.ReadOnlyField(source='extra.user_notes')
    user_copyright = serializers.ReadOnlyField(source='extra.user_copyright')
    notes = serializers.ReadOnlyField(source='extra.notes')
    image_url = serializers.SerializerMethodField()

    def get_image_url(self, i):
        return i.image.url

class MinimalOrganizationExtraSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = candidates_models.OrganizationExtra
        fields = ('id', 'url', 'name')

    id = serializers.ReadOnlyField(source='slug')
    name = serializers.ReadOnlyField(source='base.name')
    url = serializers.HyperlinkedIdentityField(
        view_name='organizationextra-detail',
        lookup_field='slug',
        lookup_url_kwarg='slug',
    )


class OrganizationExtraSerializer(MinimalOrganizationExtraSerializer):
    class Meta:
        model = candidates_models.OrganizationExtra
        fields = (
            'id',
            'url',
            'name',
            'other_names',
            'identifiers',
            'classification',
            'parent',
            'founding_date',
            'dissolution_date',
            'contact_details',
            'images',
            'links',
            'sources',
            'register',
        )

    classification = serializers.ReadOnlyField(source='base.classification')
    founding_date = serializers.ReadOnlyField(source='base.founding_date')
    dissolution_date = serializers.ReadOnlyField(source='base.dissolution_date')

    parent = MinimalOrganizationExtraSerializer(source='base.parent.extra')

    contact_details = ContactDetailSerializer(
        many=True, read_only=True, source='base.contact_details')
    identifiers = IdentifierSerializer(
        many=True, read_only=True, source='base.identifiers')
    links = LinkSerializer(
        many=True, read_only=True, source='base.links')
    other_names = OtherNameSerializer(
        many=True, read_only=True, source='base.other_names')
    sources = SourceSerializer(
        many=True, read_only=True, source='base.sources')
    images = ImageSerializer(many=True, read_only=True)

class JSONSerializerField(serializers.Field):
    def to_representation(self, value):
        return json.loads(value)

<commit_msg>Reorder classes so that everything's declared before use<commit_after>import json

from django.core.urlresolvers import reverse

from rest_framework import serializers

from candidates import models as candidates_models
from images.models import Image
from elections import models as election_models
from popolo import models as popolo_models


class OtherNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.OtherName
        fields = ('name', 'note')


class IdentifierSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Identifier
        fields = ('identifier', 'scheme')


class ContactDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.ContactDetail
        fields = ('contact_type', 'label', 'note', 'value')


class LinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Link
        fields = ('note', 'url')


class SourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = popolo_models.Source
        fields = ('note', 'url')


class AreaTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = election_models.AreaType
        fields = ('name', 'source')


class AreaSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = popolo_models.Area
        fields = (
            'id',
            'url',
            'name',
            'identifier',
            'classification',
            'other_identifiers',
            'parent',
            'type',
        )

    other_identifiers = IdentifierSerializer(many=True, read_only=True)
    type = AreaTypeSerializer(source='extra.type')


class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Image
        fields = (
            'source',
            'is_primary',
            'md5sum',
            'copyright',
            'uploading_user',
            'user_notes',
            'user_copyright',
            'notes',
            'image_url',
        )

    md5sum = serializers.ReadOnlyField(source='extra.md5sum')
    copyright = serializers.ReadOnlyField(source='extra.copyright')
    uploading_user = serializers.ReadOnlyField(source='extra.uploading_user.username')
    user_notes = serializers.ReadOnlyField(source='extra.user_notes')
    user_copyright = serializers.ReadOnlyField(source='extra.user_copyright')
    notes = serializers.ReadOnlyField(source='extra.notes')
    image_url = serializers.SerializerMethodField()

    def get_image_url(self, i):
        return i.image.url

class MinimalOrganizationExtraSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = candidates_models.OrganizationExtra
        fields = ('id', 'url', 'name')

    id = serializers.ReadOnlyField(source='slug')
    name = serializers.ReadOnlyField(source='base.name')
    url = serializers.HyperlinkedIdentityField(
        view_name='organizationextra-detail',
        lookup_field='slug',
        lookup_url_kwarg='slug',
    )


class OrganizationExtraSerializer(MinimalOrganizationExtraSerializer):
    class Meta:
        model = candidates_models.OrganizationExtra
        fields = (
            'id',
            'url',
            'name',
            'other_names',
            'identifiers',
            'classification',
            'parent',
            'founding_date',
            'dissolution_date',
            'contact_details',
            'images',
            'links',
            'sources',
            'register',
        )

    classification = serializers.ReadOnlyField(source='base.classification')
    founding_date = serializers.ReadOnlyField(source='base.founding_date')
    dissolution_date = serializers.ReadOnlyField(source='base.dissolution_date')

    parent = MinimalOrganizationExtraSerializer(source='base.parent.extra')

    contact_details = ContactDetailSerializer(
        many=True, read_only=True, source='base.contact_details')
    identifiers = IdentifierSerializer(
        many=True, read_only=True, source='base.identifiers')
    links = LinkSerializer(
        many=True, read_only=True, source='base.links')
    other_names = OtherNameSerializer(
        many=True, read_only=True, source='base.other_names')
    sources = SourceSerializer(
        many=True, read_only=True, source='base.sources')
    images = ImageSerializer(many=True, read_only=True)


class ElectionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = election_models.Election
        fields = (
            'id',
            'url',
            'name',
            'winner_membership_role',
            'candidate_membership_role',
            'election_date',
            'current',
            'use_for_candidate_suggestions',
            'area_types',
            'area_generation',
            'organization',
            'party_lists_in_use',
            'ocd_division',
            'description'
        )

    url = serializers.HyperlinkedIdentityField(
        view_name='election-detail',
        lookup_field='slug',
        lookup_url_kwarg='slug',
    )

    id = serializers.ReadOnlyField(source='slug')
    organization = MinimalOrganizationExtraSerializer(source='organization.extra')

    area_types = AreaTypeSerializer(many=True, read_only=True)


class FlatMembershipSerialzier(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = popolo_models.Membership
        fields = (
            'label',
            'role',
            'person',
            'organization',
            'on_behalf_of',
            'post',
            'start_date',
            'end_date',
            'election',
        )

    election = ElectionSerializer(source='extra.election')


class JSONSerializerField(serializers.Field):
    def to_representation(self, value):
        return json.loads(value)


{'fraction_non_alphanumeric': 0.09882875951098573, 'fraction_numerical': 0.0005129520389843549, 'mean_word_length': 2.649921996879875, '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': '1971268', 'n_tokens_mistral': 3112, 'n_tokens_neox': 3063, 'n_words': 559}