piwik release 1.0
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Tue, 11 Sep 2012 13:54:43 +0000 (09:54 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Tue, 11 Sep 2012 13:54:43 +0000 (09:54 -0400)
.gitignore [new file with mode: 0644]
CHANGES [new file with mode: 0644]
MANIFEST.in [new file with mode: 0644]
auf/__init__.py [new file with mode: 0755]
auf/django/__init__.py [new file with mode: 0755]
auf/django/piwik/__init__.py [new file with mode: 0755]
auf/django/piwik/middleware.py [new file with mode: 0644]
auf/django/piwik/settings.py [new file with mode: 0644]
setup.cfg [new file with mode: 0644]
setup.py [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..40a0928
--- /dev/null
@@ -0,0 +1,22 @@
+# binaires
+*.pyc
+*.pyo
+
+# Fichier temporaires:
+.*.swp
+*~
+\#*#
+src/*
+
+# DB de dev
+*.db
+
+# restants de merge
+*.orig
+*.rej
+
+*-info
+doc/_*
+doc/build
+doc/pip-log.txt
+dist
diff --git a/CHANGES b/CHANGES
new file mode 100644 (file)
index 0000000..c61af7e
--- /dev/null
+++ b/CHANGES
@@ -0,0 +1,4 @@
+1.0
+---
+
+* middleware qui ajoute le code de tracking
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/auf/__init__.py b/auf/__init__.py
new file mode 100755 (executable)
index 0000000..de40ea7
--- /dev/null
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
diff --git a/auf/django/__init__.py b/auf/django/__init__.py
new file mode 100755 (executable)
index 0000000..de40ea7
--- /dev/null
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
diff --git a/auf/django/piwik/__init__.py b/auf/django/piwik/__init__.py
new file mode 100755 (executable)
index 0000000..e69de29
diff --git a/auf/django/piwik/middleware.py b/auf/django/piwik/middleware.py
new file mode 100644 (file)
index 0000000..4dccf23
--- /dev/null
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+
+import re
+from settings import PIWIK_TOKEN, PIWIK_HOST, PIWIK_TRACKCODE
+
+ire_body = re.compile(re.escape('</body>'), re.IGNORECASE)
+
+
+class TrackMiddleware:
+
+    def process_response(self, request, response):
+        """
+        Trackcode injection avant le body s'il y a un token piwik dans la conf
+        locale.
+        """
+        if PIWIK_TOKEN is None:
+            return response
+
+        if request.is_secure():
+            protocol = "https"
+        else:
+            protocol = "http"
+
+        track = PIWIK_TRACKCODE % {
+                'host': PIWIK_HOST,
+                'token': PIWIK_TOKEN,
+                'protocol': protocol,
+                }
+
+        content = response.content
+        content_with_trackcode = ire_body.sub('%s</body>' % track, content)
+        response.content = content_with_trackcode
+        return response
diff --git a/auf/django/piwik/settings.py b/auf/django/piwik/settings.py
new file mode 100644 (file)
index 0000000..2bc504d
--- /dev/null
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+
+
+from django.conf import settings
+
+PIWIK_TOKEN = getattr(settings, 'PIWIK_TOKEN', None)
+
+PIWIK_HOST = getattr(settings, 'PIWIK_HOST', 'piwik.auf.org')
+
+PIWIK_TRACKCODE = """
+<!-- Piwik -->
+<script type="text/javascript">
+    var pkBaseURL = "%(protocol)s://%(host)s/";
+    document.write(unescape("%%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%%3E%%3C/script%%3E"));
+    </script><script type="text/javascript">
+    try {
+        var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", %(token)s);
+        piwikTracker.trackPageView();
+        piwikTracker.enableLinkTracking();
+    }
+    catch( err ) {}
+</script>
+<noscript>
+    <p><img src="%(protocol)s://%(host)s/piwik.php?idsite=%(token)s" style="border:0" alt="" /> </p>
+</noscript>
+<!-- End Piwik Tracking Code -->
+"""
diff --git a/setup.cfg b/setup.cfg
new file mode 100644 (file)
index 0000000..49a8107
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,2 @@
+[egg_info]
+tag_svn_revision = true
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..bb491a8
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,27 @@
+from setuptools import setup, find_packages
+
+name = 'auf.django.piwik'
+version = '1.0'
+
+setup(name=name,
+      version=version,
+      description="Middleware pour incorper le code de tracking de Piwik",
+      long_description="""\
+""",
+      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+      keywords='Django Piwik',
+      author='Olivier Larchev\xc3\xaaque',
+      author_email='olivier.larcheveque@auf.org',
+      url='http://pypi.auf.org/%s' % name,
+      license='GPL',
+      namespace_packages = ['auf', 'auf.django', ],
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=[
+          # -*- Extra requirements: -*-
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      """,
+      )