views.py
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
from django.http import JsonResponse
from django.core.checks import messages
from .models import Posting, Comment
from .serializers import PostingSerializer, CommentSerializer
from rest_framework import viewsets, mixins
from rest_framework.response import Response
from rest_framework.filters import SearchFilter
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from .permissions import IsOwnerOrReadOnly
class PostingViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticatedOrReadOnly]
queryset = Posting.objects.all()
serializer_class = PostingSerializer
filter_backends = [SearchFilter]
search_fields = ('content', 'category') # only tuple
class MyPostingViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
queryset = Posting.objects.all()
serializer_class = PostingSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def get_queryset(self):
qs = Posting.objects.all()
qs = qs.filter(user=self.request.user)
return qs
class CommentViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
queryset = Comment.objects.all()
serializer_class = CommentSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)