diff --git a/analytical/templatetags/liveinternet.py b/analytical/templatetags/liveinternet.py new file mode 100644 index 0000000..8c3b628 --- /dev/null +++ b/analytical/templatetags/liveinternet.py @@ -0,0 +1,77 @@ +from django.template import Library, Node + +from analytical.utils import is_internal_ip, disable_html + +LIVEINTERNET_WITH_IMAGE = """ + +""" + +LIVEINTERNET_CODE = """ + +""" +LIVEINTERNET_IMAGE = """ + + +""" + +register = Library() + + +@register.tag +def liveinternet(parser, token): + """ + Body Liveinternet, full image and code template tag. + + Render the body Javascript code and image for Liveinternet. + """ + return LiveInternetNode(LIVEINTERNET_WITH_IMAGE, 'liveinternet_with_image') + + +@register.tag +def liveinternet_code(parser, token): + """ + Top Liveinternet,code template tag. + + Render the top Javascript code for Liveinternet. + """ + return LiveInternetNode(LIVEINTERNET_CODE, 'liveinternet_code') + + +@register.tag +def liveinternet_img(parser, token): + """ + Body Liveinternet image template tag. + + Render the body Javascript code for Liveinternet. + """ + return LiveInternetNode(LIVEINTERNET_IMAGE, 'liveinternet_image') + + +class LiveInternetNode(Node): + def __init__(self, key, name): + self.key = key + self.name = name + + def render(self, context): + if is_internal_ip(context): + return disable_html(self.key, self.name) + return LIVEINTERNET_CODE diff --git a/docs/services/liveinternet.rst b/docs/services/liveinternet.rst new file mode 100644 index 0000000..f19d3bc --- /dev/null +++ b/docs/services/liveinternet.rst @@ -0,0 +1,61 @@ +================================== +Liveinternet -- traffic analysis +================================== + + +`Liveinternet`_ is an analytics tool like as google analytics. + +.. _`Liveinternet`: https://www.liveinternet.ru/code/ + + +.. yandex-metrica-installation: + +Installation +============ + +To start using the Liveinternet integration, you must have installed the +django-analytical package and have added the ``analytical`` application +to :const:`INSTALLED_APPS` in your project :file:`settings.py` file. +See :doc:`../install` for details. + +Next you need to add the Liveinternet template tag to your templates. + +The Liveinternet counter code is inserted into templates using a template +tag. Load the :mod:`liveinternet` template tag library and insert the +:ttag:`liveinternet` tag. To display as a single image combining a counter +and the LiveInternet logo:: + + {% load liveinternet %} + +
+ ... + {% liveinternet %} + + ... + +In the form of two images, one of which is a counter (transparent GIF size 1x1), +and the other is the LiveInternet logo. This placement method will allow you to +insert the code of the invisible counter at the beginning of the page, and the +logo - where the design and content of the page allows. :: + + {% load liveinternet %} + + + ... + {% liveinternet_code %} + + + ... + {% liveinternet_img %} + ... + + + +Internal IP addresses +--------------------- + +Usually you do not want to track clicks from your development or +internal IP addresses. It takes the value of +:const:`ANALYTICAL_INTERNAL_IPS` by default (which in turn is +:const:`INTERNAL_IPS` by default). See :ref:`identifying-visitors` for +important information about detecting the visitor IP address. \ No newline at end of file diff --git a/tests/unit/test_tag_liveinternet.py b/tests/unit/test_tag_liveinternet.py new file mode 100644 index 0000000..dee0a67 --- /dev/null +++ b/tests/unit/test_tag_liveinternet.py @@ -0,0 +1,58 @@ +""" +Tests for the LiveInternet template tags and filters. +""" + +from django.http import HttpRequest +from django.template import Context +from django.test.utils import override_settings + +from analytical.templatetags.liveinternet import (LiveInternetNode, + LIVEINTERNET_WITH_IMAGE, + LIVEINTERNET_CODE, + LIVEINTERNET_IMAGE) +from utils import TagTestCase + + +class LiveInternetTagTestCase(TagTestCase): + """ + Tests for the ``liveinternet`` template tag. + """ + + def test_render_liveinternet(self): + response = self.render_tag('liveinternet', 'liveinternet') + assert '') + + @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1']) + def test_render_liveinternet_code_render_ip(self): + req = HttpRequest() + req.META['REMOTE_ADDR'] = '1.1.1.1' + context = Context({'request': req}) + r = LiveInternetNode(LIVEINTERNET_CODE, 'liveinternet_code').render(context) + assert r.startswith('') + + @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1']) + def test_render_liveinternet_img_render_ip(self): + req = HttpRequest() + req.META['REMOTE_ADDR'] = '1.1.1.1' + context = Context({'request': req}) + r = LiveInternetNode(LIVEINTERNET_IMAGE, 'liveinternet_image').render(context) + assert r.startswith('')