genrowanie pdf: usnięcie bloku try..catch; xml na pewno jest poprawny, ponieważ zosta...
[wolnelektury.git] / apps / opds / utils.py
1
2 from django.contrib.sites.models import Site
3 from piwik.django.models import PiwikSite
4 from django.conf import settings
5 import logging
6 from functools import update_wrapper
7 import httplib
8 import urlparse
9 from random import random
10
11 logging.basicConfig(level=logging.WARN)
12 logger = logging.getLogger(__name__)
13
14
15 def piwik_url(**kw):
16     url = settings.PIWIK_URL + u"/piwik.php?"
17     url += u'&'.join([k + u"=" + str(v) for k, v in kw.items()])
18     return url
19
20 PIWIK_API_VERSION = 1
21
22
23 def piwik_track(klass):
24     current_site = Site.objects.get_current()
25     piwik_site = PiwikSite.objects.filter(site=current_site.id)
26
27     if len(piwik_site) == 0:
28         logger.warn("No PiwikSite is configured for Site " + current_site.name)
29         return klass
30
31     id_piwik = piwik_site[0].id_site
32     call_func = klass.__call__
33     host = urlparse.urlsplit(settings.PIWIK_URL).netloc
34
35     def wrap(self, request, *args, **kw):
36         conn = httplib.HTTPConnection(host)
37         conn.request('GET', piwik_url(
38             rec=1,
39             apiv=PIWIK_API_VERSION,
40             rand=int(random() * 0x10000),
41             cip=request.META['REMOTE_ADDR'],
42             url='http://' + request.META['HTTP_HOST'] + request.path,
43             urlref=request.META['HTTP_REFERER'] if 'HTTP_REFERER' in request.META else '',
44             idsite=id_piwik))
45
46         conn.close()
47         return call_func(self, request, *args, **kw)
48
49     update_wrapper(wrap, call_func)
50     klass.__call__ = wrap
51
52     return klass