我正在尝试创建一组与Django中的模型无关的端点。我正在使用Oauth2进行身份验证,并且一直出现403错误,要求缺少权限。
如何知道此端点缺少哪些权限?
我尝试使用 permission_classes = [IsAuthenticated] ,但没有成功
class RealTimeMonitoringViewSet(viewsets.ViewSet, NoModelBaseViewSet):
extra_serializers = {
"transcoding": serializers.RealTimeTranscodeSerializer,
}
@list_route(methods=["get"])
def transcoding(self, request):
if request.user.is_anonymous():
return Response(status=status.HTTP_401_UNAUTHORIZED)
if request.user.userprofile.isKmhAdmin:
data = get_queue()
else:
data = get_queue(request.user.userprofile.selectedClientId)
for jobqueue in data:
try:
if jobqueue['state'] != 'finished':
jobqueue['start_time'] = datetime.now()
jobqueue['end_time'] = datetime.now()
jobqueue['run_time'] = '0 min 0 sec'
jobqueue['file_name'] = Object.objects.get(pk=jobqueue.get('object_id')).name
except Object.DoesNotExist:
Log.exception("Transcode is returning an Object with {pk} that doesn't exist ".format(pk=jobqueue.get('object_id')))
serializer = serializers.RealTimeTranscodeSerializer(data=data, many=True)
if not serializer.is_valid():
return Response(status=status.HTTP_400_BAD_REQUEST)
return Response(data=serializer.data)
class NoModelBaseViewSet(viewsets.GenericViewSet):
authentication_classes = [OAuth2Authentication, SessionAuthentication]
pagination_class = pagination.PageNumberPagination
filter_backends = (
filters.OrderingFilter,
DjangoFilterBackend,
filters.SearchFilter,
)
extra_serializers = None
"""
Look for serializer class in self.extra_serializers, which
should be a dict mapping action name (key) to serializer class (value),
i.e.:
class MyViewSet(BaseViewSet, ViewSet):
serializer_class = MyDefaultSerializer
extra_serializers = {
'list': MyListSerializer,
'my_action': MyActionSerializer,
}
@action
def my_action:
...
If there's no entry for that action then just fallback to the regular
get_serializer_class lookup: self.serializer_class, DefaultSerializer.
"""
extra_model_permissions = None
def get_serializer_class(self):
default_serializer_class = super(NoModelBaseViewSet, self).get_serializer_class()
if self.extra_serializers is None:
return default_serializer_class
return self.extra_serializers.get(self.action, default_serializer_class)