2011-01-26 21:28:16 +00:00
|
|
|
"""
|
|
|
|
|
Utility function for django-analytical.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
|
2011-01-30 09:06:01 +00:00
|
|
|
HTML_COMMENT = "<!-- %(service)s disabled on internal IP " \
|
|
|
|
|
"address\n%(html)s\n-->"
|
2011-01-26 21:28:16 +00:00
|
|
|
|
|
|
|
|
|
2011-01-30 01:50:49 +00:00
|
|
|
def get_required_setting(setting, value_re, invalid_msg):
|
2011-02-18 12:48:51 +00:00
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2011-01-26 21:28:16 +00:00
|
|
|
try:
|
|
|
|
|
value = getattr(settings, setting)
|
|
|
|
|
except AttributeError:
|
2011-01-30 01:50:49 +00:00
|
|
|
raise AnalyticalException("%s setting: not found" % setting)
|
2011-01-26 21:28:16 +00:00
|
|
|
value = str(value)
|
|
|
|
|
if not value_re.search(value):
|
2011-01-30 01:50:49 +00:00
|
|
|
raise AnalyticalException("%s setting: %s: '%s'"
|
2011-01-26 21:28:16 +00:00
|
|
|
% (setting, invalid_msg, value))
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
2011-01-30 01:50:49 +00:00
|
|
|
def get_identity(context, prefix=None):
|
2011-02-18 12:48:51 +00:00
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2011-01-30 01:50:49 +00:00
|
|
|
if prefix is not None:
|
|
|
|
|
try:
|
|
|
|
|
return context['%s_identity' % prefix]
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass
|
2011-01-26 21:28:16 +00:00
|
|
|
try:
|
2011-01-30 01:50:49 +00:00
|
|
|
return context['analytical_identity']
|
2011-01-26 21:28:16 +00:00
|
|
|
except KeyError:
|
|
|
|
|
pass
|
|
|
|
|
if getattr(settings, 'ANALYTICAL_AUTO_IDENTIFY', True):
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
user = context['user']
|
|
|
|
|
except KeyError:
|
|
|
|
|
request = context['request']
|
|
|
|
|
user = request.user
|
|
|
|
|
if user.is_authenticated():
|
|
|
|
|
return user.username
|
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
|
pass
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2011-01-30 01:50:49 +00:00
|
|
|
def is_internal_ip(context, prefix=None):
|
2011-02-18 12:48:51 +00:00
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2011-01-26 21:28:16 +00:00
|
|
|
try:
|
|
|
|
|
request = context['request']
|
2011-01-30 01:50:49 +00:00
|
|
|
remote_ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
|
|
|
|
|
if not remote_ip:
|
2011-01-30 09:06:01 +00:00
|
|
|
remote_ip = request.META.get('REMOTE_ADDR', '')
|
|
|
|
|
if not remote_ip:
|
|
|
|
|
return False
|
2011-01-30 01:50:49 +00:00
|
|
|
|
|
|
|
|
internal_ips = ''
|
|
|
|
|
if prefix is not None:
|
|
|
|
|
internal_ips = getattr(settings, '%s_INTERNAL_IPS' % prefix, '')
|
|
|
|
|
if not internal_ips:
|
|
|
|
|
internal_ips = getattr(settings, 'ANALYTICAL_INTERNAL_IPS', '')
|
|
|
|
|
if not internal_ips:
|
|
|
|
|
internal_ips = getattr(settings, 'INTERNAL_IPS', '')
|
|
|
|
|
|
|
|
|
|
return remote_ip in internal_ips
|
2011-02-18 12:48:51 +00:00
|
|
|
except (KeyError, AttributeError):
|
2011-01-26 21:28:16 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2011-01-30 09:06:01 +00:00
|
|
|
def disable_html(html, service):
|
2011-02-18 12:48:51 +00:00
|
|
|
"""
|
|
|
|
|
Disable HTML code by commenting it out.
|
|
|
|
|
|
|
|
|
|
The `service` argument is used to display a friendly message.
|
|
|
|
|
"""
|
|
|
|
|
return HTML_COMMENT % {'html': html, 'service': service}
|
2011-01-30 01:50:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnalyticalException(Exception):
|
|
|
|
|
"""
|
|
|
|
|
Raised when an exception occurs in any django-analytical code that should
|
|
|
|
|
be silenced in templates.
|
|
|
|
|
"""
|
|
|
|
|
silent_variable_failure = True
|