Search code examples
pythonpython-3.xodooodoo-14

Odoo, how to return the variable to popup from model


I have a custom method in odoo to send bulk messages with the exception. if the message is no error exception then mail_success += 1 then mail_err += 1. My goal was to display those variables as a popup on the same page, but I didn't get the popup. Here's the code models/sale_order.py:

from odoo import models
import logging

class SaleOrder(models.Model):
    _inherit = "sale.order"
    _description = "Sales order custom proforma multiple"

    def send_multiple_profoma_invoice(self):
        """
        for record in self._context.get('active_ids'):
            sale_order = self.env[self._context.get('active_model')].browse(record)
            so.join([record])
        """
        template_id = self._find_mail_template()

        logger = logging.getLogger("output")
        logger.setLevel(logging.DEBUG)

        fh = logging.FileHandler('/mnt/extra-addons/output.log')
        fh.setLevel(logging.DEBUG)
        logger.addHandler(fh)

        mail_sent, mail_err = 0, 0

        for record in self._context.get('active_ids'):
            try:
                sale_order = self.env[self._context.get('active_model')].browse(record)
                sale_order.with_context(
                    force_send=True
                ).message_post_with_template(
                    template_id, composition_mode='comment',
                    email_layout_xmlid="mail.mail_notification_paynow"
                )
                mail_sent += 1
                logger.info(f'The email is sent {str(sale_order)}')

            except Exception as e:
                mail_err += 1
                logger.error(e.with_traceback(e.__traceback__))

        return {
            'type': 'ir.actions.act_window',
            'name': 'My Action Name',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'my.model',
            'views': [([mail_sent, mail_err], 'form')],
            'target': 'new',
        }

I'm using odoo version 14


Solution

  • You can pass default values to fields in context using default_ prefix on field names.

    In the following example, mail_sent and mail_err are integer fields defined in my.model :

    Example:

    return {
            'type': 'ir.actions.act_window',
            'name': 'My Action Name',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'my.model',
            'views': [[False, 'form']],
            'target': 'new',
            'context': {'default_mail_sent': mail_sent, 'default_mail_err': mail_err}
        }  
    

    Note that views is a list of (view_id, view_type) pairs