<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Authors: <NAME>
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
import base64
import re

from openerp.osv import orm, fields
from openerp.tools import SUPERUSER_ID
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)

MODE = ['in', 'out']
TEST_MSG = 'test'

MATCH_EMAIL = re.compile('\<(.*)\>', re.IGNORECASE)


class distribution_list(orm.Model):

    _name = 'distribution.list'
    _inherit = ['distribution.list', 'mail.thread']

    def _set_active_ids(self, cr, uid, dl_id, msg, context):
        """
        First check that email_from is a known email of the system.
        Depending the result of this search:
        * Send a mail-test if subject is "test"
        * Set `active_ids` into `context` with the resulting ids of
            distribution_list
        :type dl_id: integer
        :param dl_id: id of a distribution list
        :type context: {}
        :param context: context to set key `active_ids`
        """
        res_id = self._get_mailing_object(
            cr, uid, dl_id, msg['email_from'], context=context)
        if not res_id:
            _logger.warning('An Unknown Email '
                            'Address (%s) ' % msg['email_from'] +
                            'Try to Use Distribution List for Forwarding')
        elif msg['subject'] == TEST_MSG:
            # do not send to all contact: this is just a test
            context['active_ids'] = [res_id]
            context['dl_computed'] = True

    def _get_mailing_object(
            self, cr, uid, dl_id, email_from, mailing_model=False,
            email_field='email', context=None):
        """
        :type email_from: char
        :param email_from: email to find
        :type mailing_model: char
        :param mailing_model: a given model name to search on
        :type email_field: char
        :param email_field: name of the columns that possibly contains the
            email to search
        :rtype: integer or boolean
        :rparam: id of the object that contains `email_from` or False
        """
        res = re.findall(MATCH_EMAIL, email_from)
        email_from = res and res[0] or email_from

        if not mailing_model:
            dl = self.browse(cr, uid, dl_id, context=context)
            mailing_model = dl.dst_model_id.model

        mailing_object = self.pool[mailing_model]
        domain = [('%s' % email_field, '=', email_from)]

        mailing_ids = mailing_object.search(
            cr, uid, domain, context=context)
        return mailing_ids and mailing_ids[0] or False

    def _get_attachment_id(self, cr, uid, datas, context=None):
        ir_attach_vals = {
            'name': datas[0],
            'datas_fname': datas[0],
            'datas': base64.encodestring(datas[1]),
            'res_model': 'mail.compose.message',
        }
        return self.pool['ir.attachment'].create(
            cr, uid, ir_attach_vals, context=context)

    def _get_mail_compose_message_vals(
            self, cr, uid, msg, dl_id, context=None):
        dl = self.browse(cr, uid, dl_id, context=context)

        attachment_ids = []
        if msg.get('attachments', False):
            for attachment in msg['attachments']:
                attachment_ids.append(self._get_attachment_id(
                    cr, uid, attachment, context=context))
        return {
            'email_from': msg.get('from', False),
            'composition_mode': 'mass_mail',
            'subject': msg.get('subject', False),
            'body': msg.get('body', False),
            'distribution_list_id': dl_id,
            'mass_mailing_name': 'Mass Mailing %s' % dl.name,
            'model': dl.dst_model_id.model,
            'attachment_ids': [[6, 0, attachment_ids]],
        }

    def _mail_alias_id_function(self, cr, uid, ids, name, args, context=None):
        """
        If `distributrion.list.mail_forwarding` is True then a mail.alias
        must be set for this distribution list. If there is no one then create
        a `mail.alias` based on the distribution list name.
        Keep current `mail_alias_id` for other cases.
        """
        result = {i: False for i in ids}
        for dl_vals in self.read(
                cr, uid, ids, ['mail_forwarding', 'mail_alias_id', 'name'],
                context=context):
            if dl_vals['mail_forwarding'] and not dl_vals['mail_alias_id']:
                result[dl_vals['id']] = self.generate_alias(
                    cr, uid, dl_vals['id'], dl_vals['name'], context=context)
            else:
                # For all other cases keep the current value
                result[dl_vals['id']] = dl_vals['mail_alias_id'] and\
                    dl_vals['mail_alias_id'][0] or False
<commit_msg>[mass_mailing_distribution_list] [FIX] mailing model is not always the same that the distribution list dst_model_id<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Authors: <NAME>
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
import base64
import re

