From 903ad4eb04e9d04808be910213c936f8094b07cc Mon Sep 17 00:00:00 2001 From: Alexandre Pocquet Date: Fri, 21 Aug 2015 11:05:59 +0200 Subject: [PATCH] Update code style --- analytical/templatetags/piwik.py | 22 ++++++++++++---------- analytical/tests/test_tag_piwik.py | 10 +++++----- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/analytical/templatetags/piwik.py b/analytical/templatetags/piwik.py index 7d2351e..b543968 100644 --- a/analytical/templatetags/piwik.py +++ b/analytical/templatetags/piwik.py @@ -3,14 +3,15 @@ Piwik template tags and filters. """ from __future__ import absolute_import -from collections import namedtuple +from collections import namedtuple +from itertools import chain import re from django.template import Library, Node, TemplateSyntaxError -from django.conf import settings -from analytical.utils import is_internal_ip, disable_html, get_required_setting, get_identity +from analytical.utils import (is_internal_ip, disable_html, + get_required_setting, get_identity) # domain name (characters separated by a dot), optional URI path, no slash @@ -36,7 +37,7 @@ TRACKING_CODE = """ """ # noqa -VARIABLE_CODE = '_paq.push(["setCustomVariable", %(index)s, "%(name)s", "%(value)s", "%(scope)s"]);' +VARIABLE_CODE = '_paq.push(["setCustomVariable", %(index)s, "%(name)s", "%(value)s", "%(scope)s"]);' # noqa IDENTITY_CODE = '_paq.push(["setUserId", "%(userid)s"]);' DEFAULT_SCOPE = 'page' @@ -61,7 +62,6 @@ def piwik(parser, token): ``(index, name, value[, scope])`` where scope may be ``'page'`` (default) or ``'visit'``. Index should be an integer and the other parameters should be strings. - See the [Piwik documentation](http://developer.piwik.org/guides/tracking-javascript-guide#custom-variables) """ bits = token.split_contents() if len(bits) > 1: @@ -83,15 +83,17 @@ class PiwikNode(Node): def render(self, context): custom_variables = context.get('piwik_vars', ()) - complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) for var in custom_variables) + complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) + for var in custom_variables) - variables_code = [VARIABLE_CODE % PiwikVar(*var)._asdict() for var in complete_variables] + variables_code = (VARIABLE_CODE % PiwikVar(*var)._asdict() + for var in complete_variables) userid = get_identity(context, 'piwik') if userid is not None: - variables_code.append( - IDENTITY_CODE % {'userid': userid} - ) + variables_code = chain(variables_code, ( + IDENTITY_CODE % {'userid': userid}, + )) html = TRACKING_CODE % { 'url': self.domain_path, diff --git a/analytical/tests/test_tag_piwik.py b/analytical/tests/test_tag_piwik.py index dd722ed..8bfff04 100644 --- a/analytical/tests/test_tag_piwik.py +++ b/analytical/tests/test_tag_piwik.py @@ -2,10 +2,10 @@ Tests for the Piwik template tags and filters. """ +from django.contrib.auth.models import User from django.http import HttpRequest from django.template import Context from django.test.utils import override_settings -from django.contrib.auth.models import User from analytical.templatetags.piwik import PiwikNode from analytical.tests.utils import TagTestCase @@ -86,7 +86,7 @@ class PiwikTagTestCase(TagTestCase): 'user': User(username='BDFL', first_name='Guido', last_name='van Rossum') }) r = PiwikNode().render(context) - msg = 'Incorrect Piwik user tracking rendering. Expected:\n%s\nIn:\n%s' + msg = 'Incorrect Piwik user tracking rendering.\nNot found:\n%s\nIn:\n%s' var_code = '_paq.push(["setUserId", "BDFL"]);' self.assertIn(var_code, r, msg % (var_code, r)) @@ -95,7 +95,7 @@ class PiwikTagTestCase(TagTestCase): 'piwik_identity': 'BDFL' }) r = PiwikNode().render(context) - msg = 'Incorrect Piwik user tracking rendering. Expected:\n%s\nIn:\n%s' + msg = 'Incorrect Piwik user tracking rendering.\nNot found:\n%s\nIn:\n%s' var_code = '_paq.push(["setUserId", "BDFL"]);' self.assertIn(var_code, r, msg % (var_code, r)) @@ -104,7 +104,7 @@ class PiwikTagTestCase(TagTestCase): 'analytical_identity': 'BDFL' }) r = PiwikNode().render(context) - msg = 'Incorrect Piwik user tracking rendering. Expected:\n%s\nIn:\n%s' + msg = 'Incorrect Piwik user tracking rendering.\nNot found:\n%s\nIn:\n%s' var_code = '_paq.push(["setUserId", "BDFL"]);' self.assertIn(var_code, r, msg % (var_code, r)) @@ -115,6 +115,6 @@ class PiwikTagTestCase(TagTestCase): 'piwik_identity': None }) r = PiwikNode().render(context) - msg = 'Incorrect Piwik user tracking rendering. Expected:\n%s\nIn:\n%s' + msg = 'Incorrect Piwik user tracking rendering.\nFound:\n%s\nIn:\n%s' var_code = '_paq.push(["setUserId", "BDFL"]);' self.assertNotIn(var_code, r, msg % (var_code, r))