From 4c72289ac8e9a2bd759397cc08989b8c97505e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matth=C3=A4us=20G=2E=20Chajdas?= Date: Sat, 2 Dec 2017 22:39:03 +0100 Subject: [PATCH 1/2] Use user.is_authenticated for Django 2.0 compatibility. --- analytical/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analytical/utils.py b/analytical/utils.py index b0841cf..309275f 100644 --- a/analytical/utils.py +++ b/analytical/utils.py @@ -71,7 +71,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 user.is_authenticated: if identity_func is not None: return identity_func(user) else: From a9d0befa467643acd48e3a60696b6cb542ec30f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matth=C3=A4us=20G=2E=20Chajdas?= Date: Sun, 3 Dec 2017 11:24:07 +0100 Subject: [PATCH 2/2] Add support for Django <2 and 2; update tox test matrix. --- .travis.yml | 3 +++ analytical/templatetags/intercom.py | 7 ++++--- analytical/templatetags/woopra.py | 3 ++- analytical/utils.py | 15 ++++++++++++++- setup.py | 1 + tox.ini | 3 +++ 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2fcb67c..04c5803 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 } diff --git a/analytical/templatetags/intercom.py b/analytical/templatetags/intercom.py index 96a8dbe..2993bd4 100644 --- a/analytical/templatetags/intercom.py +++ b/analytical/templatetags/intercom.py @@ -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 diff --git a/analytical/templatetags/woopra.py b/analytical/templatetags/woopra.py index 4c0124c..e6c02cc 100644 --- a/analytical/templatetags/woopra.py +++ b/analytical/templatetags/woopra.py @@ -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: diff --git a/analytical/utils.py b/analytical/utils.py index 309275f..45e0fcf 100644 --- a/analytical/utils.py +++ b/analytical/utils.py @@ -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: diff --git a/setup.py b/setup.py index a304cc8..39d485c 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tox.ini b/tox.ini index 4572c7c..309a875 100644 --- a/tox.ini +++ b/tox.ini @@ -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