From 3dccaba116d1d232e84705a92a434205bfae80b8 Mon Sep 17 00:00:00 2001
From: Juliana <juliana@le-filament.com>
Date: Wed, 16 Jun 2021 15:49:40 +0200
Subject: [PATCH] [ADD] Add mail send from survey if configurated
---
models/survey_survey.py | 38 ++++++++++++++++++++++++++++++--------
templates/layout.xml | 7 +++++++
views/survey_views.xml | 2 ++
3 files changed, 39 insertions(+), 8 deletions(-)
diff --git a/models/survey_survey.py b/models/survey_survey.py
index e5e0fca..05616a1 100644
--- a/models/survey_survey.py
+++ b/models/survey_survey.py
@@ -31,6 +31,10 @@ class Survey(models.Model):
stage_id = fields.Many2one('survey.stage', track_visibility='onchange')
is_button_display = fields.Boolean("Afficher le bouton enregistrer")
is_mail_auto = fields.Boolean("Envoi du mail automatique")
+ question_mail_id = fields.Many2one('survey.question', string="Question pour le Mail d'envoi")
+ template_id = fields.Many2one(
+ 'mail.template', 'Template de mail', index=True,
+ domain="[('model', '=', 'survey.survey')]")
def _track_subtype(self, init_values):
self.ensure_one()
@@ -56,17 +60,10 @@ class SurveyLabel(models.Model):
class SurveyUserInputLine(models.Model):
- _inherit = "survey.user_input"
+ _inherit = "survey.user_input_line"
stage_id = fields.Many2one('survey.stage', string="Etape du sondage", related='survey_id.stage_id', store=True, readonly=False)
- @api.onchange('state')
- def on_change_state(self):
- if self.state == 'done':
- if self.survey_id.is_mail_auto:
- return
- # Envoi du mail automatique
-
class SurveyUserInput(models.Model):
_inherit = "survey.user_input"
@@ -92,3 +89,28 @@ class SurveyUserInput(models.Model):
'target': 'new',
'url': '/survey/%s/%s/%s' % (url_tag, self.survey_id.id, self.token)
}
+
+ @api.onchange('state')
+ def on_change_state(self):
+ # At the end of the survey, send a mail if survey is configured for that
+ if self.state == 'done':
+ if self.survey_id.is_mail_auto:
+ self.action_send_mail()
+
+ @api.multi
+ def action_send_mail(self):
+ self.ensure_one()
+ if self.survey_id.question_mail_id:
+ # Get the mail to send in answers
+ mail = self.env['survey.user_input_line'].search([
+ ('survey_id', '=', self.survey_id.id),
+ ('user_input_id', '=', self.id),
+ ('question_id', '=', self.survey_id.question_mail_id.id),
+ ('value_text', '!=', False)
+ ], limit=1).value_text
+ if mail:
+ template = self.survey_id.template_id
+ if template:
+ template.email_to = mail
+ template.with_context(
+ dbname=self._cr.dbname).send_mail(self.survey_id.id, force_send=True)
diff --git a/templates/layout.xml b/templates/layout.xml
index 3c98d7f..a774069 100644
--- a/templates/layout.xml
+++ b/templates/layout.xml
@@ -235,5 +235,12 @@
</xpath>
</template>
+ <template id="numerical_box" inherit_id="survey.numerical_box">
+<!-- <input type="number" step="any" class="form-control" t-att-name="prefix"/>-->
+ <xpath expr="//input" position="replace">
+ <input type="range" min="1" max="100" class="form-control slider" t-att-name="prefix"/>
+ </xpath>
+ </template>
+
</data>
</odoo>
diff --git a/views/survey_views.xml b/views/survey_views.xml
index 030cc1f..f2961f5 100644
--- a/views/survey_views.xml
+++ b/views/survey_views.xml
@@ -39,6 +39,8 @@
<xpath expr="//notebook//page//group//field[@name='users_can_go_back']" position="after">
<field name="is_button_display" />
<field name="is_mail_auto" />
+ <field name="question_mail_id" attrs="{'invisible': [('is_mail_auto', '=', False)]}"/>
+ <field name="template_id" attrs="{'invisible': [('is_mail_auto', '=', False)]}"/>
</xpath>
<xpath expr="//div[hasclass('oe_title')]" position="after">
--
GitLab