portier/source/restapi/views.py

52 lines
1.7 KiB
Python
Raw Normal View History

from rest_framework_guardian.serializers import ObjectPermissionsAssignmentMixin
from rest_framework import serializers, viewsets
from rest_framework_guardian import filters
from config.models import Stream, Restream
class StreamSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):
class Meta:
model = Stream
fields = '__all__'
read_only_fields = ['publish_counter']
def get_permissions_map(self, created):
current_user = self.context['request'].user
return {
'view_stream': [current_user],
'change_stream': [current_user],
'delete_stream': [current_user]
}
class StreamViewSet(viewsets.ModelViewSet):
queryset = Stream.objects.all()
serializer_class = StreamSerializer
filter_backends = [filters.ObjectPermissionsFilter]
class RestreamSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):
class Meta:
model = Restream
fields = '__all__'
def get_permissions_map(self, created):
current_user = self.context['request'].user
return {
'view_restreamconfig': [current_user],
'change_restreamconfig': [current_user],
'delete_restreamconfig': [current_user]
}
def validate_stream(self, value):
request = self.context['request']
if not request.user.has_perm('config.view_stream', value):
raise serializers.ValidationError('Access to stream is not authorized')
return value
class RestreamViewSet(viewsets.ModelViewSet):
queryset = Restream.objects.all()
serializer_class = RestreamSerializer
filter_backends = [filters.ObjectPermissionsFilter]