From 3729eaa960367289a333ee7bd61ec684134ab495 Mon Sep 17 00:00:00 2001 From: odi1n Date: Wed, 14 Sep 2022 21:19:30 +0300 Subject: [PATCH 1/5] Add Liveinternet --- analytical/templatetags/liveinternet.py | 77 +++++++++++++++++++++++++ docs/services/liveinternet.rst | 60 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 analytical/templatetags/liveinternet.py create mode 100644 docs/services/liveinternet.rst 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..e56b102 --- /dev/null +++ b/docs/services/liveinternet.rst @@ -0,0 +1,60 @@ +================================== +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 From dd880885b8f75473cd94001182f10264ab9e58d7 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 19 Sep 2022 16:42:35 +0300 Subject: [PATCH 2/5] Update docs/services/liveinternet.rst Co-authored-by: Peter Bittner --- docs/services/liveinternet.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/services/liveinternet.rst b/docs/services/liveinternet.rst index e56b102..c1a2f16 100644 --- a/docs/services/liveinternet.rst +++ b/docs/services/liveinternet.rst @@ -35,7 +35,7 @@ and the LiveInternet logo:: 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.:: +logo - where the design and content of the page allows. :: {% load liveinternet %} From acede461a9ac29318478ba28757f0416f3e58b98 Mon Sep 17 00:00:00 2001 From: odi1n Date: Mon, 19 Sep 2022 16:44:16 +0300 Subject: [PATCH 3/5] Change text --- docs/services/liveinternet.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/services/liveinternet.rst b/docs/services/liveinternet.rst index c1a2f16..f19d3bc 100644 --- a/docs/services/liveinternet.rst +++ b/docs/services/liveinternet.rst @@ -32,6 +32,7 @@ and the LiveInternet logo:: {% 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 From 20a14a8c8285b133d0cfcf554736f261c41117b1 Mon Sep 17 00:00:00 2001 From: odi1n Date: Mon, 19 Sep 2022 17:41:43 +0300 Subject: [PATCH 4/5] Add tests --- tests/unit/test_tag_liveinternet.py | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/unit/test_tag_liveinternet.py diff --git a/tests/unit/test_tag_liveinternet.py b/tests/unit/test_tag_liveinternet.py new file mode 100644 index 0000000..f00e965 --- /dev/null +++ b/tests/unit/test_tag_liveinternet.py @@ -0,0 +1,58 @@ +""" +Tests for the Yandex.Metrica 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('') From 4592af9569fd1ac1acc14a9fbfcad74242a0632b Mon Sep 17 00:00:00 2001 From: odi1n Date: Mon, 19 Sep 2022 17:47:02 +0300 Subject: [PATCH 5/5] Text correction --- tests/unit/test_tag_liveinternet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_tag_liveinternet.py b/tests/unit/test_tag_liveinternet.py index f00e965..dee0a67 100644 --- a/tests/unit/test_tag_liveinternet.py +++ b/tests/unit/test_tag_liveinternet.py @@ -1,5 +1,5 @@ """ -Tests for the Yandex.Metrica template tags and filters. +Tests for the LiveInternet template tags and filters. """ from django.http import HttpRequest