Search code examples
python-3.xodooodoo-11

How to call a function every interval time Odoo 11


i know we can create automatic action using cron in odoo but I want something a different in the mass mailing of odoo i want to add a repetion option of mail mass mailings Example in the Form view_mail_mass_mailing_form > Options page
I added a repetition selection field, I added this because I want each mass mail alone

class MailMassMailing(models.Model):
_inherit = 'mail.mass_mailing' 

recurrence_mail = fields.Selection([
    ('daily', 'Day'),
    ('weekly', 'Weeks'),
    ('monthly', 'Months'),
], string='Recurring')

I want this mass mailng to send each (days or weeks or months) how to call a function with interval date, how to call a function every (days or weeks or months)

The sending of this mass mailing is revived from the date of creation


Solution

  • Thank you @CZoellner for your help I found the solution with your idea

    # Solution ############### .py
    @api.model
    def run_send_recurring(self):
        """ Resend mass mailing with recurring interval"""
        date_format = '%Y-%m-%d'
        domain = [('recurrence_mail', '!=', False),('state','=','done')]
        deltas = {'daily': 1, 'weekly': 7, 'monthly': 30}
        logger.info("______deltas________: %s ",deltas)
        today = fields.Date.today()
        logger.info("______today________: %s ",today) 
        for mass_mail in self.search(domain):
            logger.info("______mass_mail________: %s ",mass_mail)
            # never sent? go send it
            if not mass_mail.last_sent_on:
                self.put_in_queue()  
    
            joining_date = mass_mail.last_sent_on
            current_date = (datetime.today()).strftime(date_format)
            print('joining_date',joining_date)
            d1 = datetime.strptime(joining_date, date_format).date()
            logger.info("______1 day________: %s ",d1)
            d2 = datetime.strptime(current_date, date_format).date()
            logger.info("______2 day________: %s ",d2)
            logger.info("______deltas[mass_mail.recurrence_mail]________: %s ",deltas[mass_mail.recurrence_mail])
            r = relativedelta(d1,d2)    
            logger.info("______r day________: %s ",r.days)  
            if (r ,'>=' ,  deltas[mass_mail.recurrence_mail]):
                mass_mail.put_in_queue()