Add shortcuts for viewset mixins
This commit is contained in:
parent
9617d9776b
commit
83ab4dffbe
|
@ -221,13 +221,13 @@ class SessionListSerializer(SessionSerializer):
|
||||||
|
|
||||||
class CourseSerializer(serializers.ModelSerializer):
|
class CourseSerializer(serializers.ModelSerializer):
|
||||||
name = serializers.CharField()
|
name = serializers.CharField()
|
||||||
description = serializers.CharField()
|
description = serializers.CharField(write_only=True)
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Course
|
model = models.Course
|
||||||
fields = '__all__'
|
fields = ['id', 'name', 'description']
|
||||||
|
|
||||||
class CourseDetailSerializer(serializers.ModelSerializer):
|
class CourseDetailSerializer(serializers.ModelSerializer):
|
||||||
sessions = SessionListSerializer(many=True)
|
sessions = SessionListSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Course
|
model = models.Course
|
||||||
|
|
|
@ -39,18 +39,13 @@ class IsInstructorOrReadOnly(BasePermission):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class RetrieveUpdateViewSet(
|
Base = viewsets.GenericViewSet
|
||||||
viewsets.GenericViewSet,
|
List = mixins.ListModelMixin
|
||||||
mixins.RetrieveModelMixin,
|
Retrieve = mixins.RetrieveModelMixin
|
||||||
mixins.UpdateModelMixin):
|
Create = mixins.CreateModelMixin
|
||||||
def list(self, request):
|
Update = mixins.UpdateModelMixin
|
||||||
return Response([])
|
Destroy = mixins.DestroyModelMixin
|
||||||
|
|
||||||
class CreateRetrieveUpdateDeleteViewSet(
|
|
||||||
RetrieveUpdateViewSet,
|
|
||||||
mixins.CreateModelMixin,
|
|
||||||
mixins.DestroyModelMixin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
search_strings = {}
|
search_strings = {}
|
||||||
|
@ -117,7 +112,7 @@ class SearchViewSet(viewsets.GenericViewSet, mixins.RetrieveModelMixin):
|
||||||
return Response({'seq': seq, 'results': serializer.data})
|
return Response({'seq': seq, 'results': serializer.data})
|
||||||
|
|
||||||
|
|
||||||
class MemberViewSet(RetrieveUpdateViewSet):
|
class MemberViewSet(Base, Retrieve, Update):
|
||||||
permission_classes = [AllowMetadata | IsAuthenticated, IsOwnerOrAdmin]
|
permission_classes = [AllowMetadata | IsAuthenticated, IsOwnerOrAdmin]
|
||||||
queryset = models.Member.objects.all()
|
queryset = models.Member.objects.all()
|
||||||
|
|
||||||
|
@ -128,21 +123,21 @@ class MemberViewSet(RetrieveUpdateViewSet):
|
||||||
return serializers.MemberSerializer
|
return serializers.MemberSerializer
|
||||||
|
|
||||||
|
|
||||||
class CardViewSet(CreateRetrieveUpdateDeleteViewSet):
|
class CardViewSet(Base, Create, Retrieve, Update, Destroy):
|
||||||
permission_classes = [AllowMetadata | IsAuthenticated, IsOwnerOrAdmin, IsAdminOrReadOnly]
|
permission_classes = [AllowMetadata | IsAuthenticated, IsOwnerOrAdmin, IsAdminOrReadOnly]
|
||||||
queryset = models.Card.objects.all()
|
queryset = models.Card.objects.all()
|
||||||
serializer_class = serializers.CardSerializer
|
serializer_class = serializers.CardSerializer
|
||||||
|
|
||||||
|
|
||||||
class CourseViewSet(viewsets.ModelViewSet):
|
class CourseViewSet(Base, List, Retrieve, Create, Update):
|
||||||
permission_classes = [AllowMetadata | IsAuthenticated, IsAdminOrReadOnly | IsInstructorOrReadOnly]
|
permission_classes = [AllowMetadata | IsAuthenticated, IsAdminOrReadOnly | IsInstructorOrReadOnly]
|
||||||
queryset = models.Course.objects.annotate(date=Max('sessions__datetime')).order_by('-date')
|
queryset = models.Course.objects.annotate(date=Max('sessions__datetime')).order_by('-date')
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
if self.action == 'retrieve':
|
if self.action == 'list':
|
||||||
return serializers.CourseDetailSerializer
|
|
||||||
else:
|
|
||||||
return serializers.CourseSerializer
|
return serializers.CourseSerializer
|
||||||
|
else:
|
||||||
|
return serializers.CourseDetailSerializer
|
||||||
|
|
||||||
|
|
||||||
class SessionViewSet(viewsets.ModelViewSet):
|
class SessionViewSet(viewsets.ModelViewSet):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user