如何每隔一段时间调用一次函数Odoo 11

时间:2019-02-07 10:36:08

标签: python-3.x odoo odoo-11

我知道我们可以在odoo中使用cron创建自动动作 但我想要一些不同的东西 在odoo的群发邮件中,我想添加邮件群发邮件的重复选项 表单view_mail_mass_mailing_form> 选项页面中的示例
我添加了一个重复选择字段, 我添加此邮件是因为我希望仅发送大量邮件

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

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

我希望此批量邮件每隔几天(几天,几周或几个月)发送一次 如何使用间隔日期调用函数 如何每隔几天(几天,几周或几个月)调用一个函数

此群发邮件的发送自创建之日起恢复

2 个答案:

答案 0 :(得分:2)

只需使用新的日期字段扩展“群发邮件”模型,并实现用于日常运行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

答案 1 :(得分:0)

感谢@CZoellner的帮助 我找到了您想法的解决方案

#解决方案############### .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()