<commit_before>from rest_framework.serializers import (HyperlinkedIdentityField,
                                        HyperlinkedModelSerializer,
                                        PrimaryKeyRelatedField)

from contact.serializers import ContactSerializer
from invoice import models
from persons.serializers import CompanySerializer, PhysicalAddressSerializer


class FiscalPositionSerializer(HyperlinkedModelSerializer):
    companies = HyperlinkedIdentityField(
        view_name='api:invoice:fiscalposition-companies'
    )
    contacts = HyperlinkedIdentityField(
        view_name='api:invoice:fiscalposition-contacts'
    )

    class Meta:
        model = models.FiscalPosition
        fields = (
            'url',
            'id',
            'name',
            'companies',
            'contacts'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:fiscalposition-detail'
            }
        }


class ContactInvoiceSerializer(HyperlinkedModelSerializer):
    contact_contact = ContactSerializer()
    fiscal_position = PrimaryKeyRelatedField(
        queryset=models.FiscalPosition.objects.all()
    )
    fiscal_address = PhysicalAddressSerializer()
    invoices = HyperlinkedIdentityField(
        view_name='api:invoice:contact-invoices'
    )

    class Meta:
        model = models.ContactInvoice
        fields = (
            'url',
            'id',
            'contact_contact',
            'fiscal_position',
            'fiscal_address',
            'invoices'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:contactinvoice-detail'
            }
        }


class CompanyInvoiceSerializer(HyperlinkedModelSerializer):
    persons_company = CompanySerializer()
    fiscal_position = PrimaryKeyRelatedField(
        queryset=models.FiscalPosition.objects.all()
    )
    fiscal_address = PhysicalAddressSerializer()
    contacts = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-contacts'
    )
    products = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-products'
    )
    invoices = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-invoices'
    )

    class Meta:
        model = models.CompanyInvoice
        fields = (
            'url',
            'persons_company',
            'fiscal_position',
            'fiscal_address',
            'contacts',
            'products',
            'invoices'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:companyinvoice-detail'
            }
        }


class VATSerializer(HyperlinkedModelSerializer):
    products = HyperlinkedIdentityField(
        view_name='api:invoice:vat-products'
    )

    class Meta:<commit_msg>IMP invoice/serializers.py Same than previous commit, but this time for ContactInvoice and CompanyInvoice.<commit_after>from rest_framework.serializers import (HyperlinkedIdentityField,
                                        HyperlinkedModelSerializer,
                                        PrimaryKeyRelatedField)

from contact.serializers import ContactSerializer
from invoice import models
from persons.models import PhysicalAddress
from persons.serializers import CompanySerializer, PhysicalAddressSerializer


class FiscalPositionSerializer(HyperlinkedModelSerializer):
    companies = HyperlinkedIdentityField(
        view_name='api:invoice:fiscalposition-companies'
    )
    contacts = HyperlinkedIdentityField(
        view_name='api:invoice:fiscalposition-contacts'
    )

    class Meta:
        model = models.FiscalPosition
        fields = (
            'url',
            'id',
            'name',
            'companies',
            'contacts'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:fiscalposition-detail'
            }
        }


class ContactInvoiceSerializer(HyperlinkedModelSerializer):
    contact_contact = ContactSerializer()
    fiscal_position = PrimaryKeyRelatedField(
        queryset=models.FiscalPosition.objects.all()
    )
    fiscal_address = PhysicalAddressSerializer()
    invoices = HyperlinkedIdentityField(
        view_name='api:invoice:contact-invoices'
    )

    class Meta:
        model = models.ContactInvoice
        fields = (
            'url',
            'id',
            'contact_contact',
            'fiscal_position',
            'fiscal_address',
            'invoices'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:contactinvoice-detail'
            }
        }

    def create(self, validated_data):
        fiscal_address_data = validated_data.pop('fiscal_address')
        fiscal_address = PhysicalAddress.objects.create(
            **fiscal_address_data
        )
        validated_data['fiscal_address'] = fiscal_address
        contact = models.ContactInvoice.objects.create(**validated_data)
        return contact

    def update(self, instance, validated_data):
        fiscal_address_data = validated_data.pop('fiscal_address')
        fiscal_address = PhysicalAddress.objects.update_or_create(
            **fiscal_address_data
        )
        validated_data['fiscal_address'] = fiscal_address
        instance.update(**validated_data)
        return instance


class CompanyInvoiceSerializer(HyperlinkedModelSerializer):
    persons_company = CompanySerializer()
    fiscal_position = PrimaryKeyRelatedField(
        queryset=models.FiscalPosition.objects.all()
    )
    fiscal_address = PhysicalAddressSerializer()
    contacts = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-contacts'
    )
    products = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-products'
    )
    invoices = HyperlinkedIdentityField(
        view_name='api:invoice:companyinvoice-invoices'
    )

    class Meta:
        model = models.CompanyInvoice
        fields = (
            'url',
            'persons_company',
            'fiscal_position',
            'fiscal_address',
            'contacts',
            'products',
            'invoices'
        )
        extra_kwargs = {
            'url': {
                'view_name': 'api:invoice:companyinvoice-detail'
            }
        }

    def create(self, validated_data):
        fiscal_address_data = validated_data.pop('fiscal_address')
        fiscal_address = PhysicalAddress.objects.create(
            **fiscal_address_data
        )
        validated_data['fiscal_address'] = fiscal_address
        company = models.CompanyInvoice.objects.create(**validated_data)
        return company

    def update(self, instance, validated_data):
        fiscal_address_data = validated_data.pop('fiscal_address')
        fiscal_address = PhysicalAddress.objects.update_or_create(
            **fiscal_address_data
        )
        validated_data['fiscal_address'] = fiscal_address
        instance.update(**validated_data)
        return instance


class VATSerializer(HyperlinkedModelSerializer):
    products = HyperlinkedIdentityField(
        view_name='api:invoice:vat-products'
    )

    class Meta:
{'fraction_non_alphanumeric': 0.08166943982251802, 'fraction_numerical': 0.0, 'mean_word_length': 2.304168575355016, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320744', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1908, 'n_words': 289}