[PATCH 08/19] compat: Handle is_authenticated move to property
Stephen Finucane
stephen at that.guru
Thu May 11 05:45:17 AEST 2017
This raises a warning in Django 1.10 and will cause an error in 2.0.
This resolves all issues with Django 1.9.
Signed-off-by: Stephen Finucane <stephen at that.guru>
---
patchwork/api/bundle.py | 3 ++-
patchwork/compat.py | 14 ++++++++++++++
patchwork/models.py | 5 +++--
patchwork/paginator.py | 4 +++-
patchwork/views/__init__.py | 3 ++-
patchwork/views/patch.py | 5 +++--
6 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/patchwork/api/bundle.py b/patchwork/api/bundle.py
index 5fa79b8..88d74a5 100644
--- a/patchwork/api/bundle.py
+++ b/patchwork/api/bundle.py
@@ -25,6 +25,7 @@ from rest_framework.serializers import SerializerMethodField
from patchwork.api.base import PatchworkPermission
from patchwork.api.filters import BundleFilter
+from patchwork.compat import is_authenticated
from patchwork.models import Bundle
@@ -55,7 +56,7 @@ class BundleMixin(object):
serializer_class = BundleSerializer
def get_queryset(self):
- if not self.request.user.is_anonymous():
+ if is_authenticated(self.request.user):
bundle_filter = Q(owner=self.request.user) | Q(public=True)
else:
bundle_filter = Q(public=True)
diff --git a/patchwork/compat.py b/patchwork/compat.py
index cd4da4c..38a844d 100644
--- a/patchwork/compat.py
+++ b/patchwork/compat.py
@@ -89,3 +89,17 @@ if django.VERSION >= (1, 10):
else:
from django.core.urlresolvers import NoReverseMatch # noqa
from django.core.urlresolvers import reverse # noqa
+
+
+# is_authenticated
+#
+# models.User.is_authenticated is now an attribute in Django 1.10 instead of a
+# function
+#
+# https://docs.djangoproject.com/en/dev/releases/1.10/
+
+def is_authenticated(user):
+ if django.VERSION >= (1, 10):
+ return user.is_authenticated
+ else:
+ return user.is_authenticated()
diff --git a/patchwork/models.py b/patchwork/models.py
index 725d0b0..bb8398b 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -33,6 +33,7 @@ from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property
+from patchwork.compat import is_authenticated
from patchwork.fields import HashField
from patchwork.hasher import hash_diff
@@ -81,7 +82,7 @@ class Project(models.Model):
use_tags = models.BooleanField(default=True)
def is_editable(self, user):
- if not user.is_authenticated():
+ if not is_authenticated(user):
return False
return self in user.profile.maintainer_projects.all()
@@ -423,7 +424,7 @@ class Patch(SeriesMixin, Submission):
self.refresh_tag_counts()
def is_editable(self, user):
- if not user.is_authenticated():
+ if not is_authenticated(user):
return False
if user in [self.submitter.user, self.delegate]:
diff --git a/patchwork/paginator.py b/patchwork/paginator.py
index 526a328..609e908 100644
--- a/patchwork/paginator.py
+++ b/patchwork/paginator.py
@@ -22,6 +22,8 @@ from __future__ import absolute_import
from django.conf import settings
from django.core import paginator
+from patchwork.compat import is_authenticated
+
DEFAULT_ITEMS_PER_PAGE = 100
LONG_PAGE_THRESHOLD = 30
@@ -42,7 +44,7 @@ class Paginator(paginator.Paginator):
items_per_page = settings.DEFAULT_ITEMS_PER_PAGE
- if request.user.is_authenticated():
+ if is_authenticated(request.user):
items_per_page = request.user.profile.items_per_page
super(Paginator, self).__init__(objects, items_per_page)
diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
index c884515..3baf299 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -20,6 +20,7 @@
from django.contrib import messages
from django.shortcuts import get_object_or_404
+from patchwork.compat import is_authenticated
from patchwork.filters import Filters
from patchwork.forms import MultiplePatchForm
from patchwork.models import Bundle
@@ -229,7 +230,7 @@ def generic_list(request, project, view, view_args=None, filter_settings=None,
user = request.user
properties_form = None
- if user.is_authenticated():
+ if is_authenticated(user):
# we only pass the post data to the MultiplePatchForm if that was
# the actual form submitted
data_tmp = None
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index 5dafd65..09705ef 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -26,6 +26,7 @@ from django.shortcuts import get_object_or_404
from django.shortcuts import render
from patchwork.compat import reverse
+from patchwork.compat import is_authenticated
from patchwork.forms import CreateBundleForm
from patchwork.forms import PatchForm
from patchwork.models import Bundle
@@ -65,7 +66,7 @@ def patch_detail(request, patch_id):
if editable:
form = PatchForm(instance=patch)
- if request.user.is_authenticated():
+ if is_authenticated(request.user):
createbundleform = CreateBundleForm()
if request.method == 'POST':
@@ -106,7 +107,7 @@ def patch_detail(request, patch_id):
form.save()
messages.success(request, 'Patch updated')
- if request.user.is_authenticated():
+ if is_authenticated(request.user):
context['bundles'] = Bundle.objects.filter(owner=request.user)
context['submission'] = patch
--
2.9.3
More information about the Patchwork
mailing list