Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Django 3.1 #65

Merged
merged 5 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ target/
# Jupyter Notebook
.ipynb_checkpoints

# VS Code
.vscode/*

# pyenv
.python-version

Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ jobs:
env: TOXENV=py36-dj22
- python: '3.6'
env: TOXENV=py36-dj30
- python: '3.6'
env: TOXENV=py36-dj31
- python: '3.7'
env: TOXENV=py37-dj22
- python: '3.7'
env: TOXENV=py37-dj30
- python: '3.7'
env: TOXENV=py37-dj31
- python: '3.8'
env: TOXENV=py38-dj22
- python: '3.8'
env: TOXENV=py38-dj30
- python: '3.8'
env: TOXENV=py38-dj31

- python: '3.6'
env: TOXENV=lint
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
=========
4.4.2
----------
# Add support for Django 3.1

4.4.1
----------
# Add Django 3.0 and Python 3.8 support
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Installation
#. Specify a Google Analytics `tracking code <https://support.google.com/analytics/bin/answer.py?hl=en&answer=1008080>`_, i.e.::

GOOGLE_ANALYTICS = {
'google_analytics_id': 'xxx',
'google_analytics_id': 'UA-000000-2',
}

where ``xxx`` is your tracking code.
where ``UA-000000-2`` is your unique tracking code.

#. If you intend tracking through middleware and Celery remember to `install Celery and run its worker process <http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html>`_.

Expand Down
3 changes: 2 additions & 1 deletion google_analytics/middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from bs4 import BeautifulSoup

from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
from google_analytics.utils import build_ga_params, set_cookie
from google_analytics.tasks import send_ga_tracking
from google_analytics.utils import build_ga_params, set_cookie


class GoogleAnalyticsMiddleware(MiddlewareMixin):
Expand Down
1 change: 1 addition & 0 deletions google_analytics/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests

from celery.task import task


Expand Down
6 changes: 3 additions & 3 deletions google_analytics/templatetags/google_analytics_tags.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from six.moves.urllib.parse import parse_qs, urlencode, urlparse

from django import template
from django.conf import settings
from django.urls import reverse
from google_analytics import CAMPAIGN_TRACKING_PARAMS

from six.moves.urllib.parse import parse_qs, urlencode, urlparse

register = template.Library()


Expand Down Expand Up @@ -47,6 +47,6 @@ def google_analytics(context, tracking_code=None, debug=False):
params['utmdebug'] = 1
# build and return the url
url = reverse('google-analytics')
if len(params) > 0:
if params:
url += '?&' + urlencode(params)
return url
16 changes: 7 additions & 9 deletions google_analytics/tests/test_ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import unittest

import responses
from six.moves.urllib.parse import parse_qs

import django
import responses
from django.contrib.sessions.middleware import SessionMiddleware
from django.http import HttpResponse
from django.test import TestCase, override_settings
from django.test.client import Client
from django.test.client import RequestFactory
from django.contrib.sessions.middleware import SessionMiddleware

from google_analytics.utils import COOKIE_NAME, build_ga_params
from google_analytics.templatetags.google_analytics_tags import google_analytics # noqa
from django.test.client import Client, RequestFactory
from google_analytics.middleware import GoogleAnalyticsMiddleware

from six.moves.urllib.parse import parse_qs
from google_analytics.templatetags.google_analytics_tags import \
google_analytics # noqa
from google_analytics.utils import COOKIE_NAME, build_ga_params


class GoogleAnalyticsTestCase(TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from google_analytics.templatetags.google_analytics_tags import (
google_analytics,
)

from django.template import Context, Template
from django.test import RequestFactory, TestCase, override_settings
from google_analytics.templatetags.google_analytics_tags import \
google_analytics


class GoogleAnalyticsTagsTestCase(TestCase):
Expand Down
1 change: 0 additions & 1 deletion google_analytics/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf.urls import url
from google_analytics.views import google_analytics


urlpatterns = [
url(r'^google-analytics/$', google_analytics, name='google-analytics'),
]
23 changes: 9 additions & 14 deletions google_analytics/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import random
import time
import uuid
import structlog

from six import text_type
from six.moves.urllib.parse import quote, urlencode

import structlog
from django.conf import settings
from django.utils.translation import get_language_from_request

from google_analytics import CAMPAIGN_TRACKING_PARAMS

from six import text_type
from six.moves.urllib.parse import quote, urlencode
try:
from urllib.parse import urlparse
except ImportError:
Expand Down Expand Up @@ -61,11 +61,7 @@ def build_ga_params(
meta = request.META
# determine the domian
domain = meta.get('HTTP_HOST', '')
if request.method == 'GET':
ni = '0'
else:
ni = '1'

ni = '0' if request.method == 'GET' else '1'
# determine the referrer
referer = referer or request.GET.get('r', '')
parse_referer = urlparse(referer)
Expand Down Expand Up @@ -148,11 +144,10 @@ def build_ga_params(
ga_url = "http://www.google-analytics.com/collect"
utm_url = ga_url + "?&" + urlencode(params)
ga_logging_enabled = False
if hasattr(settings, 'ENABLE_GA_LOGGING'):
if settings.ENABLE_GA_LOGGING:
log = structlog.get_logger()
log.msg('GA_URL: %s' % utm_url, user_agent=user_agent)
ga_logging_enabled = True
if hasattr(settings, 'ENABLE_GA_LOGGING') and settings.ENABLE_GA_LOGGING:
log = structlog.get_logger()
log.msg('GA_URL: %s' % utm_url, user_agent=user_agent)
ga_logging_enabled = True

locale = get_language_from_request(request)

Expand Down
2 changes: 1 addition & 1 deletion google_analytics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from functools import reduce

import requests

from django.http import HttpResponse
from django.views.decorators.cache import never_cache
from google_analytics.utils import build_ga_params, set_cookie


GIF_DATA = reduce(lambda x, y: x + struct.pack('B', y),
[0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
0x01, 0x00, 0x01, 0x00, 0x80, 0x00,
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import codecs
import os

from setuptools import setup, find_packages
from setuptools import find_packages, setup

HERE = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -23,7 +23,7 @@ def read(*parts):
url='http://github.com/praekelt/django-google-analytics',
packages=find_packages(),
install_requires=[
'Django>=2.2.5,<3.1',
'Django>=2.2.5,<3.2',
'django-celery',
'celery<4.0',
'requests',
Expand All @@ -37,7 +37,7 @@ def read(*parts):
},
include_package_data=True,
classifiers=[
'Development Status :: 4 - Beta',
'Development Status :: 5 - Production/Stable',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
Expand Down
1 change: 0 additions & 1 deletion test_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os


SECRET_KEY = 'foo'
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ envlist =
deps =
dj22: Django>=2.2,<2.3
dj30: Django>=3.0,<3.1
dj31: Django>=3.1,<3.2
coverage
extras = test
setenv =
Expand Down