[REF] calendar: prepare alarm communication configuration
Purpose of this commit is to ease sending mail to attendees using template by using a template record instead of an xml id. Indeed this allow having flows using a configurable template instead of an hardcoded one. Code in calendar_sms is split into main models to ease future improvements related to SMS. Taks ID-2191254 COM PR odoo/odoo#68443
This commit is contained in:
committed by
Thibault Delavallée
parent
502e89ed6a
commit
f87a0eb763
@@ -8,18 +8,6 @@ class Alarm(models.Model):
|
||||
_name = 'calendar.alarm'
|
||||
_description = 'Event Alarm'
|
||||
|
||||
@api.depends('interval', 'duration')
|
||||
def _compute_duration_minutes(self):
|
||||
for alarm in self:
|
||||
if alarm.interval == "minutes":
|
||||
alarm.duration_minutes = alarm.duration
|
||||
elif alarm.interval == "hours":
|
||||
alarm.duration_minutes = alarm.duration * 60
|
||||
elif alarm.interval == "days":
|
||||
alarm.duration_minutes = alarm.duration * 60 * 24
|
||||
else:
|
||||
alarm.duration_minutes = 0
|
||||
|
||||
_interval_selection = {'minutes': 'Minutes', 'hours': 'Hours', 'days': 'Days'}
|
||||
|
||||
name = fields.Char('Name', translate=True, required=True)
|
||||
@@ -34,6 +22,18 @@ class Alarm(models.Model):
|
||||
search='_search_duration_minutes', compute='_compute_duration_minutes',
|
||||
help="Duration in minutes")
|
||||
|
||||
@api.depends('interval', 'duration')
|
||||
def _compute_duration_minutes(self):
|
||||
for alarm in self:
|
||||
if alarm.interval == "minutes":
|
||||
alarm.duration_minutes = alarm.duration
|
||||
elif alarm.interval == "hours":
|
||||
alarm.duration_minutes = alarm.duration * 60
|
||||
elif alarm.interval == "days":
|
||||
alarm.duration_minutes = alarm.duration * 60 * 24
|
||||
else:
|
||||
alarm.duration_minutes = 0
|
||||
|
||||
def _search_duration_minutes(self, operator, value):
|
||||
return [
|
||||
'|', '|',
|
||||
|
||||
@@ -168,7 +168,7 @@ class AlarmManager(models.AbstractModel):
|
||||
events = self._get_events_to_notify('email')
|
||||
attendees = events.attendee_ids.filtered(lambda a: a.state != 'declined')
|
||||
attendees.with_context(calendar_template_ignore_recurrence=True)._send_mail_to_attendees(
|
||||
'calendar.calendar_template_meeting_reminder',
|
||||
self.env.ref('calendar.calendar_template_meeting_reminder', raise_if_not_found=False),
|
||||
force_send=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -87,20 +87,19 @@ class Attendee(models.Model):
|
||||
partners = (event.attendee_ids & self).partner_id & event.message_partner_ids
|
||||
event.message_unsubscribe(partner_ids=partners.ids)
|
||||
|
||||
def _send_mail_to_attendees(self, template_xmlid, force_send=False):
|
||||
def _send_mail_to_attendees(self, mail_template, force_send=False):
|
||||
""" Send mail for event invitation to event attendees.
|
||||
:param template_xmlid: xml id of the email template to use to send the invitation
|
||||
:param mail_template: a mail.template record
|
||||
:param force_send: if set to True, the mail(s) will be sent immediately (instead of the next queue processing)
|
||||
"""
|
||||
res = False
|
||||
|
||||
if isinstance(mail_template, str):
|
||||
raise ValueError('Template should be a template record, not an XML ID anymore.')
|
||||
if self.env['ir.config_parameter'].sudo().get_param('calendar.block_mail') or self._context.get("no_mail_to_attendees"):
|
||||
return res
|
||||
return False
|
||||
if not mail_template:
|
||||
_logger.warning("No template passed to %s notification process. Skipped.", self)
|
||||
return False
|
||||
|
||||
invitation_template = self.env.ref(template_xmlid, raise_if_not_found=False)
|
||||
if not invitation_template:
|
||||
_logger.warning("Template %s could not be found. %s not notified." % (template_xmlid, self))
|
||||
return
|
||||
# get ics file for all meetings
|
||||
ics_files = self.mapped('event_id')._get_ics_file()
|
||||
|
||||
@@ -116,12 +115,12 @@ class Attendee(models.Model):
|
||||
'mimetype': 'text/calendar',
|
||||
'datas': base64.b64encode(ics_file)})
|
||||
]
|
||||
body = invitation_template._render_field(
|
||||
body = mail_template._render_field(
|
||||
'body_html',
|
||||
attendee.ids,
|
||||
compute_lang=True,
|
||||
post_process=True)[attendee.id]
|
||||
subject = invitation_template._render_field(
|
||||
subject = mail_template._render_field(
|
||||
'subject',
|
||||
attendee.ids,
|
||||
compute_lang=True)[attendee.id]
|
||||
|
||||
@@ -382,7 +382,9 @@ class Meeting(models.Model):
|
||||
detached_events = event._apply_recurrence_values(recurrence_values)
|
||||
detached_events.active = False
|
||||
|
||||
events.filtered(lambda event: event.start > fields.Datetime.now()).attendee_ids._send_mail_to_attendees('calendar.calendar_template_meeting_invitation')
|
||||
events.filtered(lambda event: event.start > fields.Datetime.now()).attendee_ids._send_mail_to_attendees(
|
||||
self.env.ref('calendar.calendar_template_meeting_invitation', raise_if_not_found=False)
|
||||
)
|
||||
events._sync_activities(fields={f for vals in vals_list for f in vals.keys()})
|
||||
|
||||
events._setup_alarms()
|
||||
@@ -475,7 +477,9 @@ class Meeting(models.Model):
|
||||
|
||||
current_attendees = self.filtered('active').attendee_ids
|
||||
if 'partner_ids' in values:
|
||||
(current_attendees - previous_attendees)._send_mail_to_attendees('calendar.calendar_template_meeting_invitation')
|
||||
(current_attendees - previous_attendees)._send_mail_to_attendees(
|
||||
self.env.ref('calendar.calendar_template_meeting_invitation', raise_if_not_found=False)
|
||||
)
|
||||
if 'start' in values:
|
||||
start_date = fields.Datetime.to_datetime(values.get('start'))
|
||||
# Only notify on future events
|
||||
@@ -483,7 +487,7 @@ class Meeting(models.Model):
|
||||
(current_attendees & previous_attendees).with_context(
|
||||
calendar_template_ignore_recurrence=not update_recurrence
|
||||
)._send_mail_to_attendees(
|
||||
'calendar.calendar_template_meeting_changedate'
|
||||
self.env.ref('calendar.calendar_template_meeting_changedate', raise_if_not_found=False)
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -557,7 +561,9 @@ class Meeting(models.Model):
|
||||
email = self.env.user.email
|
||||
if email:
|
||||
for meeting in self:
|
||||
meeting.attendee_ids._send_mail_to_attendees('calendar.calendar_template_meeting_invitation')
|
||||
meeting.attendee_ids._send_mail_to_attendees(
|
||||
self.env.ref('calendar.calendar_template_meeting_invitation', raise_if_not_found=False)
|
||||
)
|
||||
return True
|
||||
|
||||
def action_mass_mailing(self):
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import calendar
|
||||
from . import calendar_alarm
|
||||
from . import calendar_alarm_manager
|
||||
from . import calendar_event
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class CalendarAlarm(models.Model):
|
||||
_inherit = 'calendar.alarm'
|
||||
|
||||
alarm_type = fields.Selection(selection_add=[
|
||||
('sms', 'SMS Text Message')
|
||||
], ondelete={'sms': 'set default'})
|
||||
@@ -0,0 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class AlarmManager(models.AbstractModel):
|
||||
_inherit = 'calendar.alarm_manager'
|
||||
|
||||
@api.model
|
||||
def _send_reminder(self):
|
||||
""" Cron method, overridden here to send SMS reminders as well
|
||||
"""
|
||||
super()._send_reminder()
|
||||
self._get_events_to_notify(ttype='sms')._do_sms_reminder()
|
||||
+1
-24
@@ -1,11 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import logging
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
from odoo import models, _
|
||||
|
||||
|
||||
class CalendarEvent(models.Model):
|
||||
@@ -42,22 +38,3 @@ class CalendarEvent(models.Model):
|
||||
|
||||
def _get_trigger_alarm_types(self):
|
||||
return super()._get_trigger_alarm_types() + ['sms']
|
||||
|
||||
|
||||
class CalendarAlarm(models.Model):
|
||||
_inherit = 'calendar.alarm'
|
||||
|
||||
alarm_type = fields.Selection(selection_add=[
|
||||
('sms', 'SMS Text Message')
|
||||
], ondelete={'sms': 'set default'})
|
||||
|
||||
|
||||
class AlarmManager(models.AbstractModel):
|
||||
_inherit = 'calendar.alarm_manager'
|
||||
|
||||
@api.model
|
||||
def _send_reminder(self):
|
||||
""" Cron method, overridden here to send SMS reminders as well
|
||||
"""
|
||||
super()._send_reminder()
|
||||
self._get_events_to_notify(ttype='sms')._do_sms_reminder()
|
||||
@@ -10,14 +10,14 @@ class Attendee(models.Model):
|
||||
_name = 'calendar.attendee'
|
||||
_inherit = 'calendar.attendee'
|
||||
|
||||
def _send_mail_to_attendees(self, template_xmlid, force_send=False):
|
||||
def _send_mail_to_attendees(self, mail_template, force_send=False):
|
||||
""" Override
|
||||
If not synced with Google, let Odoo in charge of sending emails
|
||||
Otherwise, nothing to do: Google will send them
|
||||
"""
|
||||
with google_calendar_token(self.env.user.sudo()) as token:
|
||||
if not token:
|
||||
super()._send_mail_to_attendees(template_xmlid, force_send)
|
||||
super()._send_mail_to_attendees(mail_template, force_send)
|
||||
|
||||
def write(self, vals):
|
||||
res = super().write(vals)
|
||||
|
||||
@@ -10,11 +10,11 @@ class Attendee(models.Model):
|
||||
_name = 'calendar.attendee'
|
||||
_inherit = 'calendar.attendee'
|
||||
|
||||
def _send_mail_to_attendees(self, template_xmlid, force_send=False):
|
||||
def _send_mail_to_attendees(self, mail_template, force_send=False):
|
||||
""" Override the super method
|
||||
If not synced with Microsoft Outlook, let Odoo in charge of sending emails
|
||||
Otherwise, Microsoft Outlook will send them
|
||||
"""
|
||||
with microsoft_calendar_token(self.env.user.sudo()) as token:
|
||||
if not token:
|
||||
super()._send_mail_to_attendees(template_xmlid, force_send)
|
||||
super()._send_mail_to_attendees(mail_template, force_send)
|
||||
|
||||
Reference in New Issue
Block a user