1.2
[auf_django_piwik.git] / auf / django / piwik / middleware.py
CommitLineData
567ee8ed
OL
1# -*- coding: utf-8 -*-
2
3import re
b9d57605
OL
4
5from django.conf import settings
6
3af9d19f 7from settings import PIWIK_TOKEN, PIWIK_HOST, PIWIK_HTTPFORCE, PIWIK_TRACKCODE
567ee8ed
OL
8
9ire_body = re.compile(re.escape('</body>'), re.IGNORECASE)
10
11
12class TrackMiddleware:
13
14 def process_response(self, request, response):
15 """
16 Trackcode injection avant le body s'il y a un token piwik dans la conf
17 locale.
18 """
19 if PIWIK_TOKEN is None:
20 return response
21
3af9d19f 22 if request.is_secure() and not PIWIK_HTTPFORCE:
567ee8ed
OL
23 protocol = "https"
24 else:
25 protocol = "http"
26
27 track = PIWIK_TRACKCODE % {
28 'host': PIWIK_HOST,
29 'token': PIWIK_TOKEN,
30 'protocol': protocol,
b9d57605 31 'static': settings.STATIC_URL,
567ee8ed
OL
32 }
33
34 content = response.content
35 content_with_trackcode = ire_body.sub('%s</body>' % track, content)
36 response.content = content_with_trackcode
37 return response