<commit_before>from rest_framework import serializers
from registries.models import (
    Organization,
    ContactAt,
    Person
)

class AuditModelSerializer(serializers.ModelSerializer):
    """
    Serializes AuditModel fields.
    Can be inherited into serializers for models that inherit from AuditModel
    """
    create_user = serializers.StringRelatedField()
    create_date = serializers.ReadOnlyField()
    update_user = serializers.StringRelatedField()
    update_date = serializers.ReadOnlyField()


class ContactAtSerializer(AuditModelSerializer):
    """
    Serializes ContactAt model fields.
    """
    person_name = serializers.StringRelatedField(source="person")
    organization_name = serializers.StringRelatedField(source="org")

    class Meta:
        model = ContactAt
        fields = (
            'contact_at_guid',
            'person',
            'org',
            'person_name',
            'organization_name',
            'contact_tel',
            'contact_email'
        )

class OrganizationListSerializer(AuditModelSerializer):
    """
    Serializes Organization model fields for "list" view.
    """

    province_state = serializers.ReadOnlyField(source="province_state.code")

    class Meta:
        model = Organization

        # Using all fields for now
        fields = (
            #'create_user',
            #'create_date',
            #'update_user',
            #'update_date',
            'org_guid',
            'name',
            'street_address',
            'city',
            'province_state',
            'postal_code',
            'main_tel',
            #'fax_tel',
            #'website_url',
            #'certificate_authority',
        )


class OrganizationSerializer(AuditModelSerializer):
    """
    Serializes Organization model fields (public fields list)
    """

    province_state = serializers.ReadOnlyField(source="province_state.code")
    contacts = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Organization
        fields = (
            'create_user',
            'create_date',
            'update_user',
            'update_date',
            'org_guid',
            'name',
            'street_address',
            'city',
            'province_state',
            'postal_code',
            'main_tel',
            'fax_tel',
            'website_url',
            'certificate_authority',
            'contacts',
        )


class PersonListSerializer(AuditModelSerializer):
    """
    Serializes the Person model for a list view (fewer fields than detail view)
    """

    companies = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Person
        fields = (
            # 'create_user',
            # 'create_date',
            # 'update_user',
            # 'update_date',
            'person_guid',
            'first_name',
            'surname',
            'companies',
        )


class PersonSerializer(AuditModelSerializer):
    """
    Serializes the Person model
    """

    companies = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Person
        fields = (
            'create_user',
            'create_date',
            'update_user',
            'update_date',
            'person_guid',
            'first_name',
            'surname',
            'companies',
        )<commit_msg>Include ProvinceState as a serializer field (to allow adding a province/state when creating an organization)<commit_after>from rest_framework import serializers
from gwells.models.ProvinceStateCode import ProvinceStateCode
from registries.models import (
    Organization,
    ContactAt,
    Person
)

class AuditModelSerializer(serializers.ModelSerializer):
    """
    Serializes AuditModel fields.
    Can be inherited into serializers for models that inherit from AuditModel
    """
    create_user = serializers.StringRelatedField()
    create_date = serializers.ReadOnlyField()
    update_user = serializers.StringRelatedField()
    update_date = serializers.ReadOnlyField()


class ContactAtSerializer(AuditModelSerializer):
    """
    Serializes ContactAt model fields.
    """
    person_name = serializers.StringRelatedField(source="person")
    organization_name = serializers.StringRelatedField(source="org")

    class Meta:
        model = ContactAt
        fields = (
            'contact_at_guid',
            'person',
            'org',
            'person_name',
            'organization_name',
            'contact_tel',
            'contact_email'
        )


# class ProvinceStateCodeSerializer(serializers.ModelSerializer):
#     """
#     Serializes Province/State objects for use by the Organization endpoints
#     """

#     class Meta:
#         model = ProvinceStateCode
#         fields = (
#             'province_state_guid',
#             'code',
#             'description',
#             'sort_order',
#         )


class OrganizationListSerializer(AuditModelSerializer):
    """
    Serializes Organization model fields for "list" view.
    """

    province_state = serializers.ReadOnlyField(source="province_state.code")

    class Meta:
        model = Organization

        # Using all fields for now
        fields = (
            # 'create_user',
            # 'create_date',
            # 'update_user',
            # 'update_date',
            'org_guid',
            'name',
            'street_address',
            'city',
            'province_state',
            'postal_code',
            'main_tel',
            #'fax_tel',
            #'website_url',
            #'certificate_authority',
        )


class OrganizationSerializer(AuditModelSerializer):
    """
    Serializes Organization model fields (public fields list)
    """

    province_state = serializers.PrimaryKeyRelatedField(queryset=ProvinceStateCode.objects.all(), required=False)
    contacts = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Organization
        fields = (
            'create_user',
            'create_date',
            'update_user',
            'update_date',
            'org_guid',
            'name',
            'street_address',
            'city',
            'province_state',
            'postal_code',
            'main_tel',
            'fax_tel',
            'website_url',
            'certificate_authority',
            'contacts',
        )


class PersonListSerializer(AuditModelSerializer):
    """
    Serializes the Person model for a list view (fewer fields than detail view)
    """

    companies = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Person
        fields = (
            # 'create_user',
            # 'create_date',
            # 'update_user',
            # 'update_date',
            'person_guid',
            'first_name',
            'surname',
            'companies',
        )


class PersonSerializer(AuditModelSerializer):
    """
    Serializes the Person model
    """

    companies = ContactAtSerializer(many=True, read_only=True)

    class Meta:
        model = Person
        fields = (
            'person_guid',
            'first_name',
            'surname',
            'companies',
            'create_user',
            'create_date',
            'update_user',
            'update_date',
        )
{'fraction_non_alphanumeric': 0.09561320114083934, 'fraction_numerical': 0.0, 'mean_word_length': 1.9550561797752808, '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': '5660554', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1901, 'n_words': 395}