Пример #1
0
    def _notify(self, cr, uid, msg_id, context=None):
        """ Send by email the notification depending on the user preferences """
        context = context or {}
        # mail_noemail (do not send email) or no partner_ids: do not send, return
        if context.get('mail_noemail'):
            return True
        msg = self.pool.get('mail.message').browse(cr, uid, msg_id, context=context)

        notify_partner_ids = self.get_partners_to_notify(cr, uid, msg, context=context)
        if not notify_partner_ids:
            return True

        mail_mail = self.pool.get('mail.mail')
        # add signature
        body_html = msg.body
        signature = msg.author_id and msg.author_id.user_ids[0].signature or ''
        if signature:
            body_html = tools.append_content_to_html(body_html, signature)

        mail_values = {
            'mail_message_id': msg.id,
            'email_to': [],
            'auto_delete': True,
            'body_html': body_html,
            'state': 'outgoing',
        }
        mail_values['email_to'] = ', '.join(mail_values['email_to'])
        email_notif_id = mail_mail.create(cr, uid, mail_values, context=context)
        return mail_mail.send(cr, uid, [email_notif_id], recipient_ids=notify_partner_ids, context=context)
Пример #2
0
    def generate_email(self, cr, uid, template_id, res_id, context=None):
        """Generates an email from the template for given (model, res_id) pair.

           :param template_id: id of the template to render.
           :param res_id: id of the record to use for rendering the template (model
                          is taken from template definition)
           :returns: a dict containing all relevant fields for creating a new
                     mail.mail entry, with one extra key ``attachments``, in the
                     format expected by :py:meth:`mail_thread.message_post`.
        """
        if context is None:
            context = {}
        report_xml_pool = self.pool.get('ir.actions.report.xml')
        template = self.get_email_template(cr, uid, template_id, res_id, context)
        values = {}
        for field in ['subject', 'body_html', 'email_from',
                      'email_to', 'email_cc', 'reply_to']:
            values[field] = self.render_template(cr, uid, getattr(template, field),
                                                 template.model, res_id, context=context) \
                                                 or False
        if template.user_signature:
            signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
            values['body_html'] = append_content_to_html(values['body_html'], signature)

        if values['body_html']:
            values['body'] = html_sanitize(values['body_html'])

        values.update(mail_server_id=template.mail_server_id.id or False,
                      auto_delete=template.auto_delete,
                      model=template.model,
                      res_id=res_id or False)

        attachments = []
        # Add report in attachments
        if template.report_template:
            report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
            report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
            # Ensure report is rendered using template's language
            ctx = context.copy()
            if template.lang:
                ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)
            service = netsvc.LocalService(report_service)
            (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
            result = base64.b64encode(result)
            if not report_name:
                report_name = report_service
            ext = "." + format
            if not report_name.endswith(ext):
                report_name += ext
            attachments.append((report_name, result))

        # Add template attachments
        for attach in template.attachment_ids:
            attachments.append((attach.datas_fname, attach.datas))

        values['attachments'] = attachments
        return values
Пример #3
0
 def send_get_mail_body(self, cr, uid, mail, partner=None, context=None):
     """ add a signin link inside the body of a mail.mail
         :param mail: mail.mail browse_record
         :param partner: browse_record of the specific recipient partner
         :return: the resulting body_html
     """
     body = super(mail_mail, self).send_get_mail_body(cr, uid, mail, partner, context=context)
     if partner:
         context = dict(context or {}, signup_valid=True)
         partner = self.pool.get('res.partner').browse(cr, uid, partner.id, context)
         body = tools.append_content_to_html(body, "Log in our portal at: %s" % partner.signup_url)
     return body
Пример #4
0
 def _message_extract_payload(self, message, save_original=False):
     """Extract body as HTML and attachments from the mail message"""
     attachments = []
     body = u''
     if save_original:
         attachments.append(('original_email.eml', message.as_string()))
     if not message.is_multipart() or 'text/' in message.get('content-type', ''):
         encoding = message.get_content_charset()
         body = message.get_payload(decode=True)
         body = tools.ustr(body, encoding, errors='replace')
         if message.get_content_type() == 'text/plain':
             # text/plain -> <pre/>
             body = tools.append_content_to_html(u'', body)
     else:
         alternative = (message.get_content_type() == 'multipart/alternative')
         for part in message.walk():
             if part.get_content_maintype() == 'multipart':
                 continue # skip container
             filename = part.get_filename() # None if normal part
             encoding = part.get_content_charset() # None if attachment
             # 1) Explicit Attachments -> attachments
             if filename or part.get('content-disposition', '').strip().startswith('attachment'):
                 attachments.append((filename or 'attachment', part.get_payload(decode=True)))
                 continue
             # 2) text/plain -> <pre/>
             if part.get_content_type() == 'text/plain' and (not alternative or not body):
                 body = tools.append_content_to_html(body, tools.ustr(part.get_payload(decode=True),
                                                                      encoding, errors='replace'))
             # 3) text/html -> raw
             elif part.get_content_type() == 'text/html':
                 html = tools.ustr(part.get_payload(decode=True), encoding, errors='replace')
                 if alternative:
                     body = html
                 else:
                     body = tools.append_content_to_html(body, html, plaintext=False)
             # 4) Anything else -> attachment
             else:
                 attachments.append((filename or 'attachment', part.get_payload(decode=True)))
     return body, attachments
    def generate_email(self, cr, uid, template_id, res_id, context=None):
        """This is a copy of generate_email from email.template, with a patch as below.
        """
        if context is None:
            context = {}
        report_xml_pool = self.pool.get('ir.actions.report.xml')
        template = self.get_email_template(cr, uid, template_id, res_id, context)
        values = {}
        for field in ['subject', 'body_html', 'email_from',
                      'email_to', 'email_recipients', 'email_cc', 'reply_to']:
            values[field] = self.render_template(cr, uid, getattr(template, field),
                                                 template.model, res_id, context=context) \
                                                 or False
        if template.user_signature:
            signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
            values['body_html'] = tools.append_content_to_html(values['body_html'], signature)

        if values['body_html']:
            values['body'] = tools.html_sanitize(values['body_html'])

        values.update(mail_server_id=template.mail_server_id.id or False,
                      auto_delete=template.auto_delete,
                      model=template.model,
                      res_id=res_id or False)

        attachments = []
        # Add report in attachments
        if template.report_template:
            report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
            report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
            # Ensure report is rendered using template's language
            ctx = context.copy()
            if template.lang:
                ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)

            # Start of Patch.

