简体   繁体   中英

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

Just extend Mass Mailing model with a new date field and implement a model method to use for a daily running ir.cron .

from odoo import api, fields, models

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

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

    @api.model
    def run_send_recurring(self):
        """ Resend mass mailing with recurring interval"""
        domain = [('recurrence_mail', '!=', False)]
        # TODO monthly should be solved in another way, but that
        # is not needed for this example
        deltas = {'daily': 1, 'weekly': 7, 'monthly': 30}
        today = fields.Date.today()
        for mass_mail in self.search(domain):
            # never sent? go send it
            if not mass_mail.last_sent_on:
                # send the way you want
            # or get delta between today and last_sent_on
            last_dt = fields.Date.from_string(mass_mail.last_sent_on)
            if (today - last_dt).days >= deltas[mass_mail.recurrence_mail]:
                # send the way you want

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM