Update code style

This commit is contained in:
Alexandre Pocquet 2015-08-21 11:05:59 +02:00
parent f1fe8ca740
commit 903ad4eb04
2 changed files with 17 additions and 15 deletions

View file

@ -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 = """
<noscript><p><img src="http://%(url)s/piwik.php?idsite=%(siteid)s" style="border:0;" alt="" /></p></noscript>
""" # 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,

View file

@ -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))