[英]How to call a function every interval time 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')
我希望這個群發郵件發送每個(幾天或幾周或幾個月)如何調用一個具有間隔日期的函數,如何每隔(幾天或幾周或幾個月)調用一個函數
此群發郵件自創建之日起恢復發送
只需使用新的日期字段擴展群發郵件模型,並實現用於日常運行的模型方法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
謝謝@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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.