<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
from tools.translate import _
import base64
import time
import tools
from crm import crm

class crm_send_new_email(osv.osv_memory):
    """ Sends new email for the case"""
    _name = "crm.send.mail"

crm_send_new_email()

class crm_send_new_email_attachment(osv.osv_memory):
    _name = 'crm.send.mail.attachment'

    _columns = {
        'binary' : fields.binary('Attachment', required=True),
        'name' : fields.char('Name', size=128, required=True),

        'wizard_id' : fields.many2one('crm.send.mail', 'Wizard', required=True),
    }

crm_send_new_email_attachment()

class crm_send_new_email(osv.osv_memory):
    """ Sends new email for the case"""
    _name = "crm.send.mail"
    _description = "Send new email"

    _columns = {
        'email_to' : fields.char('To', size=64, required=True),
        'email_from' : fields.char('From', size=64, required=True),
        'email_cc' : fields.char('CC', size=128, help="Carbon Copy: list of recipients that will receive"\
                                    " a copy of this mail, and future communication related to this case"),
        'subject': fields.char('Subject', size=128, required=True),
        'text': fields.text('Message', required=True),
        'state': fields.selection(crm.AVAILABLE_STATES, string='Set New State To', required=True),

        'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
    }

    def action_cancel(self, cr, uid, ids, context=None):
        """ Closes Phonecall to Opportunity form
        """
        return {'type':'ir.actions.act_window_close'}

    def action_send(self, cr, uid, ids, context=None):
        """ This sends an email to ALL the addresses of the selected partners.
        """

        hist_obj = self.pool.get('mailgate.message')
        smtp_pool = self.pool.get('email.smtpclient')

        if not context:
            context = {}

        if not context.get('model'):
            raise osv.except_osv(_('Error'), _('Can not send mail!'))

        model = context.get('model')
        case_pool = self.pool.get(model)
        res_id = context and context.get('active_id', False) or False

        for obj in self.browse(cr, uid, ids, context=context):
            attach = [
                (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
            ]

            message_id = None

            case = case_pool.browse(cr, uid, res_id)
            if context.get('mail', 'new') == 'new':
                if case.message_ids:
                    message_id = case.message_ids[0].message_id
            else:
                hist = hist_obj.browse(cr, uid, res_id)
                message_id = hist.message_id
                model = hist.model
                model_pool = self.pool.get(model)
                res_id = hist.res_id
                case = model_pool.browse(cr, uid, res_id)
            emails = [obj.email_to]
            email_cc = (obj.email_cc or '').split(',')
            emails = filter(None, emails)
            body = obj.text

            body = case_pool.format_body(body)
            email_from = getattr(obj, 'email_from', False)
            x_headers = {}
            if message_id:
                x_headers['References'] = "%s" % (message_id)

            flag = tools.email_send(
                email_from,
                emails,
                obj.subject,
                body,
                email_cc=email_cc,
                attach=attach,
                reply_to=case.section_id and case.section_id.reply_to,
                openobject_id=str(case.id),
                x_headers=x_headers
            )

            if not flag:
                raise osv.except_osv(_('Error!'), _('Unable to send mail. Please check SMTP is configured properly.'))
            if flag:
                case_pool._history(cr, uid, [case], _('Send'), history=True, \
                                email=obj.email_to, details=body, \
                                subject=obj.subject, email_from=email_from, \
                                message_id=message_id, attach=attach)
                if obj.state == 'unchanged':
                    pass
                elif obj.state == 'done':
                    case_pool.case_close(cr, uid, [case.id])
                elif obj.state == 'draft':
                    case_pool.case_reset(cr, uid, [case.id])
                elif obj.state in ['cancel', 'open', 'pending']:
                    act = 'case_' + obj.state
                    getattr(case_pool, act)(cr, uid, [case.id])
                cr.commit()

        return {}

    def default_get(self, cr, uid, fields, context=None):
        """
        This function gets default values
        """
        if not context:
            context = {}

        if not context.get('model'):
            raise osv.except_osv(_('Error'), _('Can not send mail!'))

        res = super(crm_send_new_email, self).default_get(cr, uid, fields, context=context)

        if context.get('mail') == 'reply':
            res.update(self.get_reply_defaults(cr, uid, fields, context=context))
            return res

        model = context.get('model')
        mod_obj = self.pool.get(model)
        res_id = context and context.get('active_ids', []) or []

        user_obj = self.pool.get('res.users')
        user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]

        for case in mod_obj.browse(cr, uid, res_id):
            if 'email_to' in fields:
                res.update({'email_to': case.email_from})
            if 'email_from' in fields:
                res.update({'email_from': user_mail_from})
            if 'subject' in fields:
                res.update({'subject': ustr(context.get('subject', case.name) or '')})
            if 'email_cc' in fields:
                res.update({'email_cc': case.email_cc or ''})
            if 'text' in fields:
                res.update({'text': '\n\n'+(case.user_id.signature or '')})
            if 'state' in fields:
                res.update({'state': 'pending'})

        return res

    def get_reply_defaults(self, cr, uid, fields, context=None):<commit_msg>[IMP] crm: another encoding issue fixed in crm + cleanup<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
from tools.translate import _
import base64
import time
import tools
from crm import crm

