Source code for pushnotifications.api.v2.views
from django.utils.translation import get_language_from_request
from oauth2_provider.contrib.rest_framework import (
IsAuthenticatedOrTokenHasScope,
TokenMatchesOASRequirements,
OAuth2Authentication,
)
from rest_framework.filters import OrderingFilter
from rest_framework.generics import (
ListAPIView,
RetrieveAPIView,
CreateAPIView,
UpdateAPIView,
)
from pushnotifications.api.v2.filters import CategoryFilter
from pushnotifications.api.v2.permissions import IsAuthenticatedOwnerOrReadOnly
from pushnotifications.api.v2.serializers import DeviceSerializer, MessageSerializer
from pushnotifications.models import Device, Category, Message
from thaliawebsite.api.v2.permissions import IsAuthenticatedOrTokenHasScopeForMethod
[docs]class DeviceListView(ListAPIView, CreateAPIView):
"""Returns an overview of all devices that are owner by the user."""
permission_classes = [
IsAuthenticatedOrTokenHasScopeForMethod,
IsAuthenticatedOwnerOrReadOnly,
]
serializer_class = DeviceSerializer
queryset = Device.objects.all()
required_scopes_per_method = {
"GET": ["pushnotifications:read"],
"POST": ["pushnotifications:write"],
}
[docs] def get_queryset(self):
if self.request.user:
return Device.objects.filter(user=self.request.user)
return super().get_queryset()
[docs]class DeviceDetailView(RetrieveAPIView, UpdateAPIView):
"""Returns details of a device."""
permission_classes = [
IsAuthenticatedOrTokenHasScope,
IsAuthenticatedOwnerOrReadOnly,
]
serializer_class = DeviceSerializer
required_scopes = ["pushnotifications:read", "pushnotifications:write"]
queryset = Device.objects.all()
[docs]class MessageListView(ListAPIView):
"""Returns a list of message sent to the user."""
serializer_class = MessageSerializer
required_scopes = ["pushnotifications:read"]
permission_classes = [
IsAuthenticatedOrTokenHasScope,
]
filter_backends = (OrderingFilter, CategoryFilter)
ordering_fields = ("sent",)
[docs] def get_queryset(self):
if self.request.user:
return Message.all_objects.filter(users=self.request.user)
return Message.all_objects.all()
[docs]class MessageDetailView(RetrieveAPIView):
"""Returns a message."""
serializer_class = MessageSerializer
required_scopes = ["pushnotifications:read"]
permission_classes = [
IsAuthenticatedOrTokenHasScope,
]
[docs] def get_queryset(self):
if self.request.user:
return Message.all_objects.filter(users=self.request.user)
return Message.all_objects.all()