from datetime import timedelta from pytz import timezone, utc from odoo import models, fields, api class SurveySurvey(models.Model): _inherit = "survey.survey" @api.model def cron_close_passed_deadline_survey(self): to_close = self.search( [ ("deadline", "!=", False), ("deadline", "<", fields.Date.today()), ("auto_close", "=", True), ] ) if to_close: to_close.action_archive() deadline = fields.Date( string="Deadline", copy=False, tracking=True, help="The day after date at 00:00 will be set as default answer deadline on survey sharing.", ) auto_close = fields.Boolean( string="Auto closing", help="If checked, this survey will be automatically closed after the deadline.", ) active = fields.Boolean( tracking=True, ) def action_send_survey(self): self.ensure_one() action = super(SurveySurvey, self).action_send_survey() action["context"].update( { "default_deadline": timezone(self.env.user.tz) .localize( fields.Datetime.to_datetime(fields.Date.to_string(self.deadline)) + timedelta(days=1) ) .astimezone(utc), } ) return action @api.onchange("deadline") def onchange_deadline(self): self.ensure_one() if not self.deadline and self.auto_close: self.auto_close = False def _track_subtype(self, init_values): self.ensure_one() if "active" in init_values: return self.env.ref( "survey_deadline_autoclose.mail_message_subtype_survey_closing" ) return super(SurveySurvey, self)._track_subtype(init_values)