74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
from odoo import models
|
|
import logging
|
|
import re
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class MailTemplate(models.Model):
|
|
_inherit = "mail.template"
|
|
|
|
def send_mail(
|
|
self,
|
|
res_id,
|
|
force_send=False,
|
|
raise_exception=False,
|
|
email_values=None,
|
|
):
|
|
|
|
record = self.env[self.model].browse(res_id)
|
|
|
|
# determine recipient directly from business object
|
|
recipient_user = False
|
|
|
|
if self.model == "weekly.periodtimesheets":
|
|
|
|
if "Submitted" in (self.subject or ""):
|
|
recipient_user = (
|
|
record.employee_id.parent_id.user_id
|
|
)
|
|
|
|
elif (
|
|
"Approved" in (self.subject or "")
|
|
or "Rejected" in (self.subject or "")
|
|
):
|
|
recipient_user = (
|
|
record.employee_id.user_id
|
|
)
|
|
|
|
mail_id = super().send_mail(
|
|
res_id,
|
|
force_send=force_send,
|
|
raise_exception=raise_exception,
|
|
email_values=email_values,
|
|
)
|
|
|
|
try:
|
|
if (
|
|
recipient_user
|
|
and recipient_user.token
|
|
):
|
|
self.env["firebase.service"].send_notification([
|
|
{
|
|
"token": recipient_user.token,
|
|
"title": self.subject or "Notification",
|
|
"body": "You have received a new email notification",
|
|
"data": {
|
|
"mail_id": str(mail_id),
|
|
"model": self.model,
|
|
"record_id": str(res_id),
|
|
},
|
|
}
|
|
])
|
|
|
|
_logger.info(
|
|
"Firebase sent to %s",
|
|
recipient_user.name,
|
|
)
|
|
|
|
except Exception:
|
|
_logger.exception(
|
|
"Firebase notification failed"
|
|
)
|
|
|
|
return mail_id |