#           service = netsvc.LocalService(report_service)
#           (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)

            if not report_xml_pool.browse(cr, uid, template.report_template.id, context).is_pentaho_report:
                # Standard service call for non-Pentaho reports
                service = netsvc.LocalService(report_service)
                (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
            else:
                # Call the report as a duplicate of the current user to remove the user concurrency issue.
                # NOTE: This works HOWEVER, if the temp user is in the in the 'portal' or 'anonymous' security groups
                #       then rendering the Pentaho report may fail because the user is denied read access to res.partner.
                #       See security rule 'res_partner: read access on my partner'.
                crtemp = pooler.get_db(cr.dbname).cursor()

                #Remove default_partner_id set by search view that could duplicate user with existing partner!
                # Use copied context, to ensure we don't affect any processing outside of this method's scope.
                ctx.pop('default_partner_id', None)

                user_obj = self.pool.get('res.users')
                existing_uids = user_obj.search(crtemp, SUPERUSER_ID, [('login', '=', "%s (copy)" % user_obj.browse(crtemp, SUPERUSER_ID, uid, context=ctx).login)], context=ctx)
                if existing_uids:
                    self._unlink_user_and_partner(crtemp, uid, existing_uids, context=ctx)

                new_uid = user_obj.copy(crtemp, SUPERUSER_ID, uid, default={}, context=ctx)
                crtemp.commit()

                service = netsvc.LocalService(report_service)
                (result, format) = service.create(crtemp, new_uid, [res_id], {'model': template.model}, ctx)
                crtemp.commit()
                crtemp.close()

                crtemp = pooler.get_db(cr.dbname).cursor()

                self._unlink_user_and_partner(crtemp, uid, [new_uid], context=ctx)

                crtemp.commit()
                crtemp.close()

            # End of Patch

            result = base64.b64encode(result)
            if not report_name:
                report_name = report_service
            ext = "." + format
            if not report_name.endswith(ext):
                report_name += ext
            attachments.append((report_name, result))

        # Add template attachments
        for attach in template.attachment_ids:
            attachments.append((attach.datas_fname, attach.datas))

        values['attachments'] = attachments
        return values
Пример #6
0
 def _mock_send_get_mail_body(self, *args, **kwargs):
     # def _send_get_mail_body(self, cr, uid, mail, partner=None, context=None)
     body = tools.append_content_to_html(args[2].body_html, kwargs.get('partner').name if kwargs.get('partner') else 'No specific partner')
     return body