[IMP] survey: send_mail and url ok

bzr revid: chm@openerp.com-20130220094751-yh3vziwnftnlxdk7
This commit is contained in:
Christophe Matthieu
2013-02-20 10:47:51 +01:00
parent 4b81cf6e8d
commit 0d99d63e83
3 changed files with 46 additions and 38 deletions
+2 -2
View File
@@ -153,7 +153,7 @@ class survey(osv.osv):
'view_mode': 'form',
'res_model': 'survey.question.wiz',
'type': 'ir.actions.act_window',
'target': 'new',
'target': 'inline',
'name': self.browse(cr, uid, id, context=context).title,
'context': context
}
@@ -200,7 +200,7 @@ class survey(osv.osv):
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_invoice_as_sent': True,
'survey_state': survey_browse.state
})
return {
'type': 'ir.actions.act_window',
@@ -20,12 +20,15 @@
##############################################################################
import re
from urllib import urlencode
from urlparse import urljoin
from openerp.osv import osv
from openerp.osv import fields
from datetime import datetime
from openerp.tools.translate import _
import uuid
class survey_mail_compose_message(osv.TransientModel):
_name = 'survey.mail.compose.message'
_inherit = 'mail.compose.message'
@@ -40,7 +43,7 @@ class survey_mail_compose_message(osv.TransientModel):
'survey_mail_compose_message_ir_attachments_rel',
'wizard_id', 'attachment_id', 'Attachments'),
'multi_email': fields.text(string='List of emails', help="This list of emails of recipients will not converted in partner. Emails separated by commas, semicolons or newline."),
'date_deadline': fields.date(string="Deadline date", help=""),
'date_deadline': fields.date(string="Deadline date", help="Deadline to complete the survey. If the field is empty, the invitation is still valid."),
}
#------------------------------------------------------
# Wizard validation and send
@@ -55,15 +58,14 @@ class survey_mail_compose_message(osv.TransientModel):
emails_split = re.compile(r"[;,\n\r]+")
survey_response_obj = self.pool.get('survey.response')
partner_obj = self.pool.get('res.partner')
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
mail_mail_obj = self.pool.get('mail.mail')
mail_message_obj = self.pool.get('mail.message')
base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
def send_mail_survey(wizard, token, partner_id, email):
def create_token_and_send_mail_survey(wizard, token, partner_id, email):
""" Create one mail by recipients and replace __URL__ by link with identification token
"""
if not partner_id:
partner_id = partner_obj.search(cr, uid, [('email', '=', email)], context=context)
partner_id = partner_id and partner_id[0] or None
# create response with token
survey_response_obj.create(cr, uid, {
'date_deadline': wizard.date_deadline,
'survey_id': wizard.res_id,
@@ -75,37 +77,43 @@ class survey_mail_compose_message(osv.TransientModel):
'email': email,
})
post_values = {
'subject': wizard.subject,
'body_html': wizard.body.replace("__URL__", "#active_id=%s&params=%s" % (wizard.res_id, token)),
'parent_id': None,
'partner_ids': partner_id and [partner_id] or None,
'attachments': attachments,
'email_from': wizard.email_from or user.partner_id.email or None,
'email_to': email,
'notification': False,
# create url
query = {
'db': cr.dbname
}
print ""
print post_values
print ""
# # post the message
# active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype='mt_comment', context=context, **post_values)
fragment = {
'active_id': wizard.res_id,
'params': token,
'action': 'survey.action_answer_survey',
}
url = urljoin(base_url, "?%s#%s" % (urlencode(query), urlencode(fragment)))
# post the message
values = {
'model': 'survey',
'res_id': wizard.res_id or False,
'subject': wizard.subject,
'body': wizard.body.replace("__URL__", url),
'parent_id': None,
'partner_ids': partner_id and [(4, partner_id)] or None,
'notified_partner_ids': partner_id and [(4, partner_id)] or None,
'attachment_ids': wizard.attachment_ids or None,
'email_from': wizard.email_from or None,
'email_to': email,
}
mail_obj = partner_id and mail_message_obj or mail_mail_obj
mail_id = mail_obj.create(cr, uid, values, context=context)
if mail_obj == mail_mail_obj:
mail_obj.send(cr, uid, [mail_id], context=context)
for wizard in self.browse(cr, uid, ids, context=context):
if wizard.model == 'survey':
# default values, according to the wizard options
attachments = [(attach.datas_fname or attach.name, base64.b64decode(attach.datas)) for attach in wizard.attachment_ids]
# check if __URL__ is in the text
if wizard.body.find("__URL__") < 0:
raise osv.except_osv(_('Warning!'),_("The content of the text don't contain '__URL__'. \
raise osv.except_osv(_('Warning!'), _("The content of the text don't contain '__URL__'. \
__URL__ is automaticaly converted into the special url of the survey."))
emails = list(set(emails_split.split( wizard.multi_email or "")) - set([partner.email for partner in wizard.partner_ids]))
emails = list(set(emails_split.split(wizard.multi_email or "")) - set([partner.email for partner in wizard.partner_ids]))
# quick check of email list
emails_checked = []
@@ -113,17 +121,17 @@ class survey_mail_compose_message(osv.TransientModel):
email = email.strip()
if email:
if not re.search(r"^[^@]+@[^@]+$", email):
raise osv.except_osv(_('Warning!'),_("An email address is incorrect: '%s'" % email))
raise osv.except_osv(_('Warning!'), _("An email address is incorrect: '%s'" % email))
else:
emails_checked.append(email)
if not len(emails_checked) and not len(wizard.partner_ids):
raise osv.except_osv(_('Warning!'),_("Please enter at least one recipient."))
raise osv.except_osv(_('Warning!'), _("Please enter at least one recipient."))
for email in emails_checked:
send_mail_survey(wizard, uuid.uuid4(), None, email)
partner_id = partner_obj.search(cr, uid, [('email', '=', email)], context=context)
partner_id = partner_id and partner_id[0] or None
create_token_and_send_mail_survey(wizard, uuid.uuid4(), partner_id, email)
for partner in wizard.partner_ids:
send_mail_survey(wizard, uuid.uuid4(), partner.id, partner.email)
create_token_and_send_mail_survey(wizard, uuid.uuid4(), partner.id, partner.email)
return None
return {'type': 'ir.actions.act_window_close'}
@@ -35,7 +35,7 @@
<field name="model" invisible="1"/>
<field name="res_id" invisible="1"/>
<field name="parent_id" invisible="1"/>
<field name="date_deadline"/>
<field name="date_deadline" invisible="context.get('survey_state') != 'restricted'"/>
<field string="List of partners" name="partner_ids" widget="many2many_tags_email" placeholder="Add contacts to notify..." context="{'force_email':True, 'show_email':True}"/>
<field name="multi_email"/>
<field name="subject" placeholder="Subject..."/>