# HG changeset patch
# User Xavier Manach <xavier.manach@xcg-consulting.fr>
# Date 1317908804 -7200
#      Thu Oct 06 15:46:44 2011 +0200
# Node ID 904d68f776fa1843177f702273d096e852206fa9
# Parent  b347499955436deae8bdc0379991cd3bbdb0c8fe
Conform to PEP8

diff --git a/__init__.py b/__init__.py
--- a/__init__.py
+++ b/__init__.py
@@ -24,4 +24,3 @@
 import marketing_campaign_tracker
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
-
diff --git a/__openerp__.py b/__openerp__.py
--- a/__openerp__.py
+++ b/__openerp__.py
@@ -22,15 +22,15 @@
 
 
 {
-    "name" : "Marketing Campaign Tracker",
-    "version" : "0.1",
-    "depends" : ["marketing",
+    "name": "Marketing Campaign Tracker",
+    "version": "0.1",
+    "depends": ["marketing",
                  "document",
                  "email_template",
                  "decimal_precision",
                  "marketing_campaign",
                 ],
-    "author" : "XCG SAS",
+    "author": "XCG SAS",
     "category": 'Sales',
     'complexity': "expert",
     "description": """
@@ -45,7 +45,7 @@
     ],
     'installable': True,
     'active': False,
-    #'certificate' : '',
+    #'certificate': '',
     'images': [],
 }
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
diff --git a/marketing_campaign_tracker.py b/marketing_campaign_tracker.py
--- a/marketing_campaign_tracker.py
+++ b/marketing_campaign_tracker.py
@@ -20,9 +20,9 @@
 #
 ##############################################################################
 
-import md5
+from md5 import md5
 from osv import osv, fields
-import openerp.tools as tools
+from openerp.tools import config
 from replace import insert_tracker_in_html, insert_tracker_in_text
 
 
@@ -32,36 +32,42 @@
     _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.                                                                                                                                 
+        """ 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)                                                                                                                                                         """
+            :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)
+        """
+
         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 = self.generate_email(cr, uid, template_id, res_id,
+                                     context=context)
 
         ## Change from original send_mail
         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.
-            hostname_bu=tools.config.get('xmlrpc_interface')
+            hostname_bu = config.get('xmlrpc_interface')
             if not hostname_bu:
-                hostname_bu="localhost"
-            port_bu=tools.config.get('xmlrpc_port')
-            database_bu=cr.dbname
-            id_bu= "%s:%s/%s" % (hostname_bu,port_bu,database_bu)
-            tracker_base+='/'+md5.md5(id_bu).hexdigest()
+                hostname_bu = "localhost"
+            port_bu = config.get('xmlrpc_port')
+            database_bu = cr.dbname
+            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, 
+                    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, 
+                    tracker_base,
                     context['activity_id'])
 
             trackitem = self.pool.get('marketing_campaign_tracker.trackitem')
@@ -71,7 +77,8 @@
 
         ## End Change from original send_mail
 
-        #values = self.add_tracker(cr, uid, template_id, res_id, context=context)
+        #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
@@ -85,9 +92,12 @@
                     'res_model': mail_message._name,
                 'res_id': message_id,
                 }
-            if context.has_key('default_type'):
+            if 'default_type' in context:
                 del context['default_type']
-            attachment_ids.append(ir_attachment.create(cr, uid, attachment_data, context))
+            attachment_ids.append(ir_attachment.create(cr,
+                                                       uid,
+                                                       attachment_data,
+                                                       context))
 
 hooked_email_template()
 
@@ -102,17 +112,15 @@
     # 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'),
+                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"),
         }
 
     # 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...
@@ -150,10 +158,7 @@
         return self.pool.get('email.template').send_mail(cr, uid,
                                             activity.email_template_id.id,
                                             workitem.res_id, context=context)
-        
 
 marketing_campaign_activity()
 
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
-
diff --git a/replace.py b/replace.py
--- a/replace.py
+++ b/replace.py
@@ -21,52 +21,46 @@
 ##############################################################################
 __author__ = 'faide'
 
