<commit_before># Copyright 2017 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval

_logger = logging.getLogger(__name__)

VOLUNTARY_FIELD_NAMES = [
    "local_voluntary",
    "regional_voluntary",
    "national_voluntary",
    "local_only",
]


class MembershipRequest(models.Model):

    _inherit = "membership.request"

    protected_values = fields.Char(
        help="Protected values coming from the REST service."
    )

    @api.model
    def _onchange_partner_id_vals(
        self, is_company, request_type, partner_id, technical_name
    ):
        res = super()._onchange_partner_id_vals(
            is_company,
            request_type,
            partner_id,
            technical_name,
        )
        protected_values = self.env.context.get("protected_values", {})

        if isinstance(protected_values, str):
            try:
                protected_values = safe_eval(protected_values)
            except Exception:  # pylint: disable=broad-except
                _logger.warning(
                    "Invalid protected_values dictionary: %s",
                    protected_values,
                    exc_info=True,
                )
                protected_values = {}

        for field_name in VOLUNTARY_FIELD_NAMES:
            field_value = protected_values.get(field_name)
            if field_value is not None:
                res[field_name] = field_value

        return res
<commit_msg>[FIX] mozaik_membership_request_protected_values: case of false protected_values dict<commit_after># Copyright 2017 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval

_logger = logging.getLogger(__name__)

VOLUNTARY_FIELD_NAMES = [
    "local_voluntary",
    "regional_voluntary",
    "national_voluntary",
    "local_only",
]


class MembershipRequest(models.Model):

    _inherit = "membership.request"

    protected_values = fields.Char(
        help="Protected values coming from the REST service."
    )

    @api.model
    def _onchange_partner_id_vals(
        self, is_company, request_type, partner_id, technical_name
    ):
        res = super()._onchange_partner_id_vals(
            is_company,
            request_type,
            partner_id,
            technical_name,
        )
        protected_values = self.env.context.get("protected_values", {})
        if isinstance(protected_values, bool):
            protected_values = {}

        if isinstance(protected_values, str):
            try:
                protected_values = safe_eval(protected_values)
            except Exception:  # pylint: disable=broad-except
                _logger.warning(
                    "Invalid protected_values dictionary: %s",
                    protected_values,
                    exc_info=True,
                )
                protected_values = {}

        for field_name in VOLUNTARY_FIELD_NAMES:
            field_value = protected_values.get(field_name)
            if field_value is not None:
                res[field_name] = field_value

        return res

{'fraction_non_alphanumeric': 0.08012326656394453, 'fraction_numerical': 0.0036979969183359015, 'mean_word_length': 2.2170465807730424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5898156', 'n_tokens_mistral': 949, 'n_tokens_neox': 905, 'n_words': 190}