1 # -*- coding: utf-8 -*-
5 from django
.conf
import settings
8 import auf
.django
.references
.models
as ref
9 REFERENCES_CHARGEES
= True
11 REFERENCES_CHARGEES
= False
14 from settings
import PIWIK_TOKEN
, PIWIK_HOST
, PIWIK_HTTPFORCE
,\
15 PIWIK_TRACKCODE
, PIWIK_EXCLUDE_REFERER
18 ire_body
= re
.compile(re
.escape('</body>'), re
.IGNORECASE
)
21 class TrackMiddleware
:
23 def process_response(self
, request
, response
):
25 Trackcode injection avant le body s'il y a un token piwik dans la conf
28 if PIWIK_TOKEN
is None or response
.get('Content-Type', '') != 'text/html':
31 http_referer
= request
.META
.get('HTTP_REFERER', "")
32 referer
= http_referer
34 for excl
in PIWIK_EXCLUDE_REFERER
:
35 if excl
in http_referer
:
39 if request
.is_secure() and not PIWIK_HTTPFORCE
:
45 user
= getattr(request
, 'user', None)
46 if user
and REFERENCES_CHARGEES
and user
.is_authenticated():
48 employe
= ref
.Employe
.objects
.get(courriel
=request
.user
.email
)
49 imp_id
= employe
.implantation
.id
50 implantation
= "piwikTracker.setCustomVariable(1, 'implantation', '%s', 'visit');" % imp_id
54 track
= PIWIK_TRACKCODE
% {
58 'static': settings
.STATIC_URL
,
60 'implantation': implantation
,
62 content
= response
.content
63 content_with_trackcode
= ire_body
.sub('%s</body>' % track
, content
)
64 response
.content
= content_with_trackcode