def notifications_details(self, request, username, subscription_id): obj = self.get_object() try: subscription = obj.subscription_set.get(id=subscription_id) except (Subscription.DoesNotExist, ValueError) as error: raise ParseError(str(error), "invalid") if request.method == "DELETE": self.perm_check(request) subscription.delete() return Response(status=HTTP_204_NO_CONTENT) if request.method == "GET": serializer = NotificationSerializer( subscription, context={"request": request} ) else: self.perm_check(request) serializer = NotificationSerializer( subscription, data=request.data, context={"request": request}, partial=request.method == "PATCH", ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK)
def notifications(self, request, **kwargs): obj = self.get_object() if request.method == "POST": self.perm_check(request) with transaction.atomic(): serializer = NotificationSerializer( data=request.data, context={"request": request}) serializer.is_valid(raise_exception=True) serializer.save(user=obj) return Response(serializer.data, status=HTTP_201_CREATED) queryset = obj.subscription_set.order_by("id") page = self.paginate_queryset(queryset) serializer = NotificationSerializer(page, many=True, context={"request": request}) return self.get_paginated_response(serializer.data)