Browse Source

Cleanup

tags/0.1.9
Johann Schmitz 3 years ago
parent
commit
8f7622ab5d
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
18 changed files with 181 additions and 146 deletions
  1. +37
    -5
      Makefile
  2. +1
    -0
      django_pypiwik/__init__.py
  3. +0
    -0
      django_pypiwik/admin.py
  4. +17
    -0
      django_pypiwik/decorators.py
  5. +23
    -0
      django_pypiwik/middleware.py
  6. +0
    -0
      django_pypiwik/migrations/0001_initial.py
  7. +0
    -0
      django_pypiwik/migrations/0002_auto_20150718_1439.py
  8. +0
    -0
      django_pypiwik/migrations/__init__.py
  9. +10
    -0
      django_pypiwik/models.py
  10. +42
    -0
      django_pypiwik/tracker.py
  11. +36
    -0
      django_pypiwik/views.py
  12. +15
    -13
      setup.py
  13. +0
    -17
      src/django_pypiwik/decorators.py
  14. +0
    -23
      src/django_pypiwik/middleware.py
  15. +0
    -0
      src/django_pypiwik/migrations/__init__.py
  16. +0
    -10
      src/django_pypiwik/models.py
  17. +0
    -42
      src/django_pypiwik/tracker.py
  18. +0
    -36
      src/django_pypiwik/views.py

+ 37
- 5
Makefile View File

@@ -1,15 +1,47 @@
TARGET?=tests

