From a72a9272767880a993a5667e57d002d7bcedb493 Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Sat, 20 Jan 2018 10:10:47 +0100 Subject: [PATCH] Added user.is_{authenticated,anonymous} compatibility --- src/wiki/core/permissions.py | 18 +++++++++--------- src/wiki/decorators.py | 4 ++-- src/wiki/forms.py | 6 +++--- src/wiki/managers.py | 8 ++++---- src/wiki/models/article.py | 2 +- src/wiki/models/urlpath.py | 2 +- src/wiki/plugins/attachments/models.py | 2 +- src/wiki/plugins/attachments/views.py | 6 +++--- src/wiki/plugins/haystack/__init__.py | 4 ++-- src/wiki/plugins/haystack/views.py | 2 +- src/wiki/plugins/images/models.py | 2 +- .../images/templatetags/wiki_images_tags.py | 2 +- src/wiki/views/accounts.py | 4 ++-- src/wiki/views/article.py | 2 +- 14 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/wiki/core/permissions.py b/src/wiki/core/permissions.py index 26020810..cc826bc2 100644 --- a/src/wiki/core/permissions.py +++ b/src/wiki/core/permissions.py @@ -23,11 +23,11 @@ def can_read(article, user): return False # Check access for other users... - if user.is_anonymous() and not settings.ANONYMOUS: + if user.is_anonymous and not settings.ANONYMOUS: return False elif article.other_read: return True - elif user.is_anonymous(): + elif user.is_anonymous: return False if user == article.owner: return True @@ -44,11 +44,11 @@ def can_write(article, user): if callable(settings.CAN_WRITE): return settings.CAN_WRITE(article, user) # Check access for other users... - if user.is_anonymous() and not settings.ANONYMOUS_WRITE: + if user.is_anonymous and not settings.ANONYMOUS_WRITE: return False elif article.other_write: return True - elif user.is_anonymous(): + elif user.is_anonymous: return False if user == article.owner: return True @@ -64,7 +64,7 @@ def can_write(article, user): def can_assign(article, user): if callable(settings.CAN_ASSIGN): return settings.CAN_ASSIGN(article, user) - return not user.is_anonymous() and user.has_perm('wiki.assign') + return not user.is_anonymous and user.has_perm('wiki.assign') def can_assign_owner(article, user): @@ -77,7 +77,7 @@ def can_change_permissions(article, user): if callable(settings.CAN_CHANGE_PERMISSIONS): return settings.CAN_CHANGE_PERMISSIONS(article, user) return ( - not user.is_anonymous() and ( + not user.is_anonymous and ( article.owner == user or user.has_perm('wiki.assign') ) @@ -87,16 +87,16 @@ def can_change_permissions(article, user): def can_delete(article, user): if callable(settings.CAN_DELETE): return settings.CAN_DELETE(article, user) - return not user.is_anonymous() and article.can_write(user) + return not user.is_anonymous and article.can_write(user) def can_moderate(article, user): if callable(settings.CAN_MODERATE): return settings.CAN_MODERATE(article, user) - return not user.is_anonymous() and user.has_perm('wiki.moderate') + return not user.is_anonymous and user.has_perm('wiki.moderate') def can_admin(article, user): if callable(settings.CAN_ADMIN): return settings.CAN_ADMIN(article, user) - return not user.is_anonymous() and user.has_perm('wiki.admin') + return not user.is_anonymous and user.has_perm('wiki.admin') diff --git a/src/wiki/decorators.py b/src/wiki/decorators.py index e932536a..0238f1c3 100644 --- a/src/wiki/decorators.py +++ b/src/wiki/decorators.py @@ -12,7 +12,7 @@ from wiki.core.exceptions import NoRootURL def response_forbidden(request, article, urlpath): - if request.user.is_anonymous(): + if request.user.is_anonymous: qs = request.META.get('QUERY_STRING', '') if qs: qs = urlquote('?' + qs) @@ -143,7 +143,7 @@ def get_article(func=None, can_read=True, can_write=False, # noqa return response_forbidden(request, article, urlpath) if can_create and not ( - request.user.is_authenticated() or settings.ANONYMOUS_CREATE): + request.user.is_authenticated or settings.ANONYMOUS_CREATE): return response_forbidden(request, article, urlpath) if can_delete and not article.can_delete(request.user): diff --git a/src/wiki/forms.py b/src/wiki/forms.py index df29b58c..467de831 100644 --- a/src/wiki/forms.py +++ b/src/wiki/forms.py @@ -117,7 +117,7 @@ class SpamProtectionMixin(object): request = self.request user = None ip_address = None - if request.user.is_authenticated(): + if request.user.is_authenticated: user = request.user else: ip_address = request.META.get('REMOTE_ADDR', None) @@ -151,7 +151,7 @@ class SpamProtectionMixin(object): return from_time = timezone.now() - timedelta(minutes=settings.REVISIONS_MINUTES_LOOKBACK) - if request.user.is_authenticated(): + if request.user.is_authenticated: per_minute = settings.REVISIONS_PER_MINUTES else: per_minute = settings.REVISIONS_PER_MINUTES_ANONYMOUS @@ -164,7 +164,7 @@ class SpamProtectionMixin(object): ) from_time = timezone.now() - timedelta(minutes=60) - if request.user.is_authenticated(): + if request.user.is_authenticated: per_hour = settings.REVISIONS_PER_MINUTES else: per_hour = settings.REVISIONS_PER_MINUTES_ANONYMOUS diff --git a/src/wiki/managers.py b/src/wiki/managers.py index 006274b7..db8b3785 100644 --- a/src/wiki/managers.py +++ b/src/wiki/managers.py @@ -11,7 +11,7 @@ class ArticleQuerySet(QuerySet): are included""" if user.has_perm('wiki.moderator'): return self - if user.is_anonymous(): + if user.is_anonymous: q = self.filter(other_read=True) else: q = self.filter(Q(other_read=True) | @@ -25,7 +25,7 @@ class ArticleQuerySet(QuerySet): are included""" if user.has_perm('wiki.moderator'): return self - if user.is_anonymous(): + if user.is_anonymous: q = self.filter(other_write=True) else: q = self.filter(Q(other_write=True) | @@ -57,7 +57,7 @@ class ArticleFkQuerySetMixin(object): are included""" if user.has_perm('wiki.moderate'): return self - if user.is_anonymous(): + if user.is_anonymous: q = self.filter(article__other_read=True) else: # https://github.com/django-wiki/django-wiki/issues/67 @@ -72,7 +72,7 @@ class ArticleFkQuerySetMixin(object): are included""" if user.has_perm('wiki.moderate'): return self - if user.is_anonymous(): + if user.is_anonymous: q = self.filter(article__other_write=True) else: # https://github.com/django-wiki/django-wiki/issues/67 diff --git a/src/wiki/models/article.py b/src/wiki/models/article.py index 56b9b597..429a7eae 100644 --- a/src/wiki/models/article.py +++ b/src/wiki/models/article.py @@ -316,7 +316,7 @@ class BaseRevisionMixin(models.Model): ) def set_from_request(self, request): - if request.user.is_authenticated(): + if request.user.is_authenticated: self.user = request.user if settings.LOG_IPS_USERS: self.ip_address = request.META.get('REMOTE_ADDR', None) diff --git a/src/wiki/models/urlpath.py b/src/wiki/models/urlpath.py index 4d34ea22..d49db739 100644 --- a/src/wiki/models/urlpath.py +++ b/src/wiki/models/urlpath.py @@ -314,7 +314,7 @@ class URLPath(MPTTModel): """ user = None ip_address = None - if not request.user.is_anonymous(): + if not request.user.is_anonymous: user = request.user if settings.LOG_IPS_USERS: ip_address = request.META.get('REMOTE_ADDR', None) diff --git a/src/wiki/plugins/attachments/models.py b/src/wiki/plugins/attachments/models.py index 59cc55fc..e2ea9032 100644 --- a/src/wiki/plugins/attachments/models.py +++ b/src/wiki/plugins/attachments/models.py @@ -40,7 +40,7 @@ class Attachment(ReusablePlugin): null=True) def can_write(self, user): - if not settings.ANONYMOUS and (not user or user.is_anonymous()): + if not settings.ANONYMOUS and (not user or user.is_anonymous): return False return ReusablePlugin.can_write(self, user) diff --git a/src/wiki/plugins/attachments/views.py b/src/wiki/plugins/attachments/views.py index bdbe3487..9ed6810b 100644 --- a/src/wiki/plugins/attachments/views.py +++ b/src/wiki/plugins/attachments/views.py @@ -41,7 +41,7 @@ class AttachmentView(ArticleMixin, FormView): def form_valid(self, form): - if (self.request.user.is_anonymous() and not settings.ANONYMOUS or + if (self.request.user.is_anonymous and not settings.ANONYMOUS or not self.article.can_write(self.request.user) or self.article.current_revision.locked): return response_forbidden(self.request, self.article, self.urlpath) @@ -81,7 +81,7 @@ class AttachmentView(ArticleMixin, FormView): current_revision__deleted=True) kwargs['search_form'] = forms.SearchForm() kwargs['selected_tab'] = 'attachments' - kwargs['anonymous_disallowed'] = self.request.user.is_anonymous() and not settings.ANONYMOUS + kwargs['anonymous_disallowed'] = self.request.user.is_anonymous and not settings.ANONYMOUS return super().get_context_data(**kwargs) @@ -118,7 +118,7 @@ class AttachmentReplaceView(ArticleMixin, FormView): @method_decorator(get_article(can_write=True, not_locked=True)) def dispatch(self, request, article, attachment_id, *args, **kwargs): - if request.user.is_anonymous() and not settings.ANONYMOUS: + if request.user.is_anonymous and not settings.ANONYMOUS: return response_forbidden(request, article, kwargs.get('urlpath', None)) if article.can_moderate(request.user): self.attachment = get_object_or_404( diff --git a/src/wiki/plugins/haystack/__init__.py b/src/wiki/plugins/haystack/__init__.py index 9fc6a98a..a9a28dcc 100644 --- a/src/wiki/plugins/haystack/__init__.py +++ b/src/wiki/plugins/haystack/__init__.py @@ -21,7 +21,7 @@ class SearchViewHaystack(haystack_views.SearchView): def dispatch(self, request, *args, **kwargs): # Do not allow anonymous users to search if they cannot read content - if request.user.is_anonymous() and not settings.ANONYMOUS: + if request.user.is_anonymous and not settings.ANONYMOUS: return redirect(settings.LOGIN_URL) return super().dispatch(request, *args, **kwargs) @@ -30,7 +30,7 @@ class SearchViewHaystack(haystack_views.SearchView): are included""" if user.has_perm('wiki.moderator'): return self.results - if user.is_anonymous(): + if user.is_anonymous: q = self.results.filter(other_read='True') return q else: diff --git a/src/wiki/plugins/haystack/views.py b/src/wiki/plugins/haystack/views.py index f23a4cc7..97327340 100644 --- a/src/wiki/plugins/haystack/views.py +++ b/src/wiki/plugins/haystack/views.py @@ -12,7 +12,7 @@ class HaystackSearchView(SearchView): def get_queryset(self): qs = SearchQuerySet().all() - if self.request.user.is_authenticated(): + if self.request.user.is_authenticated: if not permissions.can_moderate( models.URLPath.root().article, self.request.user): diff --git a/src/wiki/plugins/images/models.py b/src/wiki/plugins/images/models.py index 24404b44..fc01fe07 100644 --- a/src/wiki/plugins/images/models.py +++ b/src/wiki/plugins/images/models.py @@ -36,7 +36,7 @@ class Image(RevisionPlugin): # anything! :D def can_write(self, user): - if not settings.ANONYMOUS and (not user or user.is_anonymous()): + if not settings.ANONYMOUS and (not user or user.is_anonymous): return False return RevisionPlugin.can_write(self, user) diff --git a/src/wiki/plugins/images/templatetags/wiki_images_tags.py b/src/wiki/plugins/images/templatetags/wiki_images_tags.py index 810da730..612aaf1c 100644 --- a/src/wiki/plugins/images/templatetags/wiki_images_tags.py +++ b/src/wiki/plugins/images/templatetags/wiki_images_tags.py @@ -13,6 +13,6 @@ def images_for_article(article): @register.filter def images_can_add(article, user): - if not settings.ANONYMOUS and (not user or user.is_anonymous()): + if not settings.ANONYMOUS and (not user or user.is_anonymous): return False return article.can_write(user) diff --git a/src/wiki/views/accounts.py b/src/wiki/views/accounts.py index def962a2..5a12d7ff 100644 --- a/src/wiki/views/accounts.py +++ b/src/wiki/views/accounts.py @@ -33,7 +33,7 @@ class Signup(CreateView): def dispatch(self, request, *args, **kwargs): # Let logged in super users continue - if not request.user.is_anonymous() and not request.user.is_superuser: + if not request.user.is_anonymous and not request.user.is_superuser: return redirect('wiki:root') # If account handling is disabled, don't go here if not settings.ACCOUNT_HANDLING: @@ -77,7 +77,7 @@ class Login(FormView): template_name = "wiki/accounts/login.html" def dispatch(self, request, *args, **kwargs): - if not request.user.is_anonymous(): + if not request.user.is_anonymous: return redirect('wiki:root') if not settings.ACCOUNT_HANDLING: return redirect(settings.LOGIN_URL) diff --git a/src/wiki/views/article.py b/src/wiki/views/article.py index 0462faf6..6ff5a654 100644 --- a/src/wiki/views/article.py +++ b/src/wiki/views/article.py @@ -664,7 +664,7 @@ class SearchView(ListView): def dispatch(self, request, *args, **kwargs): self.urlpath = None # Do not allow anonymous users to search if they cannot read content - if request.user.is_anonymous() and not settings.ANONYMOUS: + if request.user.is_anonymous and not settings.ANONYMOUS: return redirect(settings.LOGIN_URL) self.search_form = forms.SearchForm(request.GET) if self.search_form.is_valid(): -- 2.45.2