<commit_before>##############################################################################
#
#    Copyright (C) 2021 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import random
import itertools

from odoo import api, models


class PartnerCommunication(models.Model):
    _inherit = "partner.communication.config"

    @api.multi
    def generate_test_cases_by_language_family_case(self, lang="de_DE", family_case="single", send_mode="digital"):
        """
        Generates example communications for our multiple cases in CH
        depending on the language and the family case
        Outputs the texts in a file
        :param lang:
        :return: True
        """
        self.ensure_one()

        comm_obj = self.env["partner.communication.job"].with_context(
            must_skip_send_to_printer=True)

        res = []

        for number_sponsorship in [1, 3, 4]:
            partner = self._find_partner(number_sponsorship, lang, family_case)
            if partner is None:
                continue
            object_ids = self._get_test_objects(partner)
            object_ids = ",".join([str(id)
                                  for id in object_ids[0:number_sponsorship]])
            temp_comm = comm_obj.create({
                "partner_id": partner.id,
                "config_id": self.id,
                "object_ids": object_ids,
                "auto_send": False,
                "send_mode": send_mode,
            })
            res.append({
                "case": f"{family_case}_{number_sponsorship}_child",
                "subject": temp_comm.subject,
                "body_html": temp_comm.body_html
            })
            temp_comm.unlink()

        return res

    @api.multi
    def generate_test_case_by_partner(self, partner=None, send_mode="digital"):
        """
        Generates example communications for our multiple cases in CH
        depending on partner
        Outputs the texts in a file
        :param partner:
        :return: True
        """
        self.ensure_one()

        comm_obj = self.env["partner.communication.job"].with_context(
            must_skip_send_to_printer=True)

        res = []

        object_ids = self._get_test_objects(partner)
        object_ids = ",".join([str(id) for id in object_ids])
        temp_comm = comm_obj.create({
            "partner_id": partner.id,
            "config_id": self.id,
            "object_ids": object_ids,
            "auto_send": False,
            "send_mode": send_mode,
        })
        res = {
            "case": "partner",
            "subject": temp_comm.subject,
            "body_html": temp_comm.body_html
        }
        temp_comm.unlink()

        return res

    def open_test_case_wizard(self):
        return {
            "name": "Test communication cases",
            "type": "ir.actions.act_window",
            "view_type": "form",
            "view_mode": "form",
            "context": self.env.context,
            "res_model": "partner.communication.test.cases.wizard",
            'target': 'current',
        }

    def _get_test_objects(self, partner):
        if self.model == "res.partner":
            object_ids = partner.ids
        elif self.model == "recurring.contract":
            object_ids = partner.sponsorship_ids.ids
        elif self.model == "correspondence":
            object_ids = partner.mapped("sponsorship_ids.child_letter_ids").ids
        elif self.model == "compassion.child":
            object_ids = partner.sponsored_child_ids.ids
        elif self.model == "account.invoice.line":
            object_ids = self.env["account.invoice.line"].search([
                ("partner_id", "=", partner.id),
                ("invoice_id.invoice_category", "=", "fund")
            ], limit=4).ids
        return object_ids

    def _find_partner(self, number_sponsorships, lang, family_case):
        family = self.env.ref("partner_compassion.res_partner_title_family")

        query = [
            ("number_sponsorships", "=", number_sponsorships),
            ("lang", "=", lang),
        ]
        if family_case == "single":
            query += [("title", "!=", family.id), ("title.plural", "=", False)]
        else:
            query += [("title", "=", family.id)]

        answers = self.env["res.partner"].search(query, limit=50)

        # check that the query returned a result
        if len(answers) <= 0:
            return None

        # randomly select one<commit_msg>CO-3807: Generate test cases fails<commit_after>##############################################################################
#
#    Copyright (C) 2021 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import random
import itertools

from odoo import api, models


