Fix small code style issues based on pylint

Also update patch version number.
This commit is contained in:
Joost Cassee 2011-02-18 13:48:51 +01:00
parent 246c72cc03
commit 327157d610
6 changed files with 49 additions and 13 deletions

View file

@ -10,6 +10,6 @@ Django_ project. See the ``docs`` directory for more information.
__author__ = "Joost Cassee"
__email__ = "joost@cassee.net"
__version__ = "0.5.0"
__version__ = "0.5.1"
__copyright__ = "Copyright (C) 2011 Joost Cassee"
__license__ = "MIT License"

View file

@ -0,0 +1,5 @@
"""
Models for the django-analytical Django application.
This application currently does not use models.
"""

View file

@ -58,7 +58,7 @@ def chartbeat_top(parser, token):
class ChartbeatTopNode(Node):
def render(self, context):
if is_internal_ip(context):
return disable_html(INIT_CODE, self.name)
return disable_html(INIT_CODE, "Chartbeat")
return INIT_CODE
@ -84,12 +84,7 @@ class ChartbeatBottomNode(Node):
def render(self, context):
config = {'uid': self.user_id}
domain = context.get(DOMAIN_CONTEXT_KEY)
if domain is None and getattr(settings, 'CHARTBEAT_AUTO_DOMAIN', True):
try:
domain = Site.objects.get_current().domain
except (ImproperlyConfigured, Site.DoesNotExist):
pass
domain = _get_domain(context)
if domain is not None:
config['domain'] = domain
html = SETUP_CODE % {'config': simplejson.dumps(config)}
@ -102,3 +97,13 @@ def contribute_to_analytical(add_node):
ChartbeatBottomNode() # ensure properly configured
add_node('head_top', ChartbeatTopNode, 'first')
add_node('body_bottom', ChartbeatBottomNode, 'last')
def _get_domain(context):
domain = context.get(DOMAIN_CONTEXT_KEY)
if domain is None and getattr(settings, 'CHARTBEAT_AUTO_DOMAIN', True):
try:
domain = Site.objects.get_current().domain
except (ImproperlyConfigured, Site.DoesNotExist): #pylint: disable=E1101
pass
return domain

View file

@ -13,7 +13,9 @@ from analytical.utils import is_internal_ip, disable_html, get_identity, \
API_KEY_RE = re.compile(r'^\w{6}$')
SETUP_CODE = """<script src="//d1nu2rn22elx8m.cloudfront.net/performable/pax/%(api_key)s.js" type="text/javascript"></script>"""
SETUP_CODE = """
<script src="//d1nu2rn22elx8m.cloudfront.net/performable/pax/%(api_key)s.js" type="text/javascript"></script>
"""
IDENTIFY_CODE = """
<script type="text/javascript">
var _paq = _paq || [];
@ -69,7 +71,7 @@ def performable_embed(hostname, page_id):
"""
Include a Performable landing page.
"""
return EMBED_CODE % locals()
return EMBED_CODE % {'hostname': hostname, 'page_id': page_id}
def contribute_to_analytical(add_node):

View file

@ -10,6 +10,12 @@ HTML_COMMENT = "<!-- %(service)s disabled on internal IP " \
def get_required_setting(setting, value_re, invalid_msg):
"""
Return a constant from ``django.conf.settings``. The `setting`
argument is the constant name, the `value_re` argument is a regular
expression used to validate the setting value and the `invalid_msg`
argument is used as exception message if the value is not valid.
"""
try:
value = getattr(settings, setting)
except AttributeError:
@ -22,6 +28,12 @@ def get_required_setting(setting, value_re, invalid_msg):
def get_identity(context, prefix=None):
"""
Get the identity of a logged in user from a template context.
The `prefix` argument is used to provide different identities to
different analytics services.
"""
if prefix is not None:
try:
return context['%s_identity' % prefix]
@ -46,6 +58,13 @@ def get_identity(context, prefix=None):
def is_internal_ip(context, prefix=None):
"""
Return whether the visitor is coming from an internal IP address,
based on information from the template context.
The prefix is used to allow different analytics services to have
different notions of internal addresses.
"""
try:
request = context['request']
remote_ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
@ -63,12 +82,17 @@ def is_internal_ip(context, prefix=None):
internal_ips = getattr(settings, 'INTERNAL_IPS', '')
return remote_ip in internal_ips
except KeyError, AttributeError:
except (KeyError, AttributeError):
return False
def disable_html(html, service):
return HTML_COMMENT % locals()
"""
Disable HTML code by commenting it out.
The `service` argument is used to display a friendly message.
"""
return HTML_COMMENT % {'html': html, 'service': service}
class AnalyticalException(Exception):

View file

@ -68,6 +68,6 @@ setup(
],
platforms = ['any'],
url = 'http://github.com/jcassee/django-analytical',
download_url = 'http://github.com/jcassee/django-analytical/archives/master',
#download_url = 'http://github.com/jcassee/django-analytical/archives/master',
cmdclass = cmdclass,
)