from openerp.osv import orm, fields
from openerp.tools import SUPERUSER_ID
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)

MODE = ['in', 'out']
TEST_MSG = 'test'

MATCH_EMAIL = re.compile('\<(.*)\>', re.IGNORECASE)


class distribution_list(orm.Model):

    _name = 'distribution.list'
    _inherit = ['distribution.list', 'mail.thread']

    def _set_active_ids(self, cr, uid, dl_id, msg, context):
        """
        First check that email_from is a known email of the system.
        Depending the result of this search:
        * Send a mail-test if subject is "test"
        * Set `active_ids` into `context` with the resulting ids of
            distribution_list
        :type dl_id: integer
        :param dl_id: id of a distribution list
        :type context: {}
        :param context: context to set key `active_ids`
        """
        res_id = self._get_mailing_object(
            cr, uid, dl_id, msg['email_from'], context=context)
        if not res_id:
            _logger.warning('An Unknown Email '
                            'Address (%s) ' % msg['email_from'] +
                            'Try to Use Distribution List for Forwarding')
        elif msg['subject'] == TEST_MSG:
            # do not send to all contact: this is just a test
            context['active_ids'] = [res_id]
            context['dl_computed'] = True

    def _get_mailing_object(
            self, cr, uid, dl_id, email_from, mailing_model=False,
            email_field='email', context=None):
        """
        :type email_from: char
        :param email_from: email to find
        :type mailing_model: char
        :param mailing_model: a given model name to search on
        :type email_field: char
        :param email_field: name of the columns that possibly contains the
            email to search
        :rtype: integer or boolean
        :rparam: id of the object that contains `email_from` or False
        """
        res = re.findall(MATCH_EMAIL, email_from)
        email_from = res and res[0] or email_from

        if not mailing_model:
            dl = self.browse(cr, uid, dl_id, context=context)
            mailing_model = dl.dst_model_id.model

        mailing_object = self.pool[mailing_model]
        domain = [('%s' % email_field, '=', email_from)]

        mailing_ids = mailing_object.search(
            cr, uid, domain, context=context)
        return mailing_ids and mailing_ids[0] or False

    def _get_attachment_id(self, cr, uid, datas, context=None):
        ir_attach_vals = {
            'name': datas[0],
            'datas_fname': datas[0],
            'datas': base64.encodestring(datas[1]),
            'res_model': 'mail.compose.message',
        }
        return self.pool['ir.attachment'].create(
            cr, uid, ir_attach_vals, context=context)

    def _get_mail_compose_message_vals(
            self, cr, uid, msg, dl_id, mailing_model=False, context=None):
        dl = self.browse(cr, uid, dl_id, context=context)
        if not mailing_model:
            mailing_model = dl.dst_model_id.model

        attachment_ids = []
        if msg.get('attachments', False):
            for attachment in msg['attachments']:
                attachment_ids.append(self._get_attachment_id(
                    cr, uid, attachment, context=context))
        return {
            'email_from': msg.get('from', False),
            'composition_mode': 'mass_mail',
            'subject': msg.get('subject', False),
            'body': msg.get('body', False),
            'distribution_list_id': dl_id,
            'mass_mailing_name': 'Mass Mailing %s' % dl.name,
            'model': mailing_model,
            'attachment_ids': [[6, 0, attachment_ids]],
        }

    def _mail_alias_id_function(self, cr, uid, ids, name, args, context=None):
        """
        If `distributrion.list.mail_forwarding` is True then a mail.alias
        must be set for this distribution list. If there is no one then create
        a `mail.alias` based on the distribution list name.
        Keep current `mail_alias_id` for other cases.
        """
        result = {i: False for i in ids}
        for dl_vals in self.read(
                cr, uid, ids, ['mail_forwarding', 'mail_alias_id', 'name'],
                context=context):
            if dl_vals['mail_forwarding'] and not dl_vals['mail_alias_id']:
                result[dl_vals['id']] = self.generate_alias(
                    cr, uid, dl_vals['id'], dl_vals['name'], context=context)
            else:
                # For all other cases keep the current value
                result[dl_vals['id']] = dl_vals['mail_alias_id'] and\
                    dl_vals['mail_alias_id'][0] or False

{'fraction_non_alphanumeric': 0.11925042589437819, 'fraction_numerical': 0.0029204185933317107, 'mean_word_length': 2.489385791112369, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '670873', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3401, 'n_words': 1145}