diff --git a/marketing_campaign_tracker.py b/marketing_campaign_tracker.py index cc5e109a6bcf89c2a8592256384aa79f76de636b_bWFya2V0aW5nX2NhbXBhaWduX3RyYWNrZXIucHk=..9e6ae17c6f778b0514f86c6e1f78496e1fd1a19a_bWFya2V0aW5nX2NhbXBhaWduX3RyYWNrZXIucHk= 100644 --- a/marketing_campaign_tracker.py +++ b/marketing_campaign_tracker.py @@ -21,32 +21,7 @@ ############################################################################## from osv import osv, fields -import re -import uuid - -simple_text_url_re = re.compile(r'http://[-a-zA-Z0-9_/.]+') - -def insert_tracker_in_text(cursor, user, text, tracker_base, pool, activity_id): - - def repl(match_obj): - # here we must keep a reference on the uuid in our db for the tracker - real_url = match_obj.group(0) - uid = str(uuid.uuid4()) - values = dict( - trackitem_uuid = uid, - real_url = real_url, - campaign_activity_id = activity_id, - ) - trackitem = pool.get('marketing_campaign_tracker.trackitem') - # create a trackitem for each URL that is replaced - trackitem.create(cursor, user, values, context=None) - return "%s/%s" % (tracker_base, uid) - - return simple_text_url_re.sub(repl, text) - - -def insert_tracker_in_html(cr, uid, html, tracker_base, pool, activity_id): - return html +from replace import insert_tracker_in_html, insert_tracker_in_text class hooked_email_template(osv.osv): """overrides the base email template to hook the URL tracker @@ -74,8 +49,13 @@ # here we need to use tracker so let's do it tracker_base = context.get('tracker_base') text = template.def_body_text - template.def_body_text = insert_tracker_in_text( - cursor, user, text, tracker_base, - self.pool, context['activity_id']) + template.def_body_text, tracks = insert_tracker_in_text( + text, tracker_base, context['activity_id']) + + trackitem = self.pool.get('marketing_campaign_tracker.trackitem') + + for trackvalues in tracks: + # create a trackitem for each URL that is replaced + trackitem.create(cursor, user, trackvalues, context=None) html = template.def_body_html @@ -80,8 +60,11 @@ html = template.def_body_html - template.def_body_html = insert_tracker_in_html( - cursor, user, html, tracker_base, - self.pool, context['activity_id']) + template.def_body_html, tracks = insert_tracker_in_html( + html, tracker_base, context['activity_id']) + + for trackvalues in tracks: + # create a trackitem for each URL that is replaced + trackitem.create(cursor, user, trackvalues, context=None) # END OF CHANGES FROM ORIGINAL METHOD @@ -144,7 +127,7 @@ return False return True - + _constraints = [ (_check_baseurl, 'If you activate tracking you must give a tracker base', @@ -161,7 +144,7 @@ if activity.use_tracker: # remove any trailing slash that could be present in the base_url - context['tracker_base'] = activity.tracker_base.rstrip('/') + context['tracker_base'] = activity.tracker_base.rstrip('/') context['activity_id'] = activity.id return self.pool.get('email.template').generate_mail(cr, uid, diff --git a/replace.py b/replace.py new file mode 100644 index 0000000000000000000000000000000000000000..9e6ae17c6f778b0514f86c6e1f78496e1fd1a19a_cmVwbGFjZS5weQ== --- /dev/null +++ b/replace.py @@ -0,0 +1,90 @@ +__author__ = 'faide' + +import uuid +import re + +simple_text_url_re = re.compile(r'(http://[-a-zA-Z0-9_/.?&=]+)') +html_href_re = re.compile(r'href="([-a-zA-Z0-9_/.?&=:]+)"') + +def insert_tracker_in_text(text, tracker_base, activity_id): + + tracks = [] + + def repl(match_obj): + # here we must keep a reference on the uuid in our db for the tracker + real_url = match_obj.group(1) + uid = str(uuid.uuid4()) + values = dict( + trackitem_uuid = uid, + real_url = real_url, + campaign_activity_id = activity_id, + ) + tracks.append(values) + return "%s/%s" % (tracker_base, uid) + + return simple_text_url_re.sub(repl, text), tracks + +def insert_tracker_in_html(html, tracker_base, activity_id): + + tracks = [] + + def repl(match_obj): + # here we must keep a reference on the uuid in our db for the tracker + real_url = match_obj.group(1) + uid = str(uuid.uuid4()) + values = dict( + trackitem_uuid = uid, + real_url = real_url, + campaign_activity_id = activity_id, + ) + tracks.append(values) + return 'href="%s/%s"' % (tracker_base, uid) + + return html_href_re.sub(repl, html), tracks + +if __name__ == '__main__': + url = "http://some.org/a_3/super-script.php?uid=3&someval=4" + text = url + base_html = '<a href="%s"> Some Super Promo </a>' + html = base_html % url + + print "*"*35 + print "Testing text replacement" + new_text, values = insert_tracker_in_text(text, 'BASE', '1') + + assert len(values) == 1, "Values should contain only one record" + + value = values[0] + track_uuid = value.get('trackitem_uuid', None) + + assert track_uuid, "Track uuid should not be None" + + real_url = value.get("real_url", None) + print "Real URL was: %s" % real_url + + assert real_url == url, "Real URL should have been %s, not %s" % ( + url, real_url) + assert new_text == '%s/%s' % ('BASE', track_uuid) + + + print "*"*35 + print "Testing HTML replacement" + new_html, values = insert_tracker_in_html(html, 'BASE', '1') + + print new_html + assert len(values) == 1, "Values should have one record, not %s" % len(values) + + value = values[0] + track_uuid = value.get('trackitem_uuid', None) + + assert track_uuid, "Track uuid should not be None" + + real_url = value.get("real_url", None) + print "Real URL was: %s" % real_url + + assert real_url == url, "Real URL should have been %s, not %s" % ( + url, real_url) + expected_html = base_html % ('BASE/' + track_uuid) + assert new_html == expected_html + print "*"*35 +