69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
from django.shortcuts import render
|
|
|
|
from django.http import Http404
|
|
from django.shortcuts import render
|
|
from oauth2_provider.contrib.rest_framework import TokenMatchesOASRequirements
|
|
from rest_framework import viewsets, mixins
|
|
from rest_framework.generics import get_object_or_404
|
|
from rest_framework.viewsets import GenericViewSet
|
|
|
|
from .models import PushUser, PushMessage
|
|
from .serializers import PushUserSerializer, PushMessageSerializer
|
|
from apps.gooyal_oauth2.models import Application
|
|
from apps.gooyal_oauth2.rest_framework import IsAuthenticatedOrTokenMatchesOASRequirements
|
|
from apps.gooyal_oauth2.utils import get_application
|
|
from ..users.models import User
|
|
|
|
|
|
class PushUserViewSet(mixins.RetrieveModelMixin,
|
|
# mixins.UpdateModelMixin,
|
|
# mixins.DestroyModelMixin,
|
|
GenericViewSet):
|
|
queryset = PushUser.objects.all()
|
|
serializer_class = PushUserSerializer
|
|
|
|
permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements]
|
|
required_alternate_scopes = {
|
|
"GET": [["notifications.push:get_client_token"]],
|
|
}
|
|
|
|
def get_object(self):
|
|
user = self.request.user
|
|
auth = self.request.auth
|
|
if user:
|
|
# plan = Plan.objects.filter(application_id=auth.application_id).first()
|
|
push_user = PushUser.objects.submit(user)
|
|
return push_user
|
|
|
|
raise Http404
|
|
|
|
|
|
class ApplicationPushUserViewSet(
|
|
# mixins.RetrieveModelMixin,
|
|
mixins.ListModelMixin,
|
|
# mixins.UpdateModelMixin,
|
|
# mixins.DestroyModelMixin,
|
|
mixins.CreateModelMixin,
|
|
GenericViewSet
|
|
):
|
|
queryset = PushMessage.objects.all()
|
|
serializer_class = PushMessageSerializer
|
|
permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements]
|
|
required_alternate_scopes = {
|
|
"GET": [["notifications.application.push:submit_message"]],
|
|
"POST": [["notifications.application.push:submit_message"]],
|
|
}
|
|
|
|
def get_object(self):
|
|
queryset = self.get_queryset()
|
|
user_uuid = self.kwargs.get('user_uuid')
|
|
push_message = get_object_or_404(queryset, user=user_uuid)
|
|
return push_message
|
|
|
|
def perform_create(self, serializer):
|
|
user_uuid = self.kwargs.get('user_uuid')
|
|
user, _ = User.objects.get_or_create(uuid=user_uuid)
|
|
push_user = PushUser.objects.submit(user)
|
|
application = get_application(self.request)
|
|
instance = serializer.save(push_user=push_user, application=application)
|
|
instance.send_push()
|