# HG changeset patch # User Florent Aide, <florent.aide@gmail.com> # Date 1362656720 -3600 # Thu Mar 07 12:45:20 2013 +0100 # Node ID 09f99957bf8b47495be9a3419aed420a29d25799 # Parent 904d68f776fa1843177f702273d096e852206fa9 Port to openerp V7 diff --git a/marketing_campaign_tracker.py b/marketing_campaign_tracker.py --- a/marketing_campaign_tracker.py +++ b/marketing_campaign_tracker.py @@ -21,36 +21,33 @@ ############################################################################## from md5 import md5 -from osv import osv, fields +from openerp.osv import osv, fields from openerp.tools import config from replace import insert_tracker_in_html, insert_tracker_in_text -class hooked_email_template(osv.osv): +class hooked_email_template(osv.Model): """overrides the base email template to hook the URL tracker """ _inherit = 'email.template' - def send_mail(self, cr, uid, template_id, res_id, context=None): - """ Generates a new mail message for the given template and record, - and schedule it for delivery through the ``mail`` module's scheduler. - - :param int template_id: id of the template to render - :param int res_id: id of the record to render the template with - (model is taken from the template) + def generate_email(self, cr, uid, template_id, res_id, context=None): + """override generate_email() from our parent to substitute URLs """ - mail_message = self.pool.get('mail.message') - ir_attachment = self.pool.get('ir.attachment') - template = self.browse(cr, uid, template_id, context) - values = self.generate_email(cr, uid, template_id, res_id, - context=context) + values = super(hooked_email_template, self).generate_email( + cr, uid, template_id, res_id, context=context) - ## Change from original send_mail + if context is None: + context = dict() + if context.get('use_tracker', False): # here we need to use tracker so let's do it tracker_base = context.get('tracker_base') - #### TODO hostbame_bu configuration with configure option menu. + + # here we add a hash that contains informations about our + # business unit... this is necessary to be able to have + # a tracker serve multiple business units hostname_bu = config.get('xmlrpc_interface') if not hostname_bu: hostname_bu = "localhost" @@ -59,68 +56,54 @@ id_bu = "%s:%s/%s" % (hostname_bu, port_bu, database_bu) tracker_base += '/' + md5(id_bu).hexdigest() - if values['body_text']: - values['body_text'], tracks = insert_tracker_in_text( - values['body_text'], - tracker_base, - context['activity_id']) - if values['body_html']: - values['body_html'], tracks = insert_tracker_in_html( - values['body_html'], - tracker_base, - context['activity_id']) + trackitem_osv = self.pool.get( + 'marketing_campaign_tracker.trackitem') - trackitem = self.pool.get('marketing_campaign_tracker.trackitem') + new_body_text, tracks = insert_tracker_in_text( + values.get('body'), tracker_base, context['activity_id']) + for trackvalues in tracks: # create a trackitem for each URL that is replaced - trackitem.create(cr, uid, trackvalues, context=None) - - ## End Change from original send_mail + trackitem_osv.create(cr, uid, trackvalues, context=None) - #values = self.add_tracker(cr, uid, template_id, res_id, - # context=context) - attachments = values.pop('attachments') or {} - message_id = mail_message.create(cr, uid, values, context=context) - # link attachments + values["body"] = new_body_text + + new_body_html, tracks = insert_tracker_in_html( + values.get('body_html'), tracker_base, context['activity_id']) - attachment_ids = [] - for fname, fcontent in attachments.iteritems(): - attachment_data = { - 'name': fname, - 'datas_fname': fname, - 'datas': fcontent, - 'res_model': mail_message._name, - 'res_id': message_id, - } - if 'default_type' in context: - del context['default_type'] - attachment_ids.append(ir_attachment.create(cr, - uid, - attachment_data, - context)) + for trackvalues in tracks: + # create a trackitem for each URL that is replaced + trackitem_osv.create(cr, uid, trackvalues, context=None) -hooked_email_template() + values["body_html"] = new_body_html + + return values -class marketing_campaign_activity(osv.osv): - # we want to change the marketing campaign activity... +class marketing_campaign_activity(osv.Model): + # we want to change the marketing campaign activity to inject our new + # options _inherit = 'marketing.campaign.activity' # here we add a new field to the marketing campaign activity # the use_tracker boolean will activate the tracking for URLs if is # set to True. The default value will be False in order to avoid any # problem with existing activities previous to this module installation. - _columns = { - 'use_tracker': fields.boolean('Use Tracker', - help='This option activates the usage of ' + - 'the external "Bounced" click tracker'), - 'tracker_base': fields.char("Tracker Base URL", size=200, - help="The base URL on which the tracker listens"), - } + _columns = dict( + use_tracker=fields.boolean( + 'Use Tracker', + help='Activates the usage of an external "Bounced" click tracker'), + tracker_base=fields.char( + "Tracker Base URL", size=200, + help="The base URL on which the tracker listens"), + ) # make sure our new field has a sensible default. # see comment above the _columns definition for more - _defaults = dict(use_tracker=False, tracker_base=None) + _defaults = dict( + use_tracker=False, + tracker_base=None, + ) # make sure an activity which uses a tracker always has a tracker # base defined... @@ -137,28 +120,31 @@ return True _constraints = [ - (_check_baseurl, - 'If you activate tracking you must give a tracker base', - ['tracker_base']) + ( + _check_baseurl, + 'If you use tracking you must give a tracker base URL', + ['tracker_base'] + ) ] # override the _process_wi_email to replace all the URLs in the template # body by our tracker URL and store the corresponding keys in database def _process_wi_email(self, cr, uid, activity, workitem, context=None): if context is None: - context = {} + context = dict() context['use_tracker'] = activity.use_tracker if activity.use_tracker: # remove any trailing slash that could be present in the base_url + # and inject the necessary keys into our context context['tracker_base'] = activity.tracker_base.rstrip('/') context['activity_id'] = activity.id - return self.pool.get('email.template').send_mail(cr, uid, - activity.email_template_id.id, - workitem.res_id, context=context) + return super(marketing_campaign_activity, self)._process_wi_email( + cr, uid, activity, workitem, context=context + ) -marketing_campaign_activity() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: + diff --git a/trackitem.py b/trackitem.py --- a/trackitem.py +++ b/trackitem.py @@ -20,11 +20,11 @@ # ############################################################################## -from osv import osv, fields +from openerp.osv import osv, fields import time -class trackitem(osv.osv): +class trackitem(osv.Model): """a trackitem is a simple link between a real URL and a UUID URL that was sent in an email. When the person clicks on the UUID URL the tracker server will hit the database with the UUID to try and find the real URL @@ -38,8 +38,7 @@ _name = 'marketing_campaign_tracker.trackitem' _columns = { - 'trackitem_uuid': fields.char("uuid", size=36, - required=True, select=1), + 'trackitem_uuid': fields.char("uuid", size=36, required=True, select=1), 'real_url': fields.char("Real URL", size=500), # we only need the activity because an activity obj has a campaign_id # attribute referencing the campaign it belongs to. @@ -49,10 +48,8 @@ ), } -trackitem() - -class trackvisit(osv.osv): +class trackvisit(osv.Model): """a trackvisit is a small timestamp associated to a trackitem Basically each time a client clicks on a tracked URL and gets redirected by the tracker a trackvisit will be recorded for the action. This opens-up @@ -75,5 +72,3 @@ # format the datetime for PG backend storage in a timestamp visit_time=lambda *a: time.strftime("%Y-%m-%d %H:%M:%S.000000"), ) - -trackvisit()