Add support for Django <2 and 2; update tox test matrix.

This commit is contained in:
Matthäus G. Chajdas 2017-12-03 11:24:07 +01:00
parent 4c72289ac8
commit a9d0befa46
6 changed files with 27 additions and 5 deletions

View file

@ -11,6 +11,7 @@ env:
- DJANGO=1.9
- DJANGO=1.10
- DJANGO=1.11
- DJANGO=2.0
matrix:
exclude:
# Python/Django combinations that aren't officially supported
@ -22,6 +23,8 @@ matrix:
- { python: 3.3, env: DJANGO=1.10 }
- { python: 3.6, env: DJANGO=1.10 }
- { python: 3.3, env: DJANGO=1.11 }
- { python: 2.7, env: DJANGO=2.0}
- { python: 3.3, env: DJANGO=2.0 }
include:
- { python: 3.6, env: TOXENV=flake8 }
- { python: 3.6, env: TOXENV=readme }

View file

@ -10,7 +10,8 @@ import re
from django.template import Library, Node, TemplateSyntaxError
from analytical.utils import disable_html, get_required_setting, \
is_internal_ip, get_user_from_context, get_identity
is_internal_ip, get_user_from_context, get_identity, \
get_user_is_authenticated
APP_ID_RE = re.compile(r'[\da-z]+$')
TRACKING_CODE = """
@ -58,7 +59,7 @@ class IntercomNode(Node):
params[var[9:]] = val
user = get_user_from_context(context)
if user is not None and user.is_authenticated():
if user is not None and get_user_is_authenticated(user):
if 'name' not in params:
params['name'] = get_identity(
context, 'intercom', self._identify, user)
@ -81,7 +82,7 @@ class IntercomNode(Node):
}
if is_internal_ip(context, 'INTERCOM') \
or not user or not user.is_authenticated():
or not user or not get_user_is_authenticated(user):
# Intercom is disabled for non-logged in users.
html = disable_html(html, 'Intercom')
return html

View file

@ -15,6 +15,7 @@ from analytical.utils import (
get_identity,
get_required_setting,
get_user_from_context,
get_user_is_authenticated,
is_internal_ip,
)
@ -81,7 +82,7 @@ class WoopraNode(Node):
params[var[7:]] = val
if 'name' not in params and 'email' not in params:
user = get_user_from_context(context)
if user is not None and user.is_authenticated():
if user is not None and get_user_is_authenticated(user):
params['name'] = get_identity(
context, 'woopra', self._identify, user)
if user.email:

View file

@ -49,6 +49,19 @@ def get_user_from_context(context):
return None
def get_user_is_authenticated(user):
"""Check if the user is authenticated.
This is a compatibility function needed to support both Django 1.x and 2.x;
Django 2.x turns the function into a proper boolean so function calls will
fail.
"""
if callable(user.is_authenticated):
return user.is_authenticated()
else:
return user.is_authenticated
def get_identity(context, prefix=None, identity_func=None, user=None):
"""
Get the identity of a logged in user from a template context.
@ -71,7 +84,7 @@ def get_identity(context, prefix=None, identity_func=None, user=None):
try:
if user is None:
user = get_user_from_context(context)
if user.is_authenticated:
if get_user_is_authenticated(user):
if identity_func is not None:
return identity_func(user)
else:

View file

@ -79,6 +79,7 @@ setup(
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',

View file

@ -6,6 +6,7 @@ envlist =
py{27,34,35}-django19
py{27,34,35}-django110
py{27,34,35,36}-django111
py{34,35,36}-django20
flake8
readme
@ -21,6 +22,7 @@ deps =
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11
django111: Django>=1.11,<2.0
django20: Django>=2.0,<2.1
passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH
whitelist_externals = sh
@ -39,6 +41,7 @@ DJANGO =
1.9: django19
1.10: django110
1.11: django111
2.0: django20
[flake8]
max-line-length = 100