clean:
rm -r dist build src/*.egg-info
SRC_PATH := django_pypiwik

VERSION := $(shell grep -Po '"(.*)"' $(SRC_PATH)/__init__.py | sed -e 's/"//g')

#test_default_python:
# PYTHONPATH="." python tests/ -v
#
#test_py2:
# @echo Executing test with python2
# PYTHONPATH="." python2 tests/ -v
#
#test_py3:
# @echo Executing test with python3
# PYTHONPATH="." python3 tests/ -v
#
#test: test_py2 test_py3

test:
PYTHONPATH=".:./src" python tests/
compile:
@echo Compiling python code
python -m compileall $(SRC_PATH)/

compile_optimized:
@echo Compiling python code optimized
python -O -m compileall $(SRC_PATH)/

coverage:
coverage erase
PYTHONPATH=".:./src" coverage run --source='src' --omit='src/test.py' --branch tests/__main__.py
PYTHONPATH="." coverage run --source='$(SRC_PATH)' --branch tests/__main__.py
coverage xml -i
coverage report -m

sonar:
/usr/local/bin/sonar-scanner/bin/sonar-scanner -Dsonar.projectVersion=$(VERSION)

clean:
find -name "*.py?" -delete
rm -f coverage.xml testresults.xml
rm -fr htmlcov dist build *.egg-info

release:
python setup.py sdist upload -r pypi

travis: compile compile_optimized # test_default_python coverage

jenkins: travis sonar

+ 1
- 0
django_pypiwik/__init__.py View File

@@ -0,0 +1 @@
VERSION="0.1.8"

src/django_pypiwik/admin.py → django_pypiwik/admin.py View File


+ 17
- 0
django_pypiwik/decorators.py View File

@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from functools import wraps
from django_pypiwik.tracker import DjangoPiwikTracker
from pypiwik.decorators import track_page_view as pp_track_page_view
from django.utils.decorators import available_attrs


def track_page_view(**tracker_kwargs):
def decorator(func):
@wraps(func, assigned=available_attrs(func))
def inner(request, *args, **kwargs):
@pp_track_page_view(tracker=DjangoPiwikTracker.for_current_site(request=request, **tracker_kwargs))
def inner2():
return func(request, *args, **kwargs)
return inner2()
return inner
return decorator

+ 23
- 0
django_pypiwik/middleware.py View File

@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch

from django_pypiwik.tracker import DjangoPiwikTracker


class PiwikTrackingMiddleware(object):

def process_response(self, request, response):
exclude_admin = getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_EXCLUDE_ADMIN', True) == True

try:
admin_url = reverse('admin:index')
except NoReverseMatch:
admin_url = None

if exclude_admin and admin_url and request.path.startswith(admin_url):
return response

tracker = DjangoPiwikTracker.for_current_site(request=request)
tracker.track_page_view(**getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_PARAMS', {}))
return response

src/django_pypiwik/migrations/0001_initial.py → django_pypiwik/migrations/0001_initial.py View File


src/django_pypiwik/migrations/0002_auto_20150718_1439.py → django_pypiwik/migrations/0002_auto_20150718_1439.py View File


src/django_pypiwik/__init__.py → django_pypiwik/migrations/__init__.py View File


+ 10
- 0
django_pypiwik/models.py View File

@@ -0,0 +1,10 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models

class PiwikConfiguration(models.Model):
site = models.OneToOneField(Site, related_name='piwik_configuration')

piwik_url = models.URLField(default=getattr(settings, 'PIWIK_URL', ''))
piwik_site_id = models.PositiveSmallIntegerField()
piwik_token_auth = models.CharField(max_length=250, null=True, blank=True)

+ 42
- 0
django_pypiwik/tracker.py View File

@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
import logging

from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.safestring import mark_safe
from django_pypiwik.models import PiwikConfiguration
from pypiwik.tracker import PiwikTracker


class DjangoPiwikTracker(PiwikTracker):

def __init__(self, *args, **kwargs):
d = getattr(settings, 'PIWIK_PARAMS', {})
d.update(kwargs.get('values', {}))
kwargs['values'] = d
super(DjangoPiwikTracker, self).__init__(*args, **kwargs)

@staticmethod
def for_current_site(*args, **kwargs):
try:
config = PiwikConfiguration.objects.get(site=Site.objects.get_current())
if config.piwik_token_auth:
kwargs.setdefault('token_auth', config.piwik_token_auth)
return DjangoPiwikTracker(config.piwik_url, config.piwik_site_id, *args, **kwargs)
except PiwikConfiguration.DoesNotExist:
token_auth = getattr(settings, 'PIWIK_TOKEN_AUTH', None)
if token_auth:
kwargs.setdefault('token_auth', token_auth)
return DjangoPiwikTracker(settings.PIWIK_URL, settings.PIWIK_SITE_ID, *args, **kwargs)

def track_page_view(self, **kwargs):
if settings.DEBUG and not getattr(settings, 'PIWIK_ALLOW_DEBUG', False):
logging.info("track_page_view() intercepted because settings.DEBUG = True and not overriden with settings.PIWIK_ALLOW_DEBUG = True")
else:
return super(DjangoPiwikTracker, self).track_page_view(**kwargs)

def tracking_code(self):
if settings.DEBUG and not getattr(settings, 'PIWIK_ALLOW_DEBUG', False):
return mark_safe("""<!-- Piwik tracking code not generated because settings.DEBUG = True and not overridden with settings.PIWIK_ALLOW_DEBUG = True -->""")
else:
return mark_safe(super(DjangoPiwikTracker, self).tracking_code())

+ 36
- 0
django_pypiwik/views.py View File

@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-

from pypiwik.tracker import PARAMETERS as PIWIK_PARAMETERS
from django_pypiwik.tracker import DjangoPiwikTracker


class PiwikMixin(object):

def get_context_data(self, **kwargs):
context = super(PiwikMixin, self).get_context_data(**kwargs)
context['piwik_tracker'] = self.piwik_tracker
return context

@property
def piwik_tracker(self):
if not hasattr(self, '_piwik_tracker'):
self._piwik_tracker = DjangoPiwikTracker.for_current_site(self.request)

self._piwik_tracker.update(self._get_piwik_params())
return self._piwik_tracker

def _get_piwik_params(self):
params = {}

for param_name in PIWIK_PARAMETERS.keys():
my_attr = getattr(self, 'piwik_%s' % param_name, None)

if not my_attr:
continue

value = my_attr() if callable(my_attr) else my_attr

if value:
params[param_name] = value

return params

+ 15
- 13
setup.py View File

@@ -3,18 +3,20 @@

from setuptools import setup, find_packages

from django_pypiwik import VERSION

setup(
name='django-pypiwik',
version='0.1.8',
description='Django helper application around pypiwik',
long_description="See https://code.not-your-server.de/django-pypiwik.git",
author='Johann Schmitz',
author_email='johann@j-schmitz.net',
url='https://ercpe.de/projects/django-pypiwik',
download_url='https://code.not-your-server.de/django-pypiwik.git/tags/',
packages=find_packages('src'),
package_dir={'': 'src'},
include_package_data=True,
zip_safe=False,
license='GPL-3',
name='django-pypiwik',
version=VERSION,
description='Django helper application around pypiwik',
long_description="See https://code.not-your-server.de/django-pypiwik.git",
author='Johann Schmitz',
author_email='johann@j-schmitz.net',
url='https://ercpe.de/projects/django-pypiwik',
download_url='https://code.not-your-server.de/django-pypiwik.git/tags/',
packages=find_packages('src'),
package_dir={'': 'src'},
include_package_data=True,
zip_safe=False,
license='GPL-3',
)

+ 0
- 17
src/django_pypiwik/decorators.py View File

@@ -1,17 +0,0 @@
# -*- coding: utf-8 -*-
from functools import wraps
from django_pypiwik.tracker import DjangoPiwikTracker
from pypiwik.decorators import track_page_view as pp_track_page_view
from django.utils.decorators import available_attrs


def track_page_view(**tracker_kwargs):
def decorator(func):
@wraps(func, assigned=available_attrs(func))
def inner(request, *args, **kwargs):
@pp_track_page_view(tracker=DjangoPiwikTracker.for_current_site(request=request, **tracker_kwargs))
def inner2():
return func(request, *args, **kwargs)
return inner2()
return inner
return decorator

+ 0
- 23
src/django_pypiwik/middleware.py View File

@@ -1,23 +0,0 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch

from django_pypiwik.tracker import DjangoPiwikTracker


class PiwikTrackingMiddleware(object):

def process_response(self, request, response):
exclude_admin = getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_EXCLUDE_ADMIN', True) == True

try:
admin_url = reverse('admin:index')
except NoReverseMatch:
admin_url = None

if exclude_admin and admin_url and request.path.startswith(admin_url):
return response

tracker = DjangoPiwikTracker.for_current_site(request=request)
tracker.track_page_view(**getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_PARAMS', {}))
return response

+ 0
- 0
src/django_pypiwik/migrations/__init__.py View File


+ 0
- 10
src/django_pypiwik/models.py View File

@@ -1,10 +0,0 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models

class PiwikConfiguration(models.Model):
site = models.OneToOneField(Site, related_name='piwik_configuration')

piwik_url = models.URLField(default=getattr(settings, 'PIWIK_URL', ''))
piwik_site_id = models.PositiveSmallIntegerField()
piwik_token_auth = models.CharField(max_length=250, null=True, blank=True)

+ 0
- 42
src/django_pypiwik/tracker.py View File

@@ -1,42 +0,0 @@
# -*- coding: utf-8 -*-
import logging

from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.safestring import mark_safe
from django_pypiwik.models import PiwikConfiguration
from pypiwik.tracker import PiwikTracker


class DjangoPiwikTracker(PiwikTracker):

def __init__(self, *args, **kwargs):
d = getattr(settings, 'PIWIK_PARAMS', {})
d.update(kwargs.get('values', {}))
kwargs['values'] = d
super(DjangoPiwikTracker, self).__init__(*args, **kwargs)

@staticmethod
def for_current_site(*args, **kwargs):
try:
config = PiwikConfiguration.objects.get(site=Site.objects.get_current())
if config.piwik_token_auth:
kwargs.setdefault('token_auth', config.piwik_token_auth)
return DjangoPiwikTracker(config.piwik_url, config.piwik_site_id, *args, **kwargs)
except PiwikConfiguration.DoesNotExist:
token_auth = getattr(settings, 'PIWIK_TOKEN_AUTH', None)
if token_auth:
kwargs.setdefault('token_auth', token_auth)
return DjangoPiwikTracker(settings.PIWIK_URL, settings.PIWIK_SITE_ID, *args, **kwargs)

def track_page_view(self, **kwargs):
if settings.DEBUG and not getattr(settings, 'PIWIK_ALLOW_DEBUG', False):
logging.info("track_page_view() intercepted because settings.DEBUG = True and not overriden with settings.PIWIK_ALLOW_DEBUG = True")
else:
return super(DjangoPiwikTracker, self).track_page_view(**kwargs)

def tracking_code(self):
if settings.DEBUG and not getattr(settings, 'PIWIK_ALLOW_DEBUG', False):
return mark_safe("""<!-- Piwik tracking code not generated because settings.DEBUG = True and not overridden with settings.PIWIK_ALLOW_DEBUG = True -->""")
else:
return mark_safe(super(DjangoPiwikTracker, self).tracking_code())

+ 0
- 36
src/django_pypiwik/views.py View File

@@ -1,36 +0,0 @@
# -*- coding: utf-8 -*-

from pypiwik.tracker import PARAMETERS as PIWIK_PARAMETERS
from django_pypiwik.tracker import DjangoPiwikTracker


class PiwikMixin(object):

def get_context_data(self, **kwargs):
context = super(PiwikMixin, self).get_context_data(**kwargs)
context['piwik_tracker'] = self.piwik_tracker
return context

@property
def piwik_tracker(self):
if not hasattr(self, '_piwik_tracker'):
self._piwik_tracker = DjangoPiwikTracker.for_current_site(self.request)

self._piwik_tracker.update(self._get_piwik_params())
return self._piwik_tracker

def _get_piwik_params(self):
params = {}

for param_name in PIWIK_PARAMETERS.keys():
my_attr = getattr(self, 'piwik_%s' % param_name, None)

if not my_attr:
continue

value = my_attr() if callable(my_attr) else my_attr

if value:
params[param_name] = value

return params

Loading…
Cancel
Save