def setUp(self): self.user = get_user_model().objects.create_user( email="*****@*****.**", password="******", name="Test User") self.user_superuser = get_user_model().objects.create_superuser( email="*****@*****.**", password="******", ) self.project_payload = { "name": "Test", "description": "test", "proposed_by": self.user } self.permission = IsOwnerOrReadOnly() self.project = ProjectModel.objects.create(name="Test", description="test", proposed_by=self.user) self.issue = IssueModel.objects.create(name="Test", description="test", cost=12, project=self.project, assigned_to=self.user) self.issue_2 = IssueModel.objects.create( name="Test", description="test", cost=12, project=self.project, assigned_to=self.user_superuser)
class BoardViewSet(viewsets.ModelViewSet): queryset = Board.objects.all() serializer_class = api.BoardSerializer filter_backends = (DjangoFilterBackend, OrderingFilter) filter_fields = ("submitter__username", ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [IsOwnerOrReadOnly("submitter"), ]
class PinViewSet(viewsets.ModelViewSet): queryset = Pin.objects.all().select_related('image', 'submitter') serializer_class = api.PinSerializer filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ("submitter__username", 'tags__name', ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [IsOwnerOrReadOnly("submitter"), ]
class BoardViewSet(viewsets.ModelViewSet): serializer_class = api.BoardSerializer filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter) search_fields = ("name", ) filter_fields = ("submitter__username", ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [ IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter") ] def get_queryset(self): return filter_private_board(self.request, Board.objects.all())
class PinViewSet(viewsets.ModelViewSet): serializer_class = api.PinSerializer filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ( "submitter__username", 'tags__name', ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [ IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter") ] def get_queryset(self): query = Pin.objects.all() request = self.request return filter_private_pin(request, query)
class PinViewSet(viewsets.ModelViewSet): serializer_class = api.PinSerializer filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ( "submitter__username", 'tags__name', ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [ IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter"), ] def get_queryset(self): # 用Q函数过滤掉审核失败的PIN ~的意思是取反 20200428 query = Pin.objects.all().filter(~Q(check=2)) request = self.request return filter_unchecked_and_private_pin(request, query)
class UserLikesViewset(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.RetrieveModelMixin): """ list: 获取用户点赞列表 retrieve: 判断某个PIN是否已经点赞 create: 点赞 """ permission_classes = [IsOwnerOrReadOnly("user"), ] lookup_field = "pin_id" def get_queryset(self): return UserLikes.objects.filter(user=self.request.user) # 用get_serializer_class 设置动态的Serializer def get_serializer_class(self): if self.action == "list": return UserLikesDetailSerializer elif self.action == "create": return UserLikeSerializer return UserLikeSerializer