From f8922d6b168b1563cfbaa437f1039e4c7e4fedd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 12 Sep 2013 12:09:09 +0200 Subject: [PATCH] [IMP] [ADD] mass_mailing: added a wizard to create new segments. It can be called directly from within the campaign form view, using a button. It allows to easily create new segments and launch the composer. bzr revid: tde@openerp.com-20130912100909-ofalececxn64a389 --- addons/mail/wizard/mail_compose_message.py | 2 +- addons/mass_mailing/__openerp__.py | 3 +- addons/mass_mailing/mass_mailing.py | 18 +++ addons/mass_mailing/mass_mailing_view.xml | 10 ++ addons/mass_mailing/wizard/__init__.py | 1 + .../wizard/mail_compose_message.py | 10 +- .../wizard/mail_compose_message_view.xml | 2 +- .../mail_mass_mailing_create_segment.py | 122 ++++++++++++++++++ .../mail_mass_mailing_create_segment.xml | 71 ++++++++++ 9 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 addons/mass_mailing/wizard/mail_mass_mailing_create_segment.py create mode 100644 addons/mass_mailing/wizard/mail_mass_mailing_create_segment.xml diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 1b3f6010893..f129e5bf0f9 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -75,7 +75,7 @@ class mail_compose_message(osv.TransientModel): if 'active_domain' in context: # not context.get() because we want to keep global [] domains result['use_active_domain'] = True result['active_domain'] = '%s' % context.get('active_domain') - else: + elif not result.get('active_domain'): result['active_domain'] = '' # get default values according to the composition mode if composition_mode == 'reply': diff --git a/addons/mass_mailing/__openerp__.py b/addons/mass_mailing/__openerp__.py index 147b9bea89c..7f2c26e4a28 100644 --- a/addons/mass_mailing/__openerp__.py +++ b/addons/mass_mailing/__openerp__.py @@ -2,7 +2,7 @@ ############################################################################## # # OpenERP, Open Source Management Solution -# Copyright (C) 2013-today OpenERP SA () +# Copyright (C) 2013-Today OpenERP SA () # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -37,6 +37,7 @@ 'mass_mailing_demo.xml', 'mail_mail_view.xml', 'wizard/mail_compose_message_view.xml', + 'wizard/mail_mass_mailing_create_segment.xml', 'security/ir.model.access.csv', ], 'js': [], diff --git a/addons/mass_mailing/mass_mailing.py b/addons/mass_mailing/mass_mailing.py index 31122bf514c..b255f3163c9 100644 --- a/addons/mass_mailing/mass_mailing.py +++ b/addons/mass_mailing/mass_mailing.py @@ -23,6 +23,7 @@ from datetime import datetime from dateutil import relativedelta from openerp import tools +from openerp.tools.translate import _ from openerp.osv import osv, fields @@ -104,6 +105,23 @@ class MassMailingCampaign(osv.Model): ), } + def launch_segment_create_wizard(self, cr, uid, ids, context=None): + ctx = dict(context) + ctx.update({ + 'default_mass_mailing_campaign_id': ids[0], + }) + return { + 'name': _('Create a Segment for the Campaign'), + 'type': 'ir.actions.act_window', + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'mail.mass_mailing.segment.create', + 'views': [(False, 'form')], + 'view_id': False, + 'target': 'new', + 'context': ctx, + } + class MassMailingSegment(osv.Model): """ MassMailingSegment models a segment for a mass mailign campaign. A segment diff --git a/addons/mass_mailing/mass_mailing_view.xml b/addons/mass_mailing/mass_mailing_view.xml index 9dddd82ad5e..cc2ffc9cb25 100644 --- a/addons/mass_mailing/mass_mailing_view.xml +++ b/addons/mass_mailing/mass_mailing_view.xml @@ -19,6 +19,10 @@ mail.mass_mailing.campaign
+
+
@@ -28,6 +32,8 @@ +
@@ -114,6 +120,10 @@ + + + + diff --git a/addons/mass_mailing/wizard/__init__.py b/addons/mass_mailing/wizard/__init__.py index 155849362cd..669d12289c2 100644 --- a/addons/mass_mailing/wizard/__init__.py +++ b/addons/mass_mailing/wizard/__init__.py @@ -20,3 +20,4 @@ ############################################################################## import mail_compose_message +import mail_mass_mailing_create_segment diff --git a/addons/mass_mailing/wizard/mail_compose_message.py b/addons/mass_mailing/wizard/mail_compose_message.py index 2b36389cb7b..4fe858734fe 100644 --- a/addons/mass_mailing/wizard/mail_compose_message.py +++ b/addons/mass_mailing/wizard/mail_compose_message.py @@ -41,17 +41,21 @@ class MailComposeMessage(osv.TransientModel): } _defaults = { - 'use_mass_mailing_campaign': True, + 'use_mass_mailing_campaign': False, } - def onchange_mass_mail_campaign_id(self, cr, uid, ids, mass_mail_campaign_id, context=None): + def onchange_mass_mail_campaign_id(self, cr, uid, ids, mass_mailing_campaign_id, mass_mail_segment_id, context=None): + if mass_mail_segment_id: + segment = self.pool['mail.mass_mailing.segment'].browse(cr, uid, mass_mail_segment_id, context=context) + if segment.mass_mailing_campaign_id.id == mass_mailing_campaign_id: + return {} return {'value': {'mass_mailing_segment_id': False}} def render_message_batch(self, cr, uid, wizard, res_ids, context=None): """ Override method that generated the mail content by adding the mass mailing campaign, when doing pure email mass mailing. """ res = super(MailComposeMessage, self).render_message_batch(cr, uid, wizard, res_ids, context=context) - if wizard.composition_mode == 'mass_mail' and wizard.mass_mailing_segment_id: # TODO: which kind of mass mailing ? + if wizard.composition_mode == 'mass_mail' and wizard.use_mass_mailing_campaign and wizard.mass_mailing_segment_id: # TODO: which kind of mass mailing ? for res_id in res_ids: res[res_id]['mass_mailing_segment_id'] = wizard.mass_mailing_segment_id.id return res diff --git a/addons/mass_mailing/wizard/mail_compose_message_view.xml b/addons/mass_mailing/wizard/mail_compose_message_view.xml index fda29f33399..9dcf57682f6 100644 --- a/addons/mass_mailing/wizard/mail_compose_message_view.xml +++ b/addons/mass_mailing/wizard/mail_compose_message_view.xml @@ -15,7 +15,7 @@
) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see +# +############################################################################## + +from openerp.osv import osv, fields + +from openerp.tools.translate import _ + + +class MailMassMailingSegmentCreate(osv.TransientModel): + """Wizard to help creating mass mailing segments for a campaign. """ + + _name = 'mail.mass_mailing.segment.create' + _description = 'Mass mailing segment creation' + + _columns = { + 'mass_mailing_campaign_id': fields.many2one( + 'mail.mass_mailing.campaign', 'Mass mailing campaign', + required=True, + ), + 'model_id': fields.many2one( + 'ir.model', 'Model', + required=True, + ), + 'model_model': fields.related( + 'model_id', 'name', + type='char', string='Model Name' + ), + 'filter_id': fields.many2one( + 'ir.filters', 'Filter', + domain="[('model_id', '=', model_model)]", + ), + 'domain': fields.related( + 'filter_id', 'domain', + type='char', string='Domain', + ), + 'template_id': fields.many2one( + 'email.template', 'Template', required=True, + domain="[('model_id', '=', model_id)]", + ), + 'segment_name': fields.char( + 'Segment name', required=True, + ), + 'mass_mailing_segment_id': fields.many2one( + 'mail.mass_mailing.segment', 'Mass Mailing Segment', + ), + } + + _defaults = { + } + + def on_change_model_id(self, cr, uid, ids, model_id, context=None): + if model_id: + model_model = self.pool['ir.model'].browse(cr, uid, model_id, context=context).model + else: + model_model = False + return {'value': {'model_model': model_model}} + + def on_change_filter_id(self, cr, uid, ids, filter_id, context=None): + if filter_id: + domain = self.pool['ir.filters'].browse(cr, uid, filter_id, context=context).domain + else: + domain = False + return {'value': {'domain': domain}} + + def create_segment(self, cr, uid, ids, context=None): + """ Create a segment based on wizard data, and update the wizard """ + for wizard in self.browse(cr, uid, ids, context=context): + segment_values = { + 'name': wizard.segment_name, + 'mass_mailing_campaign_id': wizard.mass_mailing_campaign_id.id, + 'domain': wizard.domain, + 'template_id': wizard.template_id.id, + } + segment_id = self.pool['mail.mass_mailing.segment'].create(cr, uid, segment_values, context=context) + self.write(cr, uid, [wizard.id], {'mass_mailing_segment_id': segment_id}, context=context) + return True + + def launch_composer(self, cr, uid, ids, context=None): + """ Main wizard action: create a new segment and launch the mail.compose.message + email composer with wizard data. """ + self.create_segment(cr, uid, ids, context=context) + + wizard = self.browse(cr, uid, ids[0], context=context) + ctx = dict(context) + ctx.update({ + 'default_composition_mode': 'mass_mail', + 'default_template_id': wizard.template_id.id, + 'default_use_mass_mailing_campaign': True, + 'default_use_active_domain': True, + 'default_active_domain': wizard.domain, + 'default_mass_mailing_campaign_id': wizard.mass_mailing_campaign_id.id, + 'default_mass_mailing_segment_id': wizard.mass_mailing_segment_id.id, + }) + return { + 'name': _('Compose Email for Mass Mailing'), + 'type': 'ir.actions.act_window', + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'mail.compose.message', + 'views': [(False, 'form')], + 'view_id': False, + 'target': 'new', + 'context': ctx, + } diff --git a/addons/mass_mailing/wizard/mail_mass_mailing_create_segment.xml b/addons/mass_mailing/wizard/mail_mass_mailing_create_segment.xml new file mode 100644 index 00000000000..5d211bb9eb4 --- /dev/null +++ b/addons/mass_mailing/wizard/mail_mass_mailing_create_segment.xml @@ -0,0 +1,71 @@ + + + + + + + mail.mass_mailing.segment.create.form + mail.mass_mailing.segment.create + +
+ + + +

+ Please choose a mass mailing campaign that will hold the new segment. +

+ + +

+ Please choose a model on which you will run the mass mailing. +

+ + +

+ Please choose a filter that will be applied on the model + to find the records on which you will run the mass mailing. +

+ + +

+ Please choose the template to use to render the emails + to send. +

+ + +

+ Please choose the name of the campaign segment. +

+ + +