class crm_send_new_email(osv.osv_memory):
    """ Sends new email for the case"""
    _name = "crm.send.mail"

crm_send_new_email()

class crm_send_new_email_attachment(osv.osv_memory):
    _name = 'crm.send.mail.attachment'

    _columns = {
        'binary' : fields.binary('Attachment', required=True),
        'name' : fields.char('Name', size=128, required=True),

        'wizard_id' : fields.many2one('crm.send.mail', 'Wizard', required=True),
    }

crm_send_new_email_attachment()

class crm_send_new_email(osv.osv_memory):
    """ Sends new email for the case"""
    _name = "crm.send.mail"
    _description = "Send new email"

    _columns = {
        'email_to' : fields.char('To', size=64, required=True),
        'email_from' : fields.char('From', size=64, required=True),
        'email_cc' : fields.char('CC', size=128, help="Carbon Copy: list of recipients that will receive"\
                                    " a copy of this mail, and future communication related to this case"),
        'subject': fields.char('Subject', size=128, required=True),
        'text': fields.text('Message', required=True),
        'state': fields.selection(crm.AVAILABLE_STATES, string='Set New State To', required=True),

        'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
    }

    def action_cancel(self, cr, uid, ids, context=None):
        """ Closes Phonecall to Opportunity form
        """
        return {'type':'ir.actions.act_window_close'}

    def action_send(self, cr, uid, ids, context=None):
        """ This sends an email to ALL the addresses of the selected partners.
        """

        hist_obj = self.pool.get('mailgate.message')
        smtp_pool = self.pool.get('email.smtpclient')

        if not context:
            context = {}

        if not context.get('model'):
            raise osv.except_osv(_('Error'), _('Can not send mail!'))

        model = context.get('model')
        case_pool = self.pool.get(model)
        res_id = context and context.get('active_id', False) or False

        for obj in self.browse(cr, uid, ids, context=context):
            attach = [
                (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
            ]

            message_id = None

            case = case_pool.browse(cr, uid, res_id)
            if context.get('mail', 'new') == 'new':
                if case.message_ids:
                    message_id = case.message_ids[0].message_id
            else:
                hist = hist_obj.browse(cr, uid, res_id)
                message_id = hist.message_id
                model = hist.model
                model_pool = self.pool.get(model)
                res_id = hist.res_id
                case = model_pool.browse(cr, uid, res_id)
            emails = [obj.email_to]
            email_cc = (obj.email_cc or '').split(',')
            emails = filter(None, emails)
            body = obj.text

            body = case_pool.format_body(body)
            email_from = getattr(obj, 'email_from', False)
            x_headers = {}
            if message_id:
                x_headers['References'] = "%s" % (message_id)

            flag = tools.email_send(
                email_from,
                emails,
                obj.subject,
                body,
                email_cc=email_cc,
                attach=attach,
                reply_to=case.section_id and case.section_id.reply_to,
                openobject_id=str(case.id),
                x_headers=x_headers
            )

            if not flag:
                raise osv.except_osv(_('Error!'), _('Unable to send mail. Please check SMTP is configured properly.'))
            if flag:
                case_pool._history(cr, uid, [case], _('Send'), history=True, \
                                email=obj.email_to, details=body, \
                                subject=obj.subject, email_from=email_from, \
                                message_id=message_id, attach=attach)
                if obj.state == 'unchanged':
                    pass
                elif obj.state == 'done':
                    case_pool.case_close(cr, uid, [case.id])
                elif obj.state == 'draft':
                    case_pool.case_reset(cr, uid, [case.id])
                elif obj.state in ['cancel', 'open', 'pending']:
                    act = 'case_' + obj.state
                    getattr(case_pool, act)(cr, uid, [case.id])
                cr.commit()

        return {}

    def default_get(self, cr, uid, fields, context=None):
        """
        This function gets default values
        """
        if not context:
            context = {}

        if not context.get('model'):
            raise osv.except_osv(_('Error'), _('Can not send mail!'))

        res = super(crm_send_new_email, self).default_get(cr, uid, fields, context=context)

        if context.get('mail') == 'reply':
            res.update(self.get_reply_defaults(cr, uid, fields, context=context))
            return res

        model = context.get('model')
        mod_obj = self.pool.get(model)
        res_id = context and context.get('active_ids', []) or []

        user_obj = self.pool.get('res.users')
        user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]

        for case in mod_obj.browse(cr, uid, res_id):
            if 'email_to' in fields:
                res.update({'email_to': case.email_from})
            if 'email_from' in fields:
                res.update({'email_from': user_mail_from})
            if 'subject' in fields:
                res.update({'subject': tools.ustr(context.get('subject', case.name) or '')})
            if 'email_cc' in fields:
                res.update({'email_cc': case.email_cc or ''})
            if 'text' in fields:
                res.update({'text': '\n\n'+(case.user_id.signature or '')})
            if 'state' in fields:
                res.update({'state': 'pending'})

        return res

    def get_reply_defaults(self, cr, uid, fields, context=None):
{'fraction_non_alphanumeric': 0.1376937513032599, 'fraction_numerical': 0.004170431639674706, 'mean_word_length': 2.093528273489572, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6869196', 'n_tokens_mistral': 4270, 'n_tokens_neox': 4068, 'n_words': 1075}