-import uuid
+from uuid import uuid4
 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_/.?&=:]+)"')
 html_imgsrc_re = re.compile(r'img src="([-a-zA-Z0-9_/.?&=:]+)"')
 
+
 def insert_tracker_in_text(text, tracker_base, activity_id):
     tracks = []
 
     def repl(match_obj):
         real_url = match_obj.group(1)
-        uid = str(uuid.uuid4())
-        values = dict(
-            trackitem_uuid = uid,
-            real_url = real_url,
-            campaign_activity_id = activity_id,
-            )
+        uid = str(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):
         real_url = match_obj.group(1)
-        uid = str(uuid.uuid4())
-        values = dict(
-            trackitem_uuid = uid,
-            real_url = real_url,
-            campaign_activity_id = activity_id,
-            )
+        uid = str(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)
 
     def repl2(match_obj):
         real_url = match_obj.group(1)
-        uid = str(uuid.uuid4())
-        values = dict(
-            trackitem_uuid = uid,
-            real_url = real_url,
-            campaign_activity_id = activity_id,
-            )
+        uid = str(uuid4())
+        values = dict(trackitem_uuid=uid,
+                      real_url=real_url,
+                      campaign_activity_id=activity_id,)
         tracks.append(values)
         return 'img src="%s/%s"' % (tracker_base, uid)
 
@@ -78,11 +72,11 @@
     text = url
     base_html = '<a href="%s"> Some Super Promo </a>'
     html = base_html % url
-    img_url ='http://some.org/b_5/super-img.php?img=45&value=33'
+    img_url = 'http://some.org/b_5/super-img.php?img=45&value=33'
     base_img = '<img src="%s" />'
     imgsrc = base_img % img_url
 
-    print "*"*35
+    print "*" * 35
     print "Testing text replacement"
     new_text, values = insert_tracker_in_text(text, 'BASE', '1')
 
@@ -100,13 +94,13 @@
                 url, real_url)
     assert new_text == '%s/%s' % ('BASE', track_uuid)
 
-
-    print "*"*35
+    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)
+    assert len(values) == 1, \
+            "Values should have one record, not %s" % len(values)
 
     value = values[0]
     track_uuid = value.get('trackitem_uuid', None)
@@ -121,12 +115,13 @@
     expected_html = base_html % ('BASE/' + track_uuid)
     assert new_html == expected_html
 
-    print "*"*35
+    print "*" * 35
     print "Testing img src replacement"
     new_html, values = insert_tracker_in_html(imgsrc, 'BASE', '1')
 
     print new_html
-    assert len(values) == 1, "Values should have one record, not %s" % len(values)
+    assert len(values) == 1, \
+           "Values should have one record, not %s" % len(values)
 
     value = values[0]
     track_uuid = value.get('trackitem_uuid', None)
@@ -139,7 +134,6 @@
     assert real_url == img_url, "Real URL should have been %s, not %s" % (
         img_url, real_url)
     expected_html = base_img % ('BASE/' + track_uuid)
-    assert new_html == expected_html, "new html should have been %s, not %s" % (
-        expected_html, new_html
-    )
-
+    assert new_html == expected_html, \
+            "new html should have been %s, not %s" % (
+            expected_html, new_html)
diff --git a/trackitem.py b/trackitem.py
--- a/trackitem.py
+++ b/trackitem.py
@@ -23,6 +23,7 @@
 from osv import osv, fields
 import time
 
+
 class trackitem(osv.osv):
     """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
@@ -37,7 +38,8 @@
     _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,6 +51,7 @@
 
 trackitem()
 
+
 class trackvisit(osv.osv):
     """a trackvisit is a small timestamp associated to a trackitem
     Basically each time a client clicks on a tracked URL and gets redirected
@@ -70,7 +73,7 @@
 
     _defaults = dict(
         # format the datetime for PG backend storage in a timestamp
-        visit_time = lambda *a: time.strftime("%Y-%m-%d %H:%M:%S.000000"),
+        visit_time=lambda *a: time.strftime("%Y-%m-%d %H:%M:%S.000000"),
     )
 
 trackvisit()