etq_exam_share.py 2.19 KB
from odoo import models, fields, api
import logging
from odoo.http import request

# #
# import requests
# # from datetime import datetime
# # from openerp.tools import html_escape as escape, ustr, image_resize_and_sharpen, \
# #     image_save_for_web
# # import unicodedata
# # import re
#
_logger = logging.getLogger(__name__)


class EtqExamShare(models.Model):
    _name = "etq.exam.share"
    _description = "Exam Share"

    exam_id = fields.Many2one('etq.exam', string="Exam")
    share_type = fields.Selection([('existing_contacts', 'Existing Contacts'),
                                   ('new_contacts', 'New Contacts')],
                                  string="Share Option", required=True,
                                  default="existing_contacts")
    partner_ids = fields.Many2many('res.partner', string="Existing Contacts")
    email_list = fields.Text(string="Email List")
    # placeholder="Comma seperated emails")
    email_subject = fields.Char(string="Email Subject", required=True)
    email_content = fields.Html(string="Email Content", required=True,
                                sanitize=False)

    @api.onchange('exam_id')
    def _change_share(self):
        notification_template = self.env['ir.model.data'].get_object(
            'exam_test_quiz', 'exam_share_email')
        for rec in self:
            rec.email_subject = notification_template.subject

            temp_content = notification_template.body_html
            temp_content = temp_content.replace('__URL__',
                                                request.httprequest.host_url + "exam/" + rec.exam_id.slug)
            temp_content = temp_content.replace('__EXAM__', rec.exam_id.name)

            rec.email_content = temp_content

        # request.httprequest.host_url + "form/myinsert"

    # @api.one
    def share_exam(self):
        for rec in self:
            notification_template = self.env['ir.model.data']. \
                get_object('exam_test_quiz', 'exam_share_email')

            for cust in rec.partner_ids:
                notification_template.subject = rec.email_subject
                notification_template.body_html = rec.email_content
                notification_template.send_mail(cust.id, True)