1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.contrib.sites.models import Site
6 from piwik.django.models import PiwikSite
7 from django.conf import settings
8 from datetime import datetime
10 from functools import update_wrapper
12 from random import random
13 from inspect import isclass
14 from .tasks import track_request
16 logger = logging.getLogger(__name__)
19 def piwik_url(request):
20 return urllib.urlencode(dict(
23 url='http://%s%s' % (request.META['HTTP_HOST'], request.path),
24 rand=int(random() * 0x10000),
25 apiv=PIWIK_API_VERSION,
26 urlref=request.META.get('HTTP_REFERER', ''),
27 ua=request.META.get('HTTP_USER_AGENT', ''),
28 lang=request.META.get('HTTP_ACCEPT_LANGUAGE', ''),
29 token_auth=getattr(settings, 'PIWIK_TOKEN', ''),
30 cip=request.META['REMOTE_ADDR'],
31 cdt=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
37 # Retrieve piwik information
39 _id_piwik = PiwikSite.objects.get(site=Site.objects.get_current().id).id_site
40 except PiwikSite.DoesNotExist:
41 logger.debug("No PiwikSite is configured.")
44 def piwik_track(klass_or_method):
45 """Track decorated class or method using Piwik (according to configuration in settings and django-piwik)
46 Works for handler classes (executed by __call__) or handler methods. Expects request to be the first parameter
49 return klass_or_method
52 if isclass(klass_or_method):
53 klass = klass_or_method
54 call_func = klass.__call__
56 call_func = klass_or_method
58 def wrap(self, request, *args, **kw):
59 track_request.delay(piwik_url(request))
60 return call_func(self, request, *args, **kw)
63 update_wrapper(wrap, call_func)
65 if isclass(klass_or_method):