<commit_before>from rest_framework.renderers import JSONRenderer

from api.organisation.serializers import (
    OrganisationDocumentLinkSerializer, OrganisationNameSerializer,
    OrganisationRecipientOrgBudgetSerializer,
    OrganisationTotalBudgetSerializer, OrganisationTotalExpenditureSerializer
)
from solr.activity.references import ReportingOrgReference
from solr.indexing import BaseIndexing
from solr.organisation.references import (
    DocumentLinkOrgReference, NameOrgReference,
    RecipientCountryBudgetOrgReference, RecipientOrgBudgetOrgReference,
    RecipientRegionBudgetOrgReference, TotalBudgetOrgReference,
    TotalExpenditureOrgReference
)
from solr.organisation.serializers import (
    OrganisationRecipientCountryBudgetSerializer,
    OrganisationRecipientRegionBudgetSerializer
)
from solr.utils import (
    bool_string, date_string, decimal_string, get_child_attr,
    get_narrative_lang_list, value_string
)


class OrganisationIndexing(BaseIndexing):

    def total_expenditure(self):
        total_expenditure_all = self.record.total_expenditure.all()
        if total_expenditure_all:
            self.add_field('organisation_total_expenditure', [])
            self.add_field('organisation_total_expenditure_period_start', [])
            self.add_field('organisation_total_expenditure_period_end', [])
            self.add_field('organisation_total_expenditure_value', [])
            self.add_field('organisation_total_expenditure_value_currency', [])
            self.add_field('organisation_total_expenditure_value_date', [])
            self.add_field('organisation_total_expenditure_xml', [])

            for total_expenditure in total_expenditure_all:
                self.add_value_list(
                    'organisation_total_expenditure_period_start',
                    date_string(total_expenditure.period_start)
                )
                self.add_value_list(
                    'organisation_total_expenditure_period_end',
                    date_string(total_expenditure.period_end)
                )
                self.add_value_list(
                    'organisation_total_expenditure_value',
                    value_string(total_expenditure.value)
                )
                self.add_value_list(
                    'organisation_total_expenditure_value_currency',
                    total_expenditure.currency_id
                )
                self.add_value_list(
                    'organisation_total_expenditure_value_date',
                    date_string(total_expenditure.value_date)
                )

                self.add_value_list(
                    'organisation_total_expenditure',
                    JSONRenderer().render(
                        OrganisationTotalExpenditureSerializer(
                            instance=total_expenditure
                        ).data
                    ).decode()
                )
                self.add_value_list(
                    'organisation_total_expenditure_xml',
                    TotalExpenditureOrgReference(
                        organisation_total_expenditure=total_expenditure
                    ).to_string()
                )

    def document_links(self):
        document_link_all = self.record.organisationdocumentlink_set.all()
        if document_link_all:
            self.add_field('organisation_document_link', [])
            self.add_field('organisation_document_link_url', [])
            self.add_field('organisation_document_link_format', [])
            self.add_field('organisation_document_link_document_date', [])
            self.add_field('organisation_document_link_xml', [])

            for document_link in document_link_all:
                self.add_value_list(
                    'organisation_document_link_url',
                    document_link.url
                )
                self.add_value_list(
                    'organisation_document_link_format',
                    document_link.file_format_id
                )
                self.add_value_list(
                    'organisation_document_link_document_date',
                    value_string(document_link.iso_date)
                )

                self.add_value_list(
                    'organisation_document_link',
                    JSONRenderer().render(
                        OrganisationDocumentLinkSerializer(document_link).data
                    ).decode()
                )
                self.add_value_list(
                    'organisation_document_link_xml',
                    DocumentLinkOrgReference(
                        organisation_document_link=document_link
                    ).to_string()
                )

    def related_budget(
            self,
            related_budget_all,
            serializer,
            prefix="organisation_total_budget"
    ):
        if related_budget_all:
            self.add_field(prefix + '_period_start', [])
            self.add_field(prefix + '_period_end', [])
            self.add_field(prefix + '_value', [])
            self.add_field(prefix + '_value_currency', [])
            self.add_field(prefix + '_value_date', [])
            self.add_field(prefix, [])

            if prefix == 'organisation_total_budget':
                self.add_field('organisation_total_budget_xml', [])
            elif prefix == 'organisation_recipient_org_budget':
                self.add_field('organisation_recipient_org_budget_xml', [])
            elif prefix == 'organisation_recipient_region_budget':
                self.add_field('organisation_recipient_region_budget_xml', [])
            elif prefix == 'organisation_recipient_country_budget':
                self.add_field('organisation_recipient_country_budget_xml', [])

            for related_budget in related_budget_all:
                self.add_value_list(
                    prefix + '_period_start',
                    value_string(related_budget.period_start)
                )
                self.add_value_list(
                    prefix + '_period_end',
                    value_string(related_budget.period_end)
                )
                self.add_value_list(
                    prefix + '_value',
                    decimal_string(related_budget.value)
                )
                self.add_value_list(
                    prefix + '_value_currency',
                    related_budget.currency_id
                )
                self.add_value_list(
                    prefix + '_value_date',
                    value_string(related_budget.value_date)
                )

                self.add_value_list(
                    prefix,
                    JSONRenderer().render(
                        serializer(related_budget).data
                    ).decode()
                )

                if prefix == 'organisation_total_budget':
                    self.add_value_list(
                        'organisation_total_budget_xml',
                        TotalBudgetOrgReference(
                            organisation_total_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_org_budget':
                    self.add_value_list(
                        'organisation_recipient_org_budget_xml',
                        RecipientOrgBudgetOrgReference(
                            organisation_recipient_org_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_region_budget':
                    self.add_value_list(
                        'organisation_recipient_region_budget_xml',
                        RecipientRegionBudgetOrgReference(
                            organisation_recipient_region_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_country_budget':
                    self.add_value_list(
                        'organisation_recipient_country_budget_xml',
                        RecipientCountryBudgetOrgReference(
                            organisation_recipient_country_budget=
                            related_budget
                        ).to_string()<commit_msg>PEP8 updated<commit_after>from rest_framework.renderers import JSONRenderer

from api.organisation.serializers import (
    OrganisationDocumentLinkSerializer, OrganisationNameSerializer,
    OrganisationRecipientOrgBudgetSerializer,
    OrganisationTotalBudgetSerializer, OrganisationTotalExpenditureSerializer
)
from solr.activity.references import ReportingOrgReference
from solr.indexing import BaseIndexing
from solr.organisation.references import (
    DocumentLinkOrgReference, NameOrgReference,
    RecipientCountryBudgetOrgReference, RecipientOrgBudgetOrgReference,
    RecipientRegionBudgetOrgReference, TotalBudgetOrgReference,
    TotalExpenditureOrgReference
)
from solr.organisation.serializers import (
    OrganisationRecipientCountryBudgetSerializer,
    OrganisationRecipientRegionBudgetSerializer
)
from solr.utils import (
    bool_string, date_string, decimal_string, get_child_attr,
    get_narrative_lang_list, value_string
)


class OrganisationIndexing(BaseIndexing):

    def total_expenditure(self):
        total_expenditure_all = self.record.total_expenditure.all()
        if total_expenditure_all:
            self.add_field('organisation_total_expenditure', [])
            self.add_field('organisation_total_expenditure_period_start', [])
            self.add_field('organisation_total_expenditure_period_end', [])
            self.add_field('organisation_total_expenditure_value', [])
            self.add_field('organisation_total_expenditure_value_currency', [])
            self.add_field('organisation_total_expenditure_value_date', [])
            self.add_field('organisation_total_expenditure_xml', [])

            for total_expenditure in total_expenditure_all:
                self.add_value_list(
                    'organisation_total_expenditure_period_start',
                    date_string(total_expenditure.period_start)
                )
                self.add_value_list(
                    'organisation_total_expenditure_period_end',
                    date_string(total_expenditure.period_end)
                )
                self.add_value_list(
                    'organisation_total_expenditure_value',
                    value_string(total_expenditure.value)
                )
                self.add_value_list(
                    'organisation_total_expenditure_value_currency',
                    total_expenditure.currency_id
                )
                self.add_value_list(
                    'organisation_total_expenditure_value_date',
                    date_string(total_expenditure.value_date)
                )

                self.add_value_list(
                    'organisation_total_expenditure',
                    JSONRenderer().render(
                        OrganisationTotalExpenditureSerializer(
                            instance=total_expenditure
                        ).data
                    ).decode()
                )
                self.add_value_list(
                    'organisation_total_expenditure_xml',
                    TotalExpenditureOrgReference(
                        organisation_total_expenditure=total_expenditure
                    ).to_string()
                )

    def document_links(self):
        document_link_all = self.record.organisationdocumentlink_set.all()
        if document_link_all:
            self.add_field('organisation_document_link', [])
            self.add_field('organisation_document_link_url', [])
            self.add_field('organisation_document_link_format', [])
            self.add_field('organisation_document_link_document_date', [])
            self.add_field('organisation_document_link_xml', [])

            for document_link in document_link_all:
                self.add_value_list(
                    'organisation_document_link_url',
                    document_link.url
                )
                self.add_value_list(
                    'organisation_document_link_format',
                    document_link.file_format_id
                )
                self.add_value_list(
                    'organisation_document_link_document_date',
                    value_string(document_link.iso_date)
                )

                self.add_value_list(
                    'organisation_document_link',
                    JSONRenderer().render(
                        OrganisationDocumentLinkSerializer(document_link).data
                    ).decode()
                )
                self.add_value_list(
                    'organisation_document_link_xml',
                    DocumentLinkOrgReference(
                        organisation_document_link=document_link
                    ).to_string()
                )

    def related_budget(
            self,
            related_budget_all,
            serializer,
            prefix="organisation_total_budget"
    ):
        if related_budget_all:
            self.add_field(prefix + '_period_start', [])
            self.add_field(prefix + '_period_end', [])
            self.add_field(prefix + '_value', [])
            self.add_field(prefix + '_value_currency', [])
            self.add_field(prefix + '_value_date', [])
            self.add_field(prefix, [])

            if prefix == 'organisation_total_budget':
                self.add_field('organisation_total_budget_xml', [])
            elif prefix == 'organisation_recipient_org_budget':
                self.add_field('organisation_recipient_org_budget_xml', [])
            elif prefix == 'organisation_recipient_region_budget':
                self.add_field('organisation_recipient_region_budget_xml', [])
            elif prefix == 'organisation_recipient_country_budget':
                self.add_field('organisation_recipient_country_budget_xml', [])

            for related_budget in related_budget_all:
                self.add_value_list(
                    prefix + '_period_start',
                    value_string(related_budget.period_start)
                )
                self.add_value_list(
                    prefix + '_period_end',
                    value_string(related_budget.period_end)
                )
                self.add_value_list(
                    prefix + '_value',
                    decimal_string(related_budget.value)
                )
                self.add_value_list(
                    prefix + '_value_currency',
                    related_budget.currency_id
                )
                self.add_value_list(
                    prefix + '_value_date',
                    value_string(related_budget.value_date)
                )

                self.add_value_list(
                    prefix,
                    JSONRenderer().render(
                        serializer(related_budget).data
                    ).decode()
                )

                if prefix == 'organisation_total_budget':
                    self.add_value_list(
                        'organisation_total_budget_xml',
                        TotalBudgetOrgReference(
                            organisation_total_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_org_budget':
                    self.add_value_list(
                        'organisation_recipient_org_budget_xml',
                        RecipientOrgBudgetOrgReference(
                            organisation_recipient_org_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_region_budget':
                    self.add_value_list(
                        'organisation_recipient_region_budget_xml',
                        RecipientRegionBudgetOrgReference(
                            organisation_recipient_region_budget=related_budget
                        ).to_string()
                    )
                elif prefix == 'organisation_recipient_country_budget':
                    self.add_value_list(
                        'organisation_recipient_country_budget_xml',
                        RecipientCountryBudgetOrgReference(
                            organisation_recipient_country_budget=related_budget  # NOQA: E501
                        ).to_string()
{'fraction_non_alphanumeric': 0.06376567020250723, 'fraction_numerical': 0.00024108003857280618, 'mean_word_length': 1.6801809077693426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '3378254', 'n_tokens_mistral': 4359, 'n_tokens_neox': 4013, 'n_words': 443}