api.py 4.53 KB
Newer Older
Luis García's avatar
Luis García committed
1
from django_filters.rest_framework import DjangoFilterBackend
Luis García's avatar
Fixes  
Luis García committed
2 3 4
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response as Http_Response
Luis García's avatar
Luis García committed
5
from rest_framework.viewsets import ModelViewSet
Luis García's avatar
Fixes  
Luis García committed
6
from api.mixins import OwnerFieldMixin, DestroyToInvisibleMixin
Luis García's avatar
Luis García committed
7
from api.permissions import IsSuperOrReadOnly, IsAuthOrReadOnly, IsStaffOrReadOnly
8 9 10 11
#from blog.models import Post, Comment, Reply, Category, Tag, PostTag
from blog.models import Post, Category, Tag, PostTag
#from blog.serializers import PostSerializer, CommentSerializer, ReplySerializer, CategorySerializer, TagSerializer, PostTagSerializer
from blog.serializers import PostSerializer, CategorySerializer, TagSerializer, PostTagSerializer
Luis García's avatar
Luis García committed
12 13 14 15

############################################################
#REST API for Post
############################################################
Luis García's avatar
Fixes  
Luis García committed
16
class PostViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
Luis García's avatar
Luis García committed
17
    #Everyone can read, just staff user can modificate
Luis García's avatar
Luis García committed
18 19
    permission_classes = (IsStaffOrReadOnly,)
    queryset = Post.objects.filter(visible=True)
Luis García's avatar
Luis García committed
20 21 22
    serializer_class = PostSerializer
    #Enable filter by code
    filter_backends = (DjangoFilterBackend,)
Luis García's avatar
Luis García committed
23
    filterset_fields = ('slug','category','tags__tag')
Luis García's avatar
Luis García committed
24

Luis García's avatar
Fixes  
Luis García committed
25 26 27 28 29 30 31 32 33
    @action(detail=True, methods=['post'], permission_classes=[AllowAny])
    def increase_rating(self, request, pk=None):
        post = self.get_object()
        data = {'rating': post.rating + 1}
        serializer = self.get_serializer(post, data=data, partial=True)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)
        return Http_Response(serializer.data)

34
"""
Luis García's avatar
Luis García committed
35 36 37
############################################################
#REST API for Comment
############################################################
38
class CommentViewSet(NestedViewSetMixin, ModelViewSet):
Luis García's avatar
Luis García committed
39
    permission_classes = (IsAuthOrReadOnly, )
Luis García's avatar
Luis García committed
40 41
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer
Luis García's avatar
Luis García committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    #Enable filter by comment and username
    filter_backends = (DjangoFilterBackend,)
    filterset_fields = ('post', 'username')

    def create(self, request, *args, **kwargs):
        #Set the username and email by default if the user is authenticated
        user = request.user
        if user.is_authenticated:
            request.data['username'] = user.username
            request.data['email'] = user.email
        return super(CommentViewSet, self).create(request, *args, **kwargs)


############################################################
#REST API for Reply
############################################################
class ReplyViewSet(NestedViewSetMixin, ModelViewSet):
    permission_classes = (IsAuthOrReadOnly, )
    queryset = Reply.objects.all()
    serializer_class = ReplySerializer
    #Enable filter by comment and username
Luis García's avatar
Luis García committed
63
    filter_backends = (DjangoFilterBackend,)
Luis García's avatar
Luis García committed
64
    filterset_fields = ('comment', 'username')
65 66 67 68 69 70 71

    def create(self, request, *args, **kwargs):
        #Set the username and email by default if the user is authenticated
        user = request.user
        if user.is_authenticated:
            request.data['username'] = user.username
            request.data['email'] = user.email
Luis García's avatar
Luis García committed
72
        return super(ReplyViewSet, self).create(request, *args, **kwargs)
73
"""
Luis García's avatar
Luis García committed
74 75 76 77

############################################################
#REST API for Category
############################################################
Luis García's avatar
Fixes  
Luis García committed
78
class CategoryViewSet(DestroyToInvisibleMixin, ModelViewSet):
Luis García's avatar
Luis García committed
79 80 81 82 83 84 85 86 87 88
    permission_classes = (IsStaffOrReadOnly, )
    queryset = Category.objects.filter(visible=True)
    serializer_class = CategorySerializer
    #Enable filter by name
    filter_backends = (DjangoFilterBackend,)
    filterset_fields = ('name', )

############################################################
#REST API for Tag
############################################################
Luis García's avatar
Fixes  
Luis García committed
89
class TagViewSet(ModelViewSet):
Luis García's avatar
Luis García committed
90 91 92 93 94 95 96 97 98 99
    permission_classes = (IsStaffOrReadOnly, )
    queryset = Tag.objects.all()
    serializer_class = TagSerializer
    #Enable filter by name
    filter_backends = (DjangoFilterBackend,)
    filterset_fields = ('name', )

############################################################
#REST API for PostTag
############################################################
Luis García's avatar
Fixes  
Luis García committed
100
class PostTagViewSet(ModelViewSet):
Luis García's avatar
Luis García committed
101 102 103 104 105 106
    permission_classes = (IsStaffOrReadOnly, )
    queryset = PostTag.objects.all()
    serializer_class = PostTagSerializer
    #Enable filter by name
    filter_backends = (DjangoFilterBackend,)
    filterset_fields = ('post', 'post')