from odoo import models, fields, api, _ from odoo.exceptions import UserError class SurveySurvey(models.Model): _inherit = 'survey.survey' @api.model def close_deadline_survey(self): deadline = self.search([ ('date_deadline', '!=', False), ('date_deadline', '<', fields.Date.today()), ]) to_close = deadline.filtered('auto_close') if to_close: to_close.action_close_survey() for survey in deadline - to_close: survey.message_post(subtype='survey_deadline_autoclose.mail_message_subtype_survey_deadline') date_deadline = fields.Date(string="Deadline", copy=False) auto_close = fields.Boolean(string="Auto close", default=False, help="If checked, the survey will be closed automatically when deadline is overpassed.") # ACTIONS @api.multi def action_send_survey(self): self.ensure_one() action = super(SurveySurvey, self).action_send_survey() action['context'].update({ 'default_date_deadline': self.date_deadline, }) return action @api.multi def action_close_survey(self): stage = self.env['survey.stage'].search([('closed', '=', True)], limit=1) if not stage: raise UserError(_("No \"closed\" status found, the requested operation is impossible.")) self.write({ 'stage_id': stage.id }) for survey in self: survey.message_post(subtype='survey_deadline_autoclose.mail_message_subtype_survey_closed') # ONCHANGES @api.onchange('date_deadline') def onchange_is_template(self): self.ensure_one() if not self.date_deadline: self.auto_close = False elif not self.auto_close: self.auto_close = True