d83cd58879
TestFlight builds use the production APNs endpoint, direct Xcode builds use sandbox. Store is_sandbox on Device so each token hits the right host.
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from app.subscriptions.models import Device
|
|
|
|
|
|
class SubscribeView(APIView):
|
|
def post(self, request):
|
|
feed = request.data.get("feed", "").strip()
|
|
device_token = request.data.get("device_token", "").strip()
|
|
|
|
if not feed or not device_token:
|
|
return Response(
|
|
{
|
|
"type": "Error",
|
|
"errors": ["feed and device_token are required"],
|
|
"data": None,
|
|
},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
is_sandbox = bool(request.data.get("sandbox", False))
|
|
|
|
device, created = Device.objects.update_or_create(
|
|
device_token=device_token,
|
|
defaults={"feed": feed, "is_sandbox": is_sandbox},
|
|
)
|
|
|
|
return Response(
|
|
{
|
|
"type": "Success",
|
|
"errors": [],
|
|
"data": {"feed": device.feed, "device_token": device.device_token},
|
|
"_links": {
|
|
"self": {"href": "/subscribe/", "method": "POST"},
|
|
"unsubscribe": {"href": "/unsubscribe/", "method": "DELETE"},
|
|
},
|
|
},
|
|
status=status.HTTP_201_CREATED if created else status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
class UnsubscribeView(APIView):
|
|
def delete(self, request):
|
|
device_token = request.data.get("device_token", "").strip()
|
|
|
|
if not device_token:
|
|
return Response(
|
|
{
|
|
"type": "Error",
|
|
"errors": ["device_token is required"],
|
|
"data": None,
|
|
},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
deleted, _ = Device.objects.filter(device_token=device_token).delete()
|
|
|
|
if deleted == 0:
|
|
return Response(
|
|
{
|
|
"type": "Error",
|
|
"errors": ["Device not found"],
|
|
"data": None,
|
|
},
|
|
status=status.HTTP_404_NOT_FOUND,
|
|
)
|
|
|
|
return Response(
|
|
{"type": "Success", "errors": [], "data": None},
|
|
status=status.HTTP_200_OK,
|
|
)
|