|
|
@ -57,23 +57,20 @@ class MailComposeMessage(models.TransientModel): |
|
|
'scheduled_date': self.schedule_time, |
|
|
'scheduled_date': self.schedule_time, |
|
|
'recipient_ids': partner_list, |
|
|
'recipient_ids': partner_list, |
|
|
'attachment_ids': attachment_list, |
|
|
'attachment_ids': attachment_list, |
|
|
|
|
|
'scheduled_user_tz': pytz.timezone(self.env.context.get('tz') or self.env.user.tz), |
|
|
}) |
|
|
}) |
|
|
# Get the user's timezone (fallback to UTC if not set) |
|
|
# Get the user's timezone (fallback to UTC if not set) |
|
|
user_tz = self.env.context.get('tz') or self.env.user.tz or 'UTC' |
|
|
user_tz = self.env.context.get('tz') or self.env.user.tz or 'UTC' |
|
|
|
|
|
|
|
|
# Get current UTC time and convert to user's timezone |
|
|
# Get current UTC time and convert to user's timezone |
|
|
utc_current_datetime = fields.Datetime.now() # Naive UTC datetime |
|
|
utc_current_datetime = fields.Datetime.now() # Naive UTC datetime |
|
|
local_current_datetime = fields.Datetime.context_timestamp( |
|
|
local_current_datetime = fields.Datetime.context_timestamp( |
|
|
self.with_context(tz=user_tz), timestamp=utc_current_datetime |
|
|
self.with_context(tz=user_tz), timestamp=utc_current_datetime |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
# Handle schedule_time (assumed to be UTC in the database) |
|
|
# Handle schedule_time (assumed to be UTC in the database) |
|
|
schedule_time = self.schedule_time |
|
|
schedule_time = self.schedule_time |
|
|
|
|
|
|
|
|
if isinstance(schedule_time, str): |
|
|
if isinstance(schedule_time, str): |
|
|
# If schedule_time is a string, parse it to datetime (assuming UTC) |
|
|
# If schedule_time is a string, parse it to datetime (assuming UTC) |
|
|
schedule_time = fields.Datetime.from_string(schedule_time) |
|
|
schedule_time = fields.Datetime.from_string(schedule_time) |
|
|
|
|
|
|
|
|
# Convert schedule_time to user's timezone |
|
|
# Convert schedule_time to user's timezone |
|
|
if schedule_time: |
|
|
if schedule_time: |
|
|
schedule_time = fields.Datetime.context_timestamp( |
|
|
schedule_time = fields.Datetime.context_timestamp( |
|
|
@ -107,29 +104,31 @@ class MailComposeMessage(models.TransientModel): |
|
|
return {'type': 'ir.actions.client', 'tag': 'reload'} |
|
|
return {'type': 'ir.actions.client', 'tag': 'reload'} |
|
|
|
|
|
|
|
|
def action_send_scheduled_mail(self): |
|
|
def action_send_scheduled_mail(self): |
|
|
"""This function is called by a scheduled action in each minute to |
|
|
"""Executed by cron every minute. Sends emails whose scheduled datetime |
|
|
send the scheduled mails""" |
|
|
has passed according to the timezone of the user who scheduled them. |
|
|
utc_current_datetime = fields.Datetime.now() |
|
|
""" |
|
|
# Access the time zone |
|
|
Mail = self.env['mail.mail'] |
|
|
user_tz = pytz.timezone(self.env.context.get( |
|
|
# Fetch all mails having schedule date set |
|
|
'tz') or self.env.user.tz) |
|
|
scheduled_mails = Mail.search([('scheduled_date', '!=', False)]) |
|
|
# Access local time |
|
|
for mail in scheduled_mails: |
|
|
date_today = pytz.utc.localize(utc_current_datetime).astimezone( |
|
|
# If no timezone stored, fallback to UTC |
|
|
user_tz) |
|
|
user_tz = mail.scheduled_user_tz or 'UTC' |
|
|
# Converted to string and removed the utc time difference |
|
|
# Convert current UTC time → user's timezone |
|
|
user_current_datetime = date_today.strftime( |
|
|
user_local_now = fields.Datetime.context_timestamp( |
|
|
'%Y-%m-%d %H:%M:%S') |
|
|
mail.with_context(tz=user_tz), |
|
|
# Again converted to datetime type and replace the seconds with 0 |
|
|
fields.Datetime.now() |
|
|
user_current_date = datetime.datetime.strptime( |
|
|
).replace(second=0, microsecond=0) |
|
|
user_current_datetime, |
|
|
# Convert scheduled UTC → user's timezone |
|
|
"%Y-%m-%d %H:%M:%S").replace( |
|
|
scheduled_dt = fields.Datetime.context_timestamp( |
|
|
second=0) |
|
|
mail.with_context(tz=user_tz), |
|
|
scheduled_mail_rec = self.env['mail.mail'].search( |
|
|
mail.scheduled_date |
|
|
[('scheduled_date', '<=', user_current_date)]) |
|
|
).replace(second=0, microsecond=0) |
|
|
if scheduled_mail_rec: |
|
|
# If scheduled time has passed, send mail |
|
|
for record in scheduled_mail_rec: |
|
|
if scheduled_dt <= user_local_now: |
|
|
record.send() |
|
|
mail.send() |
|
|
planned_activity = self.env['mail.activity'].search( |
|
|
# Remove any linked scheduled activity |
|
|
[('schedule_mail_id', '=', record.id)]) |
|
|
activity = self.env['mail.activity'].search( |
|
|
# unlink the planned activity |
|
|
[('schedule_mail_id', '=', mail.id)] |
|
|
planned_activity.sudo().action_feedback(self) |
|
|
) |
|
|
|
|
|
if activity: |
|
|
|
|
|
activity.sudo().action_feedback() |
|
|
|