Hey i am trying to send the result of survey to employee but i am getting the singleton error but i am already using for loops. Kindly have a look at my code
def _prepare_result_receivers(self):
"""
@return: returns a list of tuple (survey, list of employees).
"""
appraisal_send_to = []
if self.emp_appr:
appraisal_send_to.append((self.survey_id, self.emp_appr))
return appraisal_send_to
@api.multi
def throw_appraisal(self):
ComposeMessage = self.env['survey.mail.compose.message']
for appraisal in self:
appraisal_send_to = appraisal._prepare_result_receivers()
for survey,receivers in appraisal_send_to:
for employee in receivers:
email = employee.related_partner_id.email or employee.work_email
if not email:
continue
render_template = appraisal.result_template_id.with_context(email=email, survey=survey, employee=employee).generate_email([appraisal.id])
values = {
'survey_id': survey.id,
'public': 'email_private',
'partner_ids': employee.related_partner_id and [(4, employee.related_partner_id.id)] or False,
'multi_email': email,
'subject': '%s appraisal: %s' % (appraisal.employee_id.name, survey.title),
'body': render_template[appraisal.id]['body'],
'date_deadline': appraisal.date_close,
'model': appraisal._name,
'res_id': appraisal.id,
}
compose_message_wizard = ComposeMessage.with_context(active_id=appraisal.id, active_model=appraisal._name, notif_layout="mail.mail_notification_light").create(values)
compose_message_wizard.send_mail() # Sends a mail and creates a survey.user_input
if employee.user_id:
user_input = survey.user_input_ids.filtered(
lambda user_input: user_input.partner_id == employee.user_id.partner_id and user_input.appraisal_id == appraisal and user_input.state != 'done'
)
if user_input:
form_url = survey.print_url + '/' + user_input[0].token
else:
form_url = survey.print_url
appraisal.activity_schedule(
'hr_appraisal.mail_act_appraisal_form', appraisal.date_close,
note=_('Fill form <a href="%s">%s</a> for <a href="#" data-oe-model="%s" data-oe-id="%s">%s\'s</a> appraisal') % (
form_url, survey.display_name,
appraisal.employee_id._name, appraisal.employee_id.id, appraisal.employee_id.display_name),
user_id=employee.user_id.id)
appraisal.message_post(body=_("Appraisal form(s) have been sent"))
return True
And here is the error i am getting
File "/opt/odoo12/odoo-custom-addons/dos_appraisal_send/models/send_appr.py", line 33, in throw_appraisal render_template = appraisal.result_template_id.with_context(email=email, survey=survey, employee=employee).generate_email([]) File "/opt/odoo12/odoo/addons/mail/models/mail_template.py", line 422, in generate_email self.ensure_one() File "/opt/odoo12/odoo/odoo/models.py", line 4764, in ensure_one raise ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: mail.template()