Merge branch 'optimize-qs-perms' into 'master'

minor: filterbackends for permissions

See merge request djangoldp-packages/djangoldp-notification!36
This commit is contained in:
Jean-Baptiste Pasquier 2020-11-17 12:26:10 +00:00
commit d321dedd32
3 changed files with 25 additions and 1 deletions

View File

@ -0,0 +1,18 @@
from djangoldp.filters import LDPPermissionsFilterBackend
class InboxFilterBackend(LDPPermissionsFilterBackend):
def filter_queryset(self, request, queryset, view):
if not request.user.is_anonymous:
return queryset.filter(user=request.user)
else:
from djangoldp_notification.models import Notification
return Notification.objects.none()
class SubscriptionsFilterBackend(LDPPermissionsFilterBackend):
def filter_queryset(self, request, queryset, view):
if request.method == "OPTIONS":
return queryset
else:
return super().filter_queryset(request, queryset, view)

View File

@ -1,9 +1,13 @@
from django.contrib.auth import get_user_model
from djangoldp.permissions import LDPPermissions
from djangoldp_notification.filters import InboxFilterBackend, SubscriptionsFilterBackend
from rest_framework.reverse import reverse
class InboxPermissions(LDPPermissions):
filter_backends = [InboxFilterBackend]
def has_permission(self, request, view):
from djangoldp.models import Model
@ -41,6 +45,8 @@ class InboxPermissions(LDPPermissions):
class SubscriptionsPermissions(LDPPermissions):
filter_backends = [SubscriptionsFilterBackend]
def has_permission(self, request, view):
if request.user.is_anonymous and not request.method == "OPTIONS":
return False

View File

@ -10,7 +10,7 @@ license = MIT
[options]
packages = find:
install_requires =
djangoldp>=1.2.0
djangoldp>=1.3.0
[options.extras_require]
include_package_data = True