class PartnerCommunication(models.Model):
    _inherit = "partner.communication.config"

    @api.multi
    def generate_test_cases_by_language_family_case(self, lang="de_DE", family_case="single", send_mode="digital"):
        """
        Generates example communications for our multiple cases in CH
        depending on the language and the family case
        Outputs the texts in a file
        :param lang:
        :return: True
        """
        self.ensure_one()

        comm_obj = self.env["partner.communication.job"].with_context(
            must_skip_send_to_printer=True)

        res = []

        for number_sponsorship in [1, 3, 4]:
            partner = self._find_partner(number_sponsorship, lang, family_case)
            if partner is None:
                continue
            object_ids = self._get_test_objects(partner)
            object_ids = ",".join([str(id)
                                  for id in object_ids[0:number_sponsorship]])
            temp_comm = comm_obj.create({
                "partner_id": partner.id,
                "config_id": self.id,
                "object_ids": object_ids,
                "auto_send": False,
                "send_mode": send_mode,
            })
            res.append({
                "case": f"{family_case}_{number_sponsorship}_child",
                "subject": temp_comm.subject,
                "body_html": temp_comm.body_html
            })
            temp_comm.unlink()

        return res

    @api.multi
    def generate_test_case_by_partner(self, partner=None, send_mode="digital"):
        """
        Generates example communications for our multiple cases in CH
        depending on partner
        Outputs the texts in a file
        :param partner:
        :return: True
        """
        self.ensure_one()

        comm_obj = self.env["partner.communication.job"].with_context(
            must_skip_send_to_printer=True)

        res = []

        object_ids = self._get_test_objects(partner)
        object_ids = ",".join([str(id) for id in object_ids])
        temp_comm = comm_obj.create({
            "partner_id": partner.id,
            "config_id": self.id,
            "object_ids": object_ids,
            "auto_send": False,
            "send_mode": send_mode,
        })
        res = {
            "case": "partner",
            "subject": temp_comm.subject,
            "body_html": temp_comm.body_html
        }
        temp_comm.unlink()

        return res

    def open_test_case_wizard(self):
        return {
            "name": "Test communication cases",
            "type": "ir.actions.act_window",
            "view_type": "form",
            "view_mode": "form",
            "context": self.env.context,
            "res_model": "partner.communication.test.cases.wizard",
            'target': 'current',
        }

    def _get_test_objects(self, partner):
        if self.model == "res.partner":
            object_ids = partner.ids
        elif self.model == "recurring.contract":
            object_ids = partner.sponsorship_ids.ids
        elif self.model == "correspondence":
            object_ids = partner.mapped("sponsorship_ids.child_letter_ids").ids
        elif self.model == "compassion.child":
            object_ids = partner.sponsored_child_ids.ids
        elif self.model == "account.invoice.line":
            object_ids = self.env["account.invoice.line"].search([
                ("partner_id", "=", partner.id),
                ("invoice_id.invoice_category", "=", "fund")
            ], limit=4).ids
        elif self.model == "account.invoice":
            object_ids = self.env["account.invoice"].search([
                ("partner_id", "=", partner.id)
            ], limit=4).ids
        return object_ids

    def _find_partner(self, number_sponsorships, lang, family_case):
        family = self.env.ref("partner_compassion.res_partner_title_family")

        query = [
            ("number_sponsorships", "=", number_sponsorships),
            ("lang", "=", lang),
        ]
        if family_case == "single":
            query += [("title", "!=", family.id), ("title.plural", "=", False)]
        else:
            query += [("title", "=", family.id)]

        answers = self.env["res.partner"].search(query, limit=50)

        # check that the query returned a result
        if len(answers) <= 0:
            return None

        # randomly select one
{'fraction_non_alphanumeric': 0.14526425954997382, 'fraction_numerical': 0.0030350601779173206, 'mean_word_length': 2.0815865849725896, 'pattern_counts': {'":': 57, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090520', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2651, 'n